From 2a2d11f2b305f415fb7eec7a799fd4c17d6e3446 Mon Sep 17 00:00:00 2001 From: Alex S Date: Tue, 20 Aug 2019 10:51:56 +0300 Subject: [PATCH] some fixes and tests --- lib/pleroma/application.ex | 2 +- lib/pleroma/http/http.ex | 8 +++-- test/gun/connections_test.exs | 54 +++++++++++++++++++++++++++++- test/http_test.exs | 24 +++++++++++++ test/reverse_proxy/client/hackney_test.exs | 4 +++ test/reverse_proxy/client/tesla_test.exs | 4 +++ test/reverse_proxy/reverse_proxy_test.exs | 9 ----- test/support/reverse_proxy_client_case.ex | 2 +- 8 files changed, 93 insertions(+), 14 deletions(-) diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex index 06d1a187e..347f520bb 100644 --- a/lib/pleroma/application.ex +++ b/lib/pleroma/application.ex @@ -165,7 +165,7 @@ defmodule Pleroma.Application do end defp gun_pools do - if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun do + if Application.get_env(:tesla, :adapter) == Tesla.Adapter.Gun || Mix.env() == :test do for {pool_name, opts} <- Pleroma.Config.get([:gun_pools]) do %{ id: :"gun_pool_#{pool_name}", diff --git a/lib/pleroma/http/http.ex b/lib/pleroma/http/http.ex index ab0fd55de..b18ce2803 100644 --- a/lib/pleroma/http/http.ex +++ b/lib/pleroma/http/http.ex @@ -89,11 +89,15 @@ defmodule Pleroma.HTTP do case uri.scheme do "https" -> + adapter_opts = Keyword.get(options, :adapter, []) + tls_opts = - Keyword.get(options, :tls_opts, []) + Keyword.get(adapter_opts, :tls_opts, []) |> Keyword.put(:server_name_indication, host) - Keyword.put(options, :tls_opts, tls_opts) ++ [ssl: [server_name_indication: host]] + adapter_opts = Keyword.put(adapter_opts, :tls_opts, tls_opts) + + Keyword.put(options, :adapter, adapter_opts) ++ [ssl: [server_name_indication: host]] _ -> options diff --git a/test/gun/connections_test.exs b/test/gun/connections_test.exs index 4da42d854..1e41e771b 100644 --- a/test/gun/connections_test.exs +++ b/test/gun/connections_test.exs @@ -4,7 +4,9 @@ defmodule Gun.ConnectionsTest do use ExUnit.Case - alias Pleroma.Gun.{Connections, Conn, API} + alias Pleroma.Gun.API + alias Pleroma.Gun.Conn + alias Pleroma.Gun.Connections setup_all do {:ok, _} = Registry.start_link(keys: :unique, name: API.Mock) @@ -262,5 +264,55 @@ defmodule Gun.ConnectionsTest do } } = Connections.get_state(name) end + + test "remove frequently used", %{name: name, pid: pid} do + api = Pleroma.Config.get([API]) + Pleroma.Config.put([API], API.Gun) + on_exit(fn -> Pleroma.Config.put([API], api) end) + + Connections.get_conn("https://www.google.com", [genserver_pid: pid], name) + + for _ <- 1..4 do + Connections.get_conn("https://httpbin.org", [genserver_pid: pid], name) + end + + %Connections{ + conns: %{ + "https:httpbin.org:443" => %Conn{ + conn: _, + state: :up, + waiting_pids: [], + used: 4 + }, + "https:www.google.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://httpbin.org", [genserver_pid: pid], name) + + %Connections{ + conns: %{ + "http:httpbin.org:80" => %Conn{ + conn: ^conn, + state: :up, + waiting_pids: [], + used: 1 + }, + "https:httpbin.org:443" => %Conn{ + conn: _, + state: :up, + waiting_pids: [], + used: 4 + } + }, + opts: [max_connections: 2, timeout: 10] + } = Connections.get_state(name) + end end end diff --git a/test/http_test.exs b/test/http_test.exs index 5f9522cf0..5ffd351e4 100644 --- a/test/http_test.exs +++ b/test/http_test.exs @@ -56,4 +56,28 @@ defmodule Pleroma.HTTPTest do } end end + + @tag :integration + test "get_conn_for_gun/3" do + adapter = Application.get_env(:tesla, :adapter) + Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun) + api = Pleroma.Config.get([Pleroma.Gun.API]) + Pleroma.Config.put([Pleroma.Gun.API], Pleroma.Gun.API.Gun) + + on_exit(fn -> + Application.put_env(:tesla, :adapter, adapter) + Pleroma.Config.put([Pleroma.Gun.API], api) + end) + + options = [adapter: [pool: :federation]] + + assert {:ok, resp} = + Pleroma.HTTP.request(:get, "https://httpbin.org/user-agent", "", [], options) + + adapter_opts = resp.opts[:adapter] + + assert adapter_opts[:original] == "httpbin.org:443" + refute adapter_opts[:close_conn] + assert adapter_opts[:pool] == :federation + end end diff --git a/test/reverse_proxy/client/hackney_test.exs b/test/reverse_proxy/client/hackney_test.exs index cf9de912a..3c552dc83 100644 --- a/test/reverse_proxy/client/hackney_test.exs +++ b/test/reverse_proxy/client/hackney_test.exs @@ -8,4 +8,8 @@ defmodule Pleroma.ReverseProxy.Client.HackneyTest do defp check_ref(ref) do assert is_reference(ref) end + + defp close(ref) do + Pleroma.ReverseProxy.Client.Hackney.close(ref) + end end diff --git a/test/reverse_proxy/client/tesla_test.exs b/test/reverse_proxy/client/tesla_test.exs index a18d609d2..8adefbe59 100644 --- a/test/reverse_proxy/client/tesla_test.exs +++ b/test/reverse_proxy/client/tesla_test.exs @@ -10,4 +10,8 @@ defmodule Pleroma.ReverseProxy.Client.TeslaTest do assert is_reference(stream) assert ref[:fin] end + + defp close(%{pid: pid}) do + Pleroma.ReverseProxy.Client.Tesla.close(pid) + end end diff --git a/test/reverse_proxy/reverse_proxy_test.exs b/test/reverse_proxy/reverse_proxy_test.exs index 8235fcc86..1d3e554b3 100644 --- a/test/reverse_proxy/reverse_proxy_test.exs +++ b/test/reverse_proxy/reverse_proxy_test.exs @@ -357,9 +357,6 @@ defmodule Pleroma.ReverseProxyTest do api = Pleroma.Config.get([Pleroma.Gun.API]) Pleroma.Config.put([Pleroma.Gun.API], Pleroma.Gun.API.Gun) - {:ok, _} = - Pleroma.Gun.Connections.start_link({:media, [max_connections: 5, timeout: 5_000]}) - conn = ReverseProxy.call(conn, "http://httpbin.org/stream-bytes/10") assert byte_size(conn.resp_body) == 10 @@ -382,9 +379,6 @@ defmodule Pleroma.ReverseProxyTest do api = Pleroma.Config.get([Pleroma.Gun.API]) Pleroma.Config.put([Pleroma.Gun.API], Pleroma.Gun.API.Gun) - {:ok, _} = - Pleroma.Gun.Connections.start_link({:media, [max_connections: 5, timeout: 5_000]}) - conn = ReverseProxy.call(conn, "https://httpbin.org/stream-bytes/10") assert byte_size(conn.resp_body) == 10 @@ -407,9 +401,6 @@ defmodule Pleroma.ReverseProxyTest do api = Pleroma.Config.get([Pleroma.Gun.API]) Pleroma.Config.put([Pleroma.Gun.API], Pleroma.Gun.API.Gun) - {:ok, _} = - Pleroma.Gun.Connections.start_link({:media, [max_connections: 5, timeout: 5_000]}) - conn = ReverseProxy.call(conn, "https://httpbin.org/redirect/5") assert conn.state == :chunked diff --git a/test/support/reverse_proxy_client_case.ex b/test/support/reverse_proxy_client_case.ex index 16bc2803b..36df1ed95 100644 --- a/test/support/reverse_proxy_client_case.ex +++ b/test/support/reverse_proxy_client_case.ex @@ -55,7 +55,7 @@ defmodule Pleroma.ReverseProxyClientCase do assert headers != [] check_ref(ref) - assert :ok = @client.close(ref) + assert :ok == close(ref) {:ok, status, headers} -> assert headers != []