brainbag
Can't figure out how to assert error in linked process with ExUnit
I am using an Agent for state. Nothing I have tried for asserting an ArgumentError raised in the Agent has worked! The test fails and the process dies without the assertion working.
Here’s some code that demonstrates. In this function add_action, I want to verify that the value of the map key :actions is a list, and hasn’t been changed to something else.
def start_link do
Agent.start_link(@name, :init, [], name: @name)
end
def init do
%{actions: []}
end
def add_action(action) when is_atom(action) do
Agent.get_and_update(@name, fn(map) ->
result = Map.update!(map, :actions, fn
list when is_list(list) -> [action | list]
_ -> raise ArgumentError, message: "key :actions is not a list!"
end)
{result, result}
end)
end
Here is the test:
setup do
{:ok, pid} = State.start_link
[pid: pid]
end
test "action example", context do
# assume State.actions is `42` here instead of `[]`
assert_raise ArgumentError, fn ->
# the process dies here, so `assert_raise` is never called
State.add_action :failure
end
end
I have tried catch_exit, catch_error, catch_throw, catch_pokemon, Process.monitor, receive, etc. No matter what, I still get this error message
=ERROR REPORT==== 1-Oct-2016::19:39:16 ===
** Generic server 'Elixir.ManOrActionMan.State' terminating
** Last message in was {get_and_update,
#Fun<Elixir.DepsInstall.State.0.35286781>}
*snip*
I can’t seem to figure out how to catch that ArgumentError, or if that’s even the right approach to raising errors in a process.
Any help is much appreciated!
Trending in Questions
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
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
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
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
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
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
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
@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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance











First 8 of 8 Posts
JEG2
I cracked up at
catch_pokemon.You can’t catch the error because it doesn’t happen in the test process. Test it as follows:
assert_receivedon the{:EXIT, …}messageHope that helps.
brainbag
Thanks! I tried that, but I can’t seem to make it work, the process still fails before it gets to the receive. Does this look right?
josevalim
Trapping exits means that the test process won’t be terminated when the linked process terminates. The call to
State.add_action :failurewill still fail though so you’ll need tocatch_exitit.Btw, I am also wondering why are you getting exits formatted using the Erlang logger instead of the Elixir one in your initial report.
brainbag
Thanks @JEG2 and @josevalim by your powers combined I am Captain Solution! The tests are passing now.
In case anyone else ever stumbles through this, here’s my final(?) working test:
@josevalim Your message prompted me to remember that I removed
:loggerfrommix.exsa while ago as a newbie mistake; putting it back changed the Erlang error to an Elixir one. I still see an error print out during the test run, but it passes. I’m sure it’s my fault somehow.josevalim
Great! Btw, adding “@tag :capture_log” before the test should silence the
log reports.
JEG2
Yeah, sorry about this. As @josevalim pointed out, I led you astray. To try and make up for it, I’ll try to explain why this happens.
The other process dies during a
GenServer.call()(Agentis just a wrapper over theGenServerlayer.) If the called process dies mid-call,GenServer(or really the lower layergen_server) will try to exit the current process.That sounded radical to me when I first learned of it, but it makes sense, if you think about it.
GenServer.call()is expected to return a value and there’s no return value that makes sense. The expected flow of operations cannot continue.Anyway, I hope that helps. Sorry I didn’t take the time to actually understand your problem to begin with.
josevalim
To be honest, it also took me a while to figure out why trapping exits was not enough.
pradeep-in
Any way to test similar things for non-linked processes? Like tasks under a different supervisor, so no way to trap exits. How can we ensure tests in those scenerios?