Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Flaky use test #94

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions test/support/case.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule BoomNotifier.Case do
@moduledoc false

use ExUnit.CaseTemplate

setup do
BoomNotifier.TestMessageProxy.subscribe(self())
end
end
59 changes: 59 additions & 0 deletions test/support/test_message_proxy.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
defmodule BoomNotifier.TestMessageProxy do
@moduledoc """
A gen server that forwards messages to subscribed pids.
Usefull to send responses to test example processes.

Example:

use BoomNotifier.Case

test "receive a message from elsewhere" do
spawn_link(fn -> send(Process.whereis(BoomNotifier.TestMessageProxy), :message) end)

assert_receive(:message)
end
"""
use GenServer

def subscribe(pid) do
GenServer.call(__MODULE__, {:subscribe, pid}, 100)
end

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

def stop(reason \\ :shutdown) do
GenServer.stop(__MODULE__, reason)
end

@impl true
def init(_) do
{:ok, []}
end

@impl true
def handle_call({:subscribe, pid}, _from, state) do
Process.monitor(pid)

{:reply, :ok, [pid | state] |> Enum.uniq()}
end

@impl true
def handle_info({:DOWN, _ref, :process, pid, _reason}, state) do
{:noreply, Enum.reject(state, &(&1 == pid))}
end

def handle_info(message, state) do
broadcast(message, state)

{:noreply, state}
end

defp broadcast(_message, []), do: nil

defp broadcast(message, [pid | rest]) do
send(pid, message)
broadcast(message, rest)
end
end
1 change: 1 addition & 0 deletions test/unit/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
ExUnit.start()
BoomNotifier.TestMessageProxy.start_link()
Application.ensure_all_started(:bypass)
6 changes: 2 additions & 4 deletions test/unit/use_test.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
defmodule BoomNotifier.UseTest do
use ExUnit.Case
use BoomNotifier.Case

@already_sent {:plug_conn, :sent}

defmodule Notifier do
def notify(%{name: name}, _) do
pid = Process.whereis(BoomNotifier.UseTest)
pid = Process.whereis(BoomNotifier.TestMessageProxy)
send(pid, {:notification_sent, name})
end
end
Expand All @@ -15,8 +15,6 @@ defmodule BoomNotifier.UseTest do
end

setup do
Process.register(self(), BoomNotifier.UseTest)

%{conn: Plug.Test.conn(:get, "/") |> Map.put(:owner, self())}
end

Expand Down
Loading