diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/http/gun_test.exs | 8 | ||||
-rw-r--r-- | test/pool/connections_test.exs | 67 |
2 files changed, 73 insertions, 2 deletions
diff --git a/test/http/gun_test.exs b/test/http/gun_test.exs index c64cf7125..1a5a5b60c 100644 --- a/test/http/gun_test.exs +++ b/test/http/gun_test.exs @@ -9,6 +9,14 @@ defmodule Pleroma.HTTP.GunTest do alias Pleroma.HTTP.Gun describe "options/1" do + test "http" do + uri = URI.parse("http://example.com") + + opts = Gun.options([reuse_conn: false], uri) + + assert opts[:reuse_conn] == false + end + test "https url with default port" do uri = URI.parse("https://example.com") diff --git a/test/pool/connections_test.exs b/test/pool/connections_test.exs index d8436997f..8490f6bf1 100644 --- a/test/pool/connections_test.exs +++ b/test/pool/connections_test.exs @@ -26,7 +26,7 @@ defmodule Pleroma.Pool.ConnectionsTest do setup do name = :test_connections - {:ok, pid} = Connections.start_link({name, []}) + {:ok, pid} = Connections.start_link(name) on_exit(fn -> if Process.alive?(pid), do: GenServer.stop(name) @@ -97,6 +97,66 @@ defmodule Pleroma.Pool.ConnectionsTest do end end + describe "pool overflow" do + setup do: clear_config([:connections_pool, :max_connections], 2) + + test "when all conns are active return nil", %{name: name} do + open_mock(2) + conn1 = Connections.checkin("https://example1.com", name) + conn2 = Connections.checkin("https://example2.com", name) + refute Connections.checkin("https://example3.com", name) + + self = self() + + assert match?( + %Connections{ + conns: %{ + "https:example1.com:443" => %Conn{ + conn: ^conn1, + used_by: [{^self, _}] + }, + "https:example2.com:443" => %Conn{ + conn: ^conn2, + used_by: [{^self, _}] + } + } + }, + Connections.get_state(name) + ) + + assert Connections.count(name) == 2 + end + + test "close idle conn", %{name: name} do + open_mock(3) + |> expect(:close, fn _ -> :ok end) + + self = self() + conn1 = Connections.checkin("https://example1.com", name) + Connections.checkout(conn1, self, name) + conn2 = Connections.checkin("https://example2.com", name) + conn3 = Connections.checkin("https://example3.com", name) + + assert match?( + %Connections{ + conns: %{ + "https:example2.com:443" => %Conn{ + conn: ^conn2, + used_by: [{^self, _}] + }, + "https:example3.com:443" => %Conn{ + conn: ^conn3, + used_by: [{^self, _}] + } + } + }, + Connections.get_state(name) + ) + + assert Connections.count(name) == 2 + end + end + test "opens connection and reuse it on next request", %{name: name} do open_mock() url = "http://some-domain.com" @@ -376,6 +436,9 @@ defmodule Pleroma.Pool.ConnectionsTest do refute Connections.checkin(url, name) end) =~ "Opening connection to http://gun-not-up.com failed with error {:error, :timeout}" + + state = Connections.get_state(name) + assert state.conns == %{} end test "process gun_down message and then gun_up", %{name: name} do @@ -784,7 +847,7 @@ defmodule Pleroma.Pool.ConnectionsTest do test "count/1" do name = :test_count - {:ok, _} = Connections.start_link({name, []}) + {:ok, _} = Connections.start_link(name) assert Connections.count(name) == 0 Connections.add_conn(name, "1", %Conn{conn: self()}) assert Connections.count(name) == 1 |