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?

First 2 of 2 Posts Switch mode

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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
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
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
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
rahultumpala
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 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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
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

We're in Beta

About us Mission Statement