1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Gun.ConnectionPoolTest do
9 import ExUnit.CaptureLog
10 alias Pleroma.Gun.ConnectionPool
14 |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(100) end) end)
15 |> stub(:await_up, fn _, _ -> {:ok, :http} end)
16 |> stub(:set_owner, fn _, _ -> :ok end)
23 test "gives the same connection to 2 concurrent requests" do
26 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200530163914.pdf",
27 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200528183427.pdf"
34 {:ok, conn} = ConnectionPool.get_conn(uri, [])
35 ConnectionPool.release_conn(conn)
36 send(task_parent, conn)
49 test "connection limit is respected with concurrent requests" do
50 clear_config([:connections_pool, :max_connections]) do
51 clear_config([:connections_pool, :max_connections], 1)
52 # The supervisor needs a reboot to apply the new config setting
53 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
56 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
63 "https://ninenines.eu/",
64 "https://youtu.be/PFGwMiDJKNY"
71 result = ConnectionPool.get_conn(uri, [])
72 # Sleep so that we don't end up with a situation,
73 # where request from the second process gets processed
74 # only after the first process already released the connection
79 ConnectionPool.release_conn(pid)
85 send(task_parent, result)
90 [{:error, :pool_full}, {:ok, _pid}] =