Skip to content

Commit

Permalink
Always use a free port in adapter tests (#119)
Browse files Browse the repository at this point in the history
This ensure that we always use a free port for the fake server.
  • Loading branch information
paulcsmith committed Apr 13, 2016
1 parent aab2ce0 commit 1de0bc4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
12 changes: 10 additions & 2 deletions test/lib/bamboo/adapters/mandrill_adapter_test.exs
Expand Up @@ -20,8 +20,16 @@ defmodule Bamboo.MandrillAdapterTest do
def start_server(parent) do
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
Agent.update(__MODULE__, &HashDict.put(&1, :parent, parent))
Application.put_env(:bamboo, :mandrill_base_uri, "http://localhost:4001")
Plug.Adapters.Cowboy.http __MODULE__, [], port: 4001, ref: __MODULE__
port = get_free_port
Application.put_env(:bamboo, :mandrill_base_uri, "http://localhost:#{port}")
Plug.Adapters.Cowboy.http __MODULE__, [], port: port, ref: __MODULE__
end

defp get_free_port do
{:ok, socket} = :ranch_tcp.listen(port: 0)
{:ok, port} = :inet.port(socket)
:erlang.port_close(socket)
port
end

def shutdown do
Expand Down
12 changes: 10 additions & 2 deletions test/lib/bamboo/adapters/sendgrid_adapter_test.exs
Expand Up @@ -19,8 +19,16 @@ defmodule Bamboo.SendgridAdapterTest do
def start_server(parent) do
Agent.start_link(fn -> HashDict.new end, name: __MODULE__)
Agent.update(__MODULE__, &HashDict.put(&1, :parent, parent))
Application.put_env(:bamboo, :sendgrid_base_uri, "http://localhost:4002")
Plug.Adapters.Cowboy.http __MODULE__, [], port: 4002, ref: __MODULE__
port = get_free_port
Application.put_env(:bamboo, :sendgrid_base_uri, "http://localhost:#{port}")
Plug.Adapters.Cowboy.http __MODULE__, [], port: port, ref: __MODULE__
end

defp get_free_port do
{:ok, socket} = :ranch_tcp.listen(port: 0)
{:ok, port} = :inet.port(socket)
:erlang.port_close(socket)
port
end

def shutdown do
Expand Down

0 comments on commit 1de0bc4

Please sign in to comment.