summaryrefslogtreecommitdiff
path: root/test/pleroma/gun/connection_pool_test.exs
blob: e0c9e99040652878c678686f352ab1f7499a6175 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Gun.ConnectionPoolTest do
  use Pleroma.DataCase

  import Mox
  import ExUnit.CaptureLog
  alias Pleroma.Gun.ConnectionPool

  defp gun_mock(_) do
    Pleroma.GunMock
    |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(100) end) end)
    |> stub(:await_up, fn _, _ -> {:ok, :http} end)
    |> stub(:set_owner, fn _, _ -> :ok end)

    :ok
  end

  setup :gun_mock

  test "gives the same connection to 2 concurrent requests" do
    Enum.map(
      [
        "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200530163914.pdf",
        "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200528183427.pdf"
      ],
      fn uri ->
        uri = URI.parse(uri)
        task_parent = self()

        Task.start_link(fn ->
          {:ok, conn} = ConnectionPool.get_conn(uri, [])
          ConnectionPool.release_conn(conn)
          send(task_parent, conn)
        end)
      end
    )

    [pid, pid] =
      for _ <- 1..2 do
        receive do
          pid -> pid
        end
      end
  end

  @tag :erratic
  test "connection limit is respected with concurrent requests" do
    clear_config([:connections_pool, :max_connections]) do
      clear_config([:connections_pool, :max_connections], 1)
      # The supervisor needs a reboot to apply the new config setting
      Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)

      on_exit(fn ->
        Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
      end)
    end

    capture_log(fn ->
      Enum.map(
        [
          "https://ninenines.eu/",
          "https://youtu.be/PFGwMiDJKNY"
        ],
        fn uri ->
          uri = URI.parse(uri)
          task_parent = self()

          Task.start_link(fn ->
            result = ConnectionPool.get_conn(uri, [])
            # Sleep so that we don't end up with a situation,
            # where request from the second process gets processed
            # only after the first process already released the connection
            Process.sleep(50)

            case result do
              {:ok, pid} ->
                ConnectionPool.release_conn(pid)

              _ ->
                nil
            end

            send(task_parent, result)
          end)
        end
      )

      [{:error, :pool_full}, {:ok, _pid}] =
        for _ <- 1..2 do
          receive do
            result -> result
          end
        end
        |> Enum.sort()
    end)
  end
end