[#3213] ActivityPub: implemented subqueries-based hashtags filtering, removed aggrega...
[akkoma] / test / pleroma / gun / connection_pool_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Gun.ConnectionPoolTest do
6 use Pleroma.DataCase
7
8 import Mox
9 import ExUnit.CaptureLog
10 alias Pleroma.Config
11 alias Pleroma.Gun.ConnectionPool
12
13 defp gun_mock(_) do
14 Pleroma.GunMock
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)
18
19 :ok
20 end
21
22 setup :gun_mock
23
24 test "gives the same connection to 2 concurrent requests" do
25 Enum.map(
26 [
27 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200530163914.pdf",
28 "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200528183427.pdf"
29 ],
30 fn uri ->
31 uri = URI.parse(uri)
32 task_parent = self()
33
34 Task.start_link(fn ->
35 {:ok, conn} = ConnectionPool.get_conn(uri, [])
36 ConnectionPool.release_conn(conn)
37 send(task_parent, conn)
38 end)
39 end
40 )
41
42 [pid, pid] =
43 for _ <- 1..2 do
44 receive do
45 pid -> pid
46 end
47 end
48 end
49
50 test "connection limit is respected with concurrent requests" do
51 clear_config([:connections_pool, :max_connections]) do
52 Config.put([: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)
55
56 on_exit(fn ->
57 Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
58 end)
59 end
60
61 capture_log(fn ->
62 Enum.map(
63 [
64 "https://ninenines.eu/",
65 "https://youtu.be/PFGwMiDJKNY"
66 ],
67 fn uri ->
68 uri = URI.parse(uri)
69 task_parent = self()
70
71 Task.start_link(fn ->
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
76 Process.sleep(50)
77
78 case result do
79 {:ok, pid} ->
80 ConnectionPool.release_conn(pid)
81
82 _ ->
83 nil
84 end
85
86 send(task_parent, result)
87 end)
88 end
89 )
90
91 [{:error, :pool_full}, {:ok, _pid}] =
92 for _ <- 1..2 do
93 receive do
94 result -> result
95 end
96 end
97 |> Enum.sort()
98 end)
99 end
100 end