How to test terminate callback for GenServer

I’m finishing reading Dave Thomas’s book “Programming Elixir 1.6”. And I was faced with a question about how to test terminate callback from GenServer. I made a test for this, but I’m not sure that it’s a good practice to test in the way that I did. Also, I didn’t find an answer on how to test terminate on google and this forum.

Here is an example of how my GenServer looks like.

defmodule Sequence.Server do
  use GenServer

  @me __MODULE__

  #####
  # External API

  def init(_) do
    {:ok, Sequence.Stash.get()}
  end

  def start_link(_) do
    GenServer.start_link(@me, nil, name: @me)
  end

  def current_number do
    GenServer.call(@me, :current_number)
  end

  def next_number do
    GenServer.call(@me, :next_number)
  end

  def update_number(new_number) when is_integer(new_number) do
    GenServer.cast(@me, {:update, new_number})
  end

  def increment_number(delta) do
    GenServer.cast(@me, {:increment_number, delta})
  end

  #####
  # GenServer implementation

  def handle_call(:next_number, _from, current_number) do
    {:reply, current_number, current_number + 1}
  end

  def handle_call(:current_number, _from, current_number) do
    {:reply, current_number, current_number}
  end

  def handle_cast({:increment_number, delta}, current_number) do
    {:noreply, current_number + delta}
  end

  def handle_cast({:update, new_number}, _current_number) do
    {:noreply, new_number}
  end

  def terminate(_reason, current_number) do
    Sequence.Stash.update(current_number)
  end

  def format_status(_reason, [_pdict, state]) do
    [data: [{'State', "My current state is '#{inspect(state)}'"}]]
  end
end

And here is an example of how I tried to test this callback.

defmodule ServerTest do
  use ExUnit.Case

  alias Sequence.Server

  import :timer, only: [ sleep: 1 ]

  setup do
    Server.update_number(1)
  end

  test "terminate returns last value before terminating" do
    assert Server.next_number == 1

    Server.increment_number("cat")

    sleep(500)
    assert Server.current_number == 2
  end
end

GenServer.stop/3 should be useful here:

Synchronously stops the server with the given reason.
The terminate/2 callback of the given server will be invoked before exiting. This function returns :ok if the server terminates with the given reason; if it terminates with another reason, the call exits.

1 Like

Thanks for your comment and suggestion. Unfortunately I still can’t test a message that terminate callback returns. And also I can call terminate without stop simply passing to the function string instead of number that I already did.

Server.increment_number("cat")

I changed a test to this with stop function.

test "terminate returns last value before terminating" do
    assert Server.next_number == 1

    assert :ok = GenServer.stop(Server)

    sleep(500)
    assert Server.current_number == 2
  end

I don’t think you should be making assertions on the result of the terminate callback. I think a better approach would be to make assertions on the observable effects of calling the callback:

test "terminate updates the stash with the last value when terminating" do
  assert Server.next_number() == 1

  assert :ok = GenServer.stop(Server)

  # No need to sleep here, GenServer.stop is a synchronous call.

  # Make assertion on the Stash
  assert Sequence.Stash.get() == ...
end
1 Like

Yes, I think it makes sense, thanks :+1: