Test failing randomly, around once on five times

In a library I am developing I execute the following:

if function_exported?(type, :foo, 0) do
  # some code

When I run my tests, once on five this function will return false, for the same data.

So I was looking to get some more information, and inserted the code below to do some inspection, right before the condition:

if type == FooLib.Foo do
  IO.inspect function_exported?(type, :foo, 0)
  IO.inspect type.foo()
end

But since I added this code, the if condition thereafter now always returns true for the same data… and tests always pass.

Something weird is going on. Any idea?

My guess is sometimes the BEAM gets opportunistically lazy about mounting modules (you can change the BEAM vm mode to embedded to solve this). I’m not sure that’s the best solution; if this is the problem it results from sort of race condition. Are there four other async tests that are using that module? Then function_exported? is failing when it happens to be runnning first. Maybe try dropping Code.ensure_loaded?(type) in front, as a better solution than changing how your BEAM is built.

2 Likes

thank you