1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Gun.ConnectionPoolTest do
9 import ExUnit.CaptureLog
11 alias Pleroma.Gun.ConnectionPool
15 |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(100) end) end)
16 |> stub(:await_up, fn _, _ -> {:ok, :http} end)
17 |> stub(:set_owner, fn _, _ -> :ok end)
22 setup :set_mox_from_context
25 test "gives the same connection to 2 concurrent requests" do
28 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200530163914.pdf",
29 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200528183427.pdf"
36 {:ok, conn} = ConnectionPool.get_conn(uri, [])
37 ConnectionPool.release_conn(conn)
38 send(task_parent, conn)
51 test "connection limit is respected with concurrent requests" do
52 clear_config([:connections_pool, :max_connections]) do
53 Config.put([:connections_pool, :max_connections], 1)
54 # The supervisor needs a reboot to apply the new config setting
55 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
58 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
65 "https://ninenines.eu/",
66 "https://youtu.be/PFGwMiDJKNY"
73 result = ConnectionPool.get_conn(uri, [])
74 # Sleep so that we don't end up with a situation,
75 # where request from the second process gets processed
76 # only after the first process already released the connection
81 ConnectionPool.release_conn(pid)
87 send(task_parent, result)
92 [{:error, :pool_full}, {:ok, _pid}] =