Closed feifanzhou closed 3 years ago
I am suspecting this is a race condition in your tests.
with_mock
is most likely replacing your module B2.RequestContext
and, while it is being replaced, it affects how other modules that invoke B2.RequestContext
are compiled. Does the issue happens if you set async: false
on all tests that mock this struct or if you do mix test --max-cases 1
?
Thank you José! That does appear to be the problem — toggling async
on my tests makes the problem go away or come back. It hadn't occurred to me that async tests would still share a single compiled "instance" of a particular module. Are there any plans to increase isolation when running async tests, such that these tests could run concurrently?
@feifanzhou the thing to keep in mind is that ExUnit runs async tests while test files are still loading. And then the mock library is globally replacing a module while that happens. You should either use a mock library that is safe under concurrency (such as Mox) or run tests synchronously.
Closing this, as there is nothing the compiler can do if something is changing modules globally. :) Thank you!
Environment
Erlang/OTP 24 [erts-12.0.3] [source] [64-bit] [smp:24:24] [ds:24:24:10] [async-threads:1] [jit] [dtrace]
Elixir 1.12.2 (compiled with Erlang/OTP 24)
Current behavior
Hello! I'm getting the error
** (UndefinedFunctionError) function B2.RequestContext.__struct__/0 is undefined or private
whenmix test
gets to a particular ExUnit test that references the module in the error (see below for the full text of the error and source of the struct and test file).Unfortunately, I'm not sure how specifically to induce the problem, although it does happen almost all the time. Additional behaviors I notice:
@tag :skip
mix test
to run my entire test suite.mix test filename:linenumber
to run just the specific test identified in the error.mix test
will occasionally pass, but usually crash with the same error on a different test that references this struct.mix clean
sometimes makes the problem go away on the next run ofmix test
, but other times the crash will still happen aftermix clean
.Expected behavior
This crash shouldn't happen; the
__struct__/0
function should be defined.Appendix
I'm happy to add people to my private repo with the full project if that helps repo. But to start, here's the error message and relevant code:
Full error from running
mix test
:Struct module source:
Test code source: