budgie

budgie

Can I stop swoosh test adapter from sending a message to self()?

Swoosh is mucking with a few of my tests. It looks like there is no configurability in the test adapter to change the behavior and get it to stop sending messages. Is there an alternative test adapter that people are using? Maybe the Logger adapter?

It would seem like a silly solution to implement a handle_info catch-all in my LiveView just to make the tests pass.

defmodule Swoosh.Adapters.Test do
  @moduledoc ~S"""
  An adapter that sends emails as messages to the current process.

  This is meant to be used during tests and works with the assertions found in
  the [Swoosh.TestAssertions](Swoosh.TestAssertions.html) module.

  ## Example

      # config/test.exs
      config :sample, Sample.Mailer,
        adapter: Swoosh.Adapters.Test

      # lib/sample/mailer.ex
      defmodule Sample.Mailer do
        use Swoosh.Mailer, otp_app: :sample
      end
  """

  use Swoosh.Adapter

  def deliver(email, _config) do
    for pid <- pids() do
      send(pid, {:email, email})
    end

    {:ok, %{}}
  end

  def deliver_many(emails, _config) do
    for pid <- pids() do
      send(pid, {:emails, emails})
    end

    responses = for _email <- emails, do: %{}

    {:ok, responses}
  end

  # Essentially finds all of the processes that tried to send an email (in the test)
  # and sends an email to that process.
  defp pids do
    if pid = Application.get_env(:swoosh, :shared_test_process) do
      [pid]
    else
      Enum.uniq([self() | List.wrap(Process.get(:"$callers"))])
    end
  end
end

First Post!

Eiji

Eiji

Yes and no … Even if you cannot stop some process from sending any messages to self() then you can “silence” such messages by ensuring that self() is not a current test process. Consider simple example:

defmodule Example do
  def sample1 do
    send(self(), :ok)
    notify(1)
  end

  def sample2 do
    spawn(fn -> send(self(), :ok) end)
    notify(2)
  end

  defp notify(step) do
    receive do
      :ok -> IO.puts("[#{step}/2] Received :ok")
    after
      1000 -> IO.puts("[#{step}/2] No message received")
    end
  end
end

Example.sample1()
Example.sample2()

When running this code using elixir example.exs we would got such result:

[1/2] Received :ok
[2/2] No message received

However if you have to send to self() only your messages then it’s also very easy to do, see:

defmodule Example do
  def sample1 do
    send(self(), "ok")
    notify(1)
  end

  def sample2 do
    parent_pid = self()

    spawn(fn ->
      send(self(), "example message from library")
      send(parent_pid, "custom message")
    end)

    notify(2)
  end

  defp notify(step) do
    receive do
      message -> IO.puts("[#{step}/2] Received: #{message}")
    after
      1000 -> IO.puts("[#{step}/2] No message received")
    end
  end
end

Example.sample1()
Example.sample2()

After such changes here is what we’ve got in console:

[1/2] Received: ok
[2/2] Received: custom message

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31494 112
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40042 209
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement