Fl4m3Ph03n1x

Fl4m3Ph03n1x

Background

I have a process that tries to establish a connection to a server and I am testing it. On the happy path, I check that the process does it’s job once it connects.

But its the sad path that is interesting. Should the process fail to connect to the server, I want it to be gracefully terminated and to stop initialization. How do I test this without making the test fail? (the expected behaviour is to have the process die after all)

Code

Worker:

def init(state) do
  Process.flag(:trap_exit, true)
  {:ok, state, {:continue, :establish_conn}}
end

def handle_continue(:establish_conn, state) do
   
    conn_deps = [
      open_fn:      state.deps.http.open,
      await_fn:     state.deps.http.await_up,
      dump_error:   state.deps.dumper.save_failed_request
    ]

    base_url = state.opts.base_url

    with  {:ok, conn_pid} <- Logic.establish_connection(base_url, conn_deps)
    do
      new_state = %{state | conn_pid: conn_pid}
      {:noreply, new_state}
    else
      {:error, reason} -> {:stop, reason, state}
    end
  end

  def terminate(_reason, state) do
    Logic.close_connection(state.conn_pid, [close_fn: state.deps.http.close])
  end

Because connections take a long time to establish, I use handle_continue. However, since in this case the process will fail to connect, handle_continue will return {:stop, reason, state} and prevent the process from continuing, which is expected.

The process then suicides itself and executes the terminate function.

Question

How do I test this without making the tests fail?

Showing Posts 1 to 2

peerreynders

peerreynders

Anything wrong with the terminate_wait approach?

So for example you could just stick a monitor on the process and wait for the :DOWN message with a blocking receive with a timeout.

Fl4m3Ph03n1x

Fl4m3Ph03n1x OP

Actually, simply using the format:


    test "closes connection if it cannot establish it" do
      Process.flag :trap_exit, true

      args = %{a: 1, b: 2}

      Worker.start_link(args)

      assert_receive {:EXIT, _, :my_awesome_termination_reason_here_in_this_place}
      Process.flag :trap_exit, false
    end

Does the trick. No supervisor needed

— 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
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews