r11na

r11na

I’m having difficult figuring out how to stop supervisors running genservers during my tests and I haven’t found any helpful guides online.

I have the following setup in a test file:

setup do
  {:ok, server_pid} = Program.Myserver.start_link()
  {:ok, server: server_pid}
end

When I run my tests I get an
** (MatchError) no match of right hand side value: {:error, {:already_started, pid<0.405.0>}}

No doubt because the supervisor has already started the genserver but I don’t know how to stop it doing this during my tests…

Any ideas on the best way to stop this?

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

You just need to tear it down after, see another thread already talking about this at:

:slight_smile:

michalmuskala

michalmuskala

Since Elixir 1.5 the recommened approach for starting processes in a test is to use start_supervised/2. This is an easy way to ensure the process is started and stopped together with the test process.

r11na

r11na OP

I tried to do this:

setup do
    start_supervised(CeresCore.Queue.Consumers.PaymentConsumer)
end

But I get the below error…

1) test consume a payment message (CeresCore.Queue.Consumers.PaymentConsumerTest)
test/ceres_core/queue/consumers/payment_consumer_test.exs:13
** (RuntimeError) expected ExUnit callback in 
CeresCore.Queue.Consumers.PaymentConsumerTest to return :ok | keyword | map, got {:error, {{:EXIT, {:undef, [{CeresCore.Queue.Consumers.PaymentConsumer, :start_link, [[]], []}, {:supervisor, :do_start_child, 2, [file: 'supervisor.erl', line: 365]}, {:supervisor, :handle_start_child, 2, [file: 'supervisor.erl', line: 724]}, {:supervisor, :handle_call, 3, [file: 'supervisor.erl', line: 422]}, {:gen_server, :try_handle_call, 4, [file: 'gen_server.erl', line: 636]}, {:gen_server, :handle_msg, 6, [file: 'gen_server.erl', line: 665]}, {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 247]}]}}, {:child, :undefined, CeresCore.Queue.Consumers.PaymentConsumer, {CeresCore.Queue.Consumers.PaymentConsumer, :start_link, [[]]}, :permanent, 5000, :worker, [CeresCore.Queue.Consumers.PaymentConsumer]}}} instead
stacktrace:
(ex_unit) lib/ex_unit/callbacks.ex:455: ExUnit.Callbacks.raise_merge_failed!/2
r11na

r11na OP

Is there like an example project where this is done with start_supervised?

I see lots of snippets of code but I can’t seem to get any of them working…and I don’t quite know what the error message wants me to fix…

blatyo

blatyo

Conduit Core Team

The setup callback needs you to return one of those types. Like:

setup do
  start_supervised(CeresCore.Queue.Consumers.PaymentConsumer)
  :ok
end

The other issue appears to be that CeresCore.Queue.Consumers.PaymentConsumer.start_link/1 is not a function that exists.

r11na

r11na OP

I’ve changed my setup call back to what you specified and that fixed one error, so I’m one step closer…

and I made the start_link function as follows to define start_link/1

  def start_link(_ \\ nil) do
      GenServer.start_link(__MODULE__, [], name: __MODULE__)
  end

However start_supervised still returns already_started error…

{:error,
{{:already_started, #PID<0.407.0>},
{:child, :undefined, CeresCore.Queue.Consumers.PaymentConsumer,
{CeresCore.Queue.Consumers.PaymentConsumer, :start_link, [[]]}, :permanent,
5000, :worker, [CeresCore.Queue.Consumers.PaymentConsumer]}}}
blatyo

blatyo

Conduit Core Team

What I typically do for this situation is set something like this in config/test.exs:

config :my_app, :env, :test

Then in my application supervisor, I separate out the processes I don’t want to start automatically and check the env.

children = [{MyApp.Endpoint, []), {MyApp.Repo, []}]

children = case Application.get_env(:my_app, :env) do
  :test -> children ++ [{MyApp.OtherThing, []}]
  _ -> children
end

Supervisor.init(children, strategy: :one_for_one)

If there’s a better way than this, I’m not aware of it.

r11na

r11na OP

Thats seems helpful but I’m still unable to just get the tests running sadly.
When I try to do it it just results in lower level supervisors not running…

The application itself works fine… but the tests won’t start.

Is there anywhere to find a full working example of using start_supervised?
So I can do a line by line comparison with my code.

All I find are snippets but they just aren’t working for me.

edit I was able to get get it sort of working now by conditionally loading genservers based on the environment. Seems clunky having to litter every single supervisor with an environment check, but I guess thats what has to be done.

Thanks for your help.

SZJX

SZJX

I think the OP’s problem is that they defined some GenServer to be started in application.ex, but wants to start the same server under the test setup block, therefore encountered the :already_started error. It’s not the same issue as the one in the link you provided.

SZJX

SZJX

It seems that the easiest way to solve it is just to start your GenServer under a different name, using the :name option? Though if your GenServer functions depend on a hardcoded :name such as __MODULE__ it would indeed making testing harder.

e.g. instead of writing

  def start_link(_) do
    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
  end

write

  def start_link(opts) do
    GenServer.start_link(__MODULE__, :ok, opts)
  end

and then the child spec:

      %{
        id: MyApp.MyGenServer,
        start: {MyApp.MyGenserver, :start_link, [[name: MyApp.MyGenServer]]}
      }

then in your test you can give a different name, e.g.

  setup context do
    _ = start_supervised!({MyApp.MyGenserver, name: context.test})
    %{producer: context.test}
  end
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
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
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews