I’m facing a flaky test, and it seems to be due to the following code:
defp get_default_encoder(opts) do
with true <- Keyword.get(opts, :encoder_fallback, true),
true <- function_exported?(Jason, :encode, 2) do
&Jason.encode(&1, [])
else
_ -> nil
end
end
In particular, function_exported?(Jason, :encode, 2)
will sometimes evaluate as true
(which is correct) and sometimes it will be false
.
What could be happening? How can I prevent it?
I’m having this issue in the context of an open source API wrapper I’m working on, so the code in question is visible at https://github.com/davidsulc/scrapy_cloud_ex/blob/master/lib/endpoints/app/jobs.ex#L140
This issue started popping up in this commit where I’m using meta-programming to define tests (see https://github.com/davidsulc/scrapy_cloud_ex/blob/http_adapter_tests/integration_test/http_adapter.ex which is use
d in https://github.com/davidsulc/scrapy_cloud_ex/blob/http_adapter_tests/test/http_adapters/default_test.exs)
For context, this is an API wrapper and I wanted users to be able to use whatever HTTP client and JSON library they wanted by writing their own adapter (the default adapter uses Hackney and Jason). I wanted to provide a “one line test” for users to be able to test their adapter works correctly (and have all adapters go through the same tests), hence the meta-programming (I’m open to other approaches).