Intermittent test failures due to (it seems) `function_exported?`

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 used 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).

Have you looked at the documentation for function_exported?? It suggests that it will return false if the module in question has not been loaded yet. Perhaps your test needs to call Code.ensure_loaded(Jason) before the with expression?

2 Likes

Thanks a lot for your help!

In the end, it looks like the problem was between the keyboard and chair: it seems like I was having an issue with a test which I fixed, and then I had an issue with a different test where the test code itself was incorrect and confused the 2 due to similar test names. In any case, I’m unable to reproduce any issues now…

2 Likes