diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/gun/connections_test.exs | 94 |
1 files changed, 76 insertions, 18 deletions
diff --git a/test/gun/connections_test.exs b/test/gun/connections_test.exs index a63c8eaf9..8308b5f9f 100644 --- a/test/gun/connections_test.exs +++ b/test/gun/connections_test.exs @@ -6,12 +6,17 @@ defmodule Gun.ConnectionsTest do use ExUnit.Case alias Pleroma.Gun.{Connections, Conn, API} + setup_all do + {:ok, _} = Registry.start_link(keys: :unique, name: API.Mock) + :ok + end + setup do name = :test_gun_connections adapter = Application.get_env(:tesla, :adapter) Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun) on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end) - {:ok, pid} = Connections.start_link(name) + {:ok, pid} = Connections.start_link({name, [max_connections: 2, timeout: 10]}) {:ok, name: name, pid: pid} end @@ -37,10 +42,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "some-domain.com:80" => %Conn{ + "http:some-domain.com:80" => %Conn{ conn: ^conn, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 2 } } } = Connections.get_state(name) @@ -58,10 +64,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "some-domain.com:80" => %Conn{ + "http:some-domain.com:80" => %Conn{ conn: ^conn, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 2 } } } = Connections.get_state(name) @@ -84,12 +91,12 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "some-domain.com:80" => %Conn{ + "http:some-domain.com:80" => %Conn{ conn: ^conn, state: :up, waiting_pids: [] }, - "some-domain.com:443" => %Conn{ + "https:some-domain.com:443" => %Conn{ conn: ^https_conn, state: :up, waiting_pids: [] @@ -105,7 +112,7 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "gun_down.com:80" => %Conn{ + "http:gun_down.com:80" => %Conn{ conn: _, state: :down, waiting_pids: _ @@ -121,10 +128,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "gun_down_and_up.com:80" => %Conn{ + "http:gun_down_and_up.com:80" => %Conn{ conn: _, state: :down, - waiting_pids: _ + waiting_pids: _, + used: 0 } } } = Connections.get_state(name) @@ -136,10 +144,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "gun_down_and_up.com:80" => %Conn{ + "http:gun_down_and_up.com:80" => %Conn{ conn: _, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 2 } } } = Connections.get_state(name) @@ -164,10 +173,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "some-domain.com:80" => %Conn{ + "http:some-domain.com:80" => %Conn{ conn: conn, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 5 } } } = Connections.get_state(name) @@ -175,6 +185,52 @@ defmodule Gun.ConnectionsTest do assert Enum.all?(conns, fn res -> res == conn end) end + test "remove frequently used", %{name: name, pid: pid} do + Connections.get_conn("https://some-domain.com", [genserver_pid: pid], name) + + for _ <- 1..4 do + Connections.get_conn("http://some-domain.com", [genserver_pid: pid], name) + end + + %Connections{ + conns: %{ + "http:some-domain.com:80" => %Conn{ + conn: _, + state: :up, + waiting_pids: [], + used: 4 + }, + "https:some-domain.com:443" => %Conn{ + conn: _, + state: :up, + waiting_pids: [], + used: 1 + } + }, + opts: [max_connections: 2, timeout: 10] + } = Connections.get_state(name) + + conn = Connections.get_conn("http://another-domain.com", [genserver_pid: pid], name) + + %Connections{ + conns: %{ + "http:another-domain.com:80" => %Conn{ + conn: ^conn, + state: :up, + waiting_pids: [], + used: 1 + }, + "http:some-domain.com:80" => %Conn{ + conn: _, + state: :up, + waiting_pids: [], + used: 4 + } + }, + opts: [max_connections: 2, timeout: 10] + } = Connections.get_state(name) + end + describe "integration test" do @describetag :integration @@ -193,10 +249,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "httpbin.org:80" => %Conn{ + "http:httpbin.org:80" => %Conn{ conn: ^conn, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 2 } } } = Connections.get_state(name) @@ -217,10 +274,11 @@ defmodule Gun.ConnectionsTest do %Connections{ conns: %{ - "httpbin.org:443" => %Conn{ + "https:httpbin.org:443" => %Conn{ conn: ^conn, state: :up, - waiting_pids: [] + waiting_pids: [], + used: 2 } } } = Connections.get_state(name) |