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

Tee
can someone please explain to me how Enum.reduce works with maps
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement