some fixes and tests

This commit is contained in:
Alex S 2019-08-20 10:51:56 +03:00
parent af12d08da7
commit 2a2d11f2b3
8 changed files with 93 additions and 14 deletions

View File

@ -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}",

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 != []