logo

pleroma

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

fed_registry_test.exs (3558B)


  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.Web.FedSockets.FedRegistryTest do
  5. use ExUnit.Case
  6. alias Pleroma.Web.FedSockets
  7. alias Pleroma.Web.FedSockets.FedRegistry
  8. alias Pleroma.Web.FedSockets.SocketInfo
  9. @good_domain "http://good.domain"
  10. @good_domain_origin "good.domain:80"
  11. setup do
  12. start_supervised({Pleroma.Web.FedSockets.Supervisor, []})
  13. build_test_socket(@good_domain)
  14. Process.sleep(10)
  15. :ok
  16. end
  17. describe "add_fed_socket/1 without conflicting sockets" do
  18. test "can be added" do
  19. Process.sleep(10)
  20. assert {:ok, %SocketInfo{origin: origin}} = FedRegistry.get_fed_socket(@good_domain_origin)
  21. assert origin == "good.domain:80"
  22. end
  23. test "multiple origins can be added" do
  24. build_test_socket("http://anothergood.domain")
  25. Process.sleep(10)
  26. assert {:ok, %SocketInfo{origin: origin_1}} =
  27. FedRegistry.get_fed_socket(@good_domain_origin)
  28. assert {:ok, %SocketInfo{origin: origin_2}} =
  29. FedRegistry.get_fed_socket("anothergood.domain:80")
  30. assert origin_1 == "good.domain:80"
  31. assert origin_2 == "anothergood.domain:80"
  32. assert FedRegistry.list_all() |> Enum.count() == 2
  33. end
  34. end
  35. describe "add_fed_socket/1 when duplicate sockets conflict" do
  36. setup do
  37. build_test_socket(@good_domain)
  38. build_test_socket(@good_domain)
  39. Process.sleep(10)
  40. :ok
  41. end
  42. test "will be ignored" do
  43. assert {:ok, %SocketInfo{origin: origin, pid: pid_one}} =
  44. FedRegistry.get_fed_socket(@good_domain_origin)
  45. assert origin == "good.domain:80"
  46. assert FedRegistry.list_all() |> Enum.count() == 1
  47. end
  48. test "the newer process will be closed" do
  49. pid_two = build_test_socket(@good_domain)
  50. assert {:ok, %SocketInfo{origin: origin, pid: pid_one}} =
  51. FedRegistry.get_fed_socket(@good_domain_origin)
  52. assert origin == "good.domain:80"
  53. Process.sleep(10)
  54. refute Process.alive?(pid_two)
  55. assert FedRegistry.list_all() |> Enum.count() == 1
  56. end
  57. end
  58. describe "get_fed_socket/1" do
  59. test "returns missing for unknown hosts" do
  60. assert {:error, :missing} = FedRegistry.get_fed_socket("not_a_dmoain")
  61. end
  62. test "returns rejected for hosts previously rejected" do
  63. "rejected.domain:80"
  64. |> FedSockets.uri_for_origin()
  65. |> FedRegistry.set_host_rejected()
  66. assert {:error, :rejected} = FedRegistry.get_fed_socket("rejected.domain:80")
  67. end
  68. test "can retrieve a previously added SocketInfo" do
  69. build_test_socket(@good_domain)
  70. Process.sleep(10)
  71. assert {:ok, %SocketInfo{origin: origin}} = FedRegistry.get_fed_socket(@good_domain_origin)
  72. assert origin == "good.domain:80"
  73. end
  74. test "removes references to SocketInfos when the process crashes" do
  75. assert {:ok, %SocketInfo{origin: origin, pid: pid}} =
  76. FedRegistry.get_fed_socket(@good_domain_origin)
  77. assert origin == "good.domain:80"
  78. Process.exit(pid, :testing)
  79. Process.sleep(100)
  80. assert {:error, :missing} = FedRegistry.get_fed_socket(@good_domain_origin)
  81. end
  82. end
  83. def build_test_socket(uri) do
  84. Kernel.spawn(fn -> fed_socket_almost(uri) end)
  85. end
  86. def fed_socket_almost(origin) do
  87. FedRegistry.add_fed_socket(origin)
  88. receive do
  89. :close ->
  90. :ok
  91. after
  92. 5_000 -> :timeout
  93. end
  94. end
  95. end