logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

conneciton_pool_test.exs (2702B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Gun.ConnectionPoolTest do
  5. use Pleroma.DataCase
  6. import Mox
  7. import ExUnit.CaptureLog
  8. alias Pleroma.Config
  9. alias Pleroma.Gun.ConnectionPool
  10. defp gun_mock(_) do
  11. Pleroma.GunMock
  12. |> stub(:open, fn _, _, _ -> Task.start_link(fn -> Process.sleep(100) end) end)
  13. |> stub(:await_up, fn _, _ -> {:ok, :http} end)
  14. |> stub(:set_owner, fn _, _ -> :ok end)
  15. :ok
  16. end
  17. setup :set_mox_from_context
  18. setup :gun_mock
  19. test "gives the same connection to 2 concurrent requests" do
  20. Enum.map(
  21. [
  22. "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200530163914.pdf",
  23. "http://www.korean-books.com.kp/KBMbooks/en/periodic/pictorial/20200528183427.pdf"
  24. ],
  25. fn uri ->
  26. uri = URI.parse(uri)
  27. task_parent = self()
  28. Task.start_link(fn ->
  29. {:ok, conn} = ConnectionPool.get_conn(uri, [])
  30. ConnectionPool.release_conn(conn)
  31. send(task_parent, conn)
  32. end)
  33. end
  34. )
  35. [pid, pid] =
  36. for _ <- 1..2 do
  37. receive do
  38. pid -> pid
  39. end
  40. end
  41. end
  42. test "connection limit is respected with concurrent requests" do
  43. clear_config([:connections_pool, :max_connections]) do
  44. Config.put([:connections_pool, :max_connections], 1)
  45. # The supervisor needs a reboot to apply the new config setting
  46. Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
  47. on_exit(fn ->
  48. Process.exit(Process.whereis(Pleroma.Gun.ConnectionPool.WorkerSupervisor), :kill)
  49. end)
  50. end
  51. capture_log(fn ->
  52. Enum.map(
  53. [
  54. "https://ninenines.eu/",
  55. "https://youtu.be/PFGwMiDJKNY"
  56. ],
  57. fn uri ->
  58. uri = URI.parse(uri)
  59. task_parent = self()
  60. Task.start_link(fn ->
  61. result = ConnectionPool.get_conn(uri, [])
  62. # Sleep so that we don't end up with a situation,
  63. # where request from the second process gets processed
  64. # only after the first process already released the connection
  65. Process.sleep(50)
  66. case result do
  67. {:ok, pid} ->
  68. ConnectionPool.release_conn(pid)
  69. _ ->
  70. nil
  71. end
  72. send(task_parent, result)
  73. end)
  74. end
  75. )
  76. [{:error, :pool_full}, {:ok, _pid}] =
  77. for _ <- 1..2 do
  78. receive do
  79. result -> result
  80. end
  81. end
  82. |> Enum.sort()
  83. end)
  84. end
  85. end