gmile

gmile

An issue with mock library

It appears I am doing something off, as after mocking a module using mock, the module is completely unloaded from memory and subsequent code that depends on the module being there fails.

As a working proof of concept, the following script fails - the error is raised every time execution gets to run the last line, e.g. MyModule3.my_fun(1):

Mix.install [:mock]

defmodule MyModule1 do
  def my_fun(arg) do
    arg + 1
  end
end

defmodule MyModule2 do
  import Mock

  def my_fun(_arg) do
    with_mock(MyModule1, my_fun: fn _number -> 123 end) do
      MyModule1.my_fun(1)
    end
  end
end

MyModule1.my_fun(1) |> IO.inspect
MyModule2.my_fun(1) |> IO.inspect
MyModule1.my_fun(1) |> IO.inspect

The failure is:

$ elixir example.exs
2
123
** (UndefinedFunctionError) function MyModule1.my_fun/1 is undefined (module MyModule1 is not available)
    MyModule1.my_fun(1)
    example.exs:27: (file)
    (elixir 1.14.1) lib/code.ex:1245: Code.require_file/2
$

After digging a little, the mocking code appears to be reasonably straightforward, which leaves me quite puzzled as to what’s going on:

https://github.com/jjh42/mock/blob/f3580cb68692b41ee59e9784ba253473dab9420d/lib/mock.ex#L340-L368

Trivia:

elixir --version
Erlang/OTP 25 [erts-13.1.1] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.14.1 (compiled with Erlang/OTP 25)

Marked As Solved Switch mode

al2o3cr

al2o3cr

:meck.unload doesn’t “put the module back”, it unloads it entirely (from the docs):

This will purge and delete the module(s) from the Erlang virtual machine. If the mocked module(s) replaced an existing module, this module will still be in the Erlang load path and can be loaded manually or when called.

Future calls like MyModule1.my_fun will try to load MyModule1 from disk, but if it’s not compiled it won’t be found at all.

For instance, I made your example pass by extracting MyModule1 to on_disk.ex (the name is arbitrary, but the ex is important):

defmodule MyModule1 do
  def my_fun(arg) do
    arg + 1
  end
end

and then compiling it with elixirc on_disk.ex.

Then running the remainder as elixir example.exs prints out:

2
123
2

as expected.

Last Post!

gmile

gmile

@al2o3cr thank you, your notes were instrumental in helping us understand what was going on with mocking in our case!

While my example above ended up being overly simplified, our actual situation turned out to be much more involved. I will describe it below, but a word of warning: it will sound like it’s not related to what has been so far discussed in this thread :slight_smile:

We were mocking something in a test, and seeing the whole test suit stop working as a consequence of that. A suite would throw a cryptic error saying that such and such process name cannot be located.

It turned out that, at the time of mocking the specific module (and as you pointed out, that modules’ code being completely unloaded from memory), code from that module also happened to be running in the background (which is by design).

We were mocking a call to :hackney.request, and there was some code in the background that was executing a long-running :hackney.request. Our module happens to be a part of app supervision tree, so unloading :hackney from memory resulted in taking down entire app (e.g. during test suite run) with it.

Conclusion: take great care if you absolutely must use Mock library. Avoid it altogether if you can.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement