1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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)
50 test "connection limit is respected with concurrent requests" do
51 clear_config([:connections_pool, :max_connections]) do
52 clear_config([:connections_pool, :max_connections], 1)
53 # The supervisor needs a reboot to apply the new config setting
54 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
57 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
64 "https://ninenines.eu/",
65 "https://youtu.be/PFGwMiDJKNY"
72 result = ConnectionPool.get_conn(uri, [])
73 # Sleep so that we don't end up with a situation,
74 # where request from the second process gets processed
75 # only after the first process already released the connection
80 ConnectionPool.release_conn(pid)
86 send(task_parent, result)
91 [{:error, :pool_full}, {:ok, _pid}] =