fireproofsocks

fireproofsocks

Restarting a process?

I’ve got a simple app that starts up a supervised Broadway pipeline. This is fine for regular use, but it makes testing difficult:

  def start(_type, _args) do
    children = [{My.Pipe, []}]
    opts = [strategy: :one_for_one, name: My.Supervisor]
    Supervisor.start_link(children, opts)
  end

I’m wondering if there’s a way to re-start the process with different options specifically for testing? I would like to send some different options to the start_link/1 function specifically to help override things for testing.

Is this possible? Or should I rely instead on using Application.put_env/3 to modify settings at (testing) runtime?

Most Liked

LostKobrakai

LostKobrakai

I tend to avoid testing processes started by the applications supervision tree. I start a separate (set) of them manually and without global naming and pass the pid around to tests. This way I often get the ability to run tests async as well.

idi527

idi527

Just in case, here’s how I’d use start_supervised. Not sure if it addresses the problem.

Relevant bits:

  • in test env the “real” pipeline is disabled in the config
  • if config says the pipeline is disabled, it’s not added to the in the root supervisor, so that it doesn’t clash with any pipelines started in tests
  • the pipeline callback module has default opts which can be overwritten in tests
fireproofsocks

fireproofsocks

I think I found one way of working with this based on this post.

First was to make the Broadway :name option overrideable by using Keyword.get/3 with a default value:

defmodule My.Pipeline do
    use Broadway

    def start_link(opts) do
        Broadway.start_link(__MODULE__,
        name: Keyword.get(opts, :name, __MODULE__),
        # ... etc ...
     )
   end
   # ... 
end

Then I can pass a unique :name option to my pipeline when I call start_supervised in my test:

test "start_link with custom options"
    {:ok, pid} = start_supervised({My.Pipeline,
                   [
                     queue_url: "fake-url",
                     producer: Broadway.DummyProducer,
                     # ... etc ...
                     name: :example
                   ]}
end

and it works equally well when I call start_link/1 directly:

{:ok, pid} = My.Pipeline.start_link(
                producer: Broadway.DummyProducer,
                queue_url: "fake-url",
              name: :something
              )

This works and I will probably use this method… the only downside I can see is that the regular built-in process in the application’s supervisor always starts… but we kinda just ignore it. That’s not a problem for my use case, however.

I think the docs for https://hexdocs.pm/ex_unit/master/ExUnit.Callbacks.html?#start_supervised/2 need some updating because they don’t really explain how to work with this common use case… I’m not sure what I did is even the proper way to work with that function… it just feels like a work-around more than anything else. Thoughts?

Last Post!

alecnmk

alecnmk

:wave: 3 years later! spotted this post while dealing with some legacy code.

If you really need to restart a process that’s part of the app sup tree, say in the setup block of your test, you can do that with:

:ok = Supervisor.terminate_child(My.Supervisor, My.Pipe)
{:ok, _} = Supervisor.restart_child(My.Supervisor, My.Pipe)

And in order to make it work you’d need to name your Supervisor in the application start, like:

Supervisor.start_link(children, name: My.Supervisor)

Note: restarting child would use original child spec defined in your app. So if you’d like to override some options passed into a supervised module you’d need to not rely on actual options in the child spec, but on the configuration of your supervised module that’s pulled from the app configs. In the original example that topic started brought up that My.Pipe is getting started with empty options.

I saw that approach recommended by @sasajuric a while back and I would suggest you do that only if you have no other way and you just need to get things done quick. Of course, most likely you wouldn’t like to have such test running async.

But the approach with start_supervised that @LostKobrakai and @idi527 pointed out above is what is probably an ideal way of doing things :+1:

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement