Skip to content
Open
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
6 changes: 5 additions & 1 deletion lib/clients/fake_rabbitmq.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,18 @@ defmodule ExRabbitPool.FakeRabbitMQ do
end

@impl true
def open_connection(config) do
def open_connection(config) when is_list(config) do
if Keyword.get(config, :queue) == "error.queue" do
{:error, :invalid}
else
{:ok, %Connection{pid: self()}}
end
end

def open_connection(_config) do
{:ok, %Connection{pid: self()}}
end

@impl true
def open_channel(conn) do
{:ok, %Channel{conn: conn, pid: self()}}
Expand Down
9 changes: 7 additions & 2 deletions lib/worker/rabbit_connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ defmodule ExRabbitPool.Worker.RabbitConnection do
# split our opts from the ones passed to the amqp client
{opts, amqp_config} = Keyword.split(config, [:adapter])
adapter = Keyword.get(opts, :adapter, ExRabbitPool.RabbitMQ)
uri = amqp_config[:uri]

send(self(), :connect)
{:ok, %State{adapter: adapter, config: amqp_config}}
{:ok, %State{adapter: adapter, config: uri || amqp_config}}
end

@impl true
Expand Down Expand Up @@ -188,7 +190,10 @@ defmodule ExRabbitPool.Worker.RabbitConnection do
# defaults to `1_000`
true = Process.link(pid)

num_channels = Keyword.get(config, :channels, @default_channels)
num_channels =
if Keyword.keyword?(config),
do: Keyword.get(config, :channels, @default_channels),
else: @default_channels

channels =
do_times(num_channels, 0, fn ->
Expand Down
8 changes: 8 additions & 0 deletions test/worker/rabbit_connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ defmodule ExRabbitPool.Worker.RabbitConnectionTest do
assert length(channels) == 5
end

test "creates a connection using uri", %{config: config} do
config = Keyword.put(config, :uri, "amqp://guest:guest@localhost:5672/")
pid = start_supervised!({ConnWorker, config})
%{channels: channels, connection: connection} = ConnWorker.state(pid)
refute is_nil(connection)
assert length(channels) == 10
end

test "creates a pool of channels by default", %{config: config} do
pid = start_supervised!({ConnWorker, Keyword.delete(config, :channels)})
%{channels: channels} = ConnWorker.state(pid)
Expand Down