753fd8b0baa0c95993e23979ba5fdb3627184f2c
[akkoma] / test / pool / connections_test.exs
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
5 defmodule Pleroma.Pool.ConnectionsTest do
6 use ExUnit.Case
7 use Pleroma.Tests.Helpers
8 import ExUnit.CaptureLog
9 alias Pleroma.Gun.Conn
10 alias Pleroma.Pool.Connections
11
12 setup_all do
13 {:ok, _} = Registry.start_link(keys: :unique, name: Pleroma.GunMock)
14 :ok
15 end
16
17 clear_config([:connections_pool, :retry]) do
18 Pleroma.Config.put([:connections_pool, :retry], 5)
19 end
20
21 setup do
22 name = :test_connections
23 adapter = Application.get_env(:tesla, :adapter)
24 Application.put_env(:tesla, :adapter, Tesla.Adapter.Gun)
25
26 {:ok, pid} = Connections.start_link({name, [max_connections: 2, checkin_timeout: 1_500]})
27
28 on_exit(fn ->
29 Application.put_env(:tesla, :adapter, adapter)
30
31 if Process.alive?(pid) do
32 GenServer.stop(name)
33 end
34 end)
35
36 {:ok, name: name}
37 end
38
39 describe "alive?/2" do
40 test "is alive", %{name: name} do
41 assert Connections.alive?(name)
42 end
43
44 test "returns false if not started" do
45 refute Connections.alive?(:some_random_name)
46 end
47 end
48
49 test "opens connection and reuse it on next request", %{name: name} do
50 url = "http://some-domain.com"
51 key = "http:some-domain.com:80"
52 refute Connections.checkin(url, name)
53 :ok = Conn.open(url, name)
54
55 conn = Connections.checkin(url, name)
56 assert is_pid(conn)
57 assert Process.alive?(conn)
58
59 self = self()
60
61 %Connections{
62 conns: %{
63 ^key => %Conn{
64 conn: ^conn,
65 gun_state: :up,
66 used_by: [{^self, _}],
67 conn_state: :active
68 }
69 }
70 } = Connections.get_state(name)
71
72 reused_conn = Connections.checkin(url, name)
73
74 assert conn == reused_conn
75
76 %Connections{
77 conns: %{
78 ^key => %Conn{
79 conn: ^conn,
80 gun_state: :up,
81 used_by: [{^self, _}, {^self, _}],
82 conn_state: :active
83 }
84 }
85 } = Connections.get_state(name)
86
87 :ok = Connections.checkout(conn, self, name)
88
89 %Connections{
90 conns: %{
91 ^key => %Conn{
92 conn: ^conn,
93 gun_state: :up,
94 used_by: [{^self, _}],
95 conn_state: :active
96 }
97 }
98 } = Connections.get_state(name)
99
100 :ok = Connections.checkout(conn, self, name)
101
102 %Connections{
103 conns: %{
104 ^key => %Conn{
105 conn: ^conn,
106 gun_state: :up,
107 used_by: [],
108 conn_state: :idle
109 }
110 }
111 } = Connections.get_state(name)
112 end
113
114 test "reuse connection for idna domains", %{name: name} do
115 url = "http://ですsome-domain.com"
116 refute Connections.checkin(url, name)
117
118 :ok = Conn.open(url, name)
119
120 conn = Connections.checkin(url, name)
121 assert is_pid(conn)
122 assert Process.alive?(conn)
123
124 self = self()
125
126 %Connections{
127 conns: %{
128 "http:ですsome-domain.com:80" => %Conn{
129 conn: ^conn,
130 gun_state: :up,
131 used_by: [{^self, _}],
132 conn_state: :active
133 }
134 }
135 } = Connections.get_state(name)
136
137 reused_conn = Connections.checkin(url, name)
138
139 assert conn == reused_conn
140 end
141
142 test "reuse for ipv4", %{name: name} do
143 url = "http://127.0.0.1"
144
145 refute Connections.checkin(url, name)
146
147 :ok = Conn.open(url, name)
148
149 conn = Connections.checkin(url, name)
150 assert is_pid(conn)
151 assert Process.alive?(conn)
152
153 self = self()
154
155 %Connections{
156 conns: %{
157 "http:127.0.0.1:80" => %Conn{
158 conn: ^conn,
159 gun_state: :up,
160 used_by: [{^self, _}],
161 conn_state: :active
162 }
163 }
164 } = Connections.get_state(name)
165
166 reused_conn = Connections.checkin(url, name)
167
168 assert conn == reused_conn
169
170 :ok = Connections.checkout(conn, self, name)
171 :ok = Connections.checkout(reused_conn, self, name)
172
173 %Connections{
174 conns: %{
175 "http:127.0.0.1:80" => %Conn{
176 conn: ^conn,
177 gun_state: :up,
178 used_by: [],
179 conn_state: :idle
180 }
181 }
182 } = Connections.get_state(name)
183 end
184
185 test "reuse for ipv6", %{name: name} do
186 url = "http://[2a03:2880:f10c:83:face:b00c:0:25de]"
187
188 refute Connections.checkin(url, name)
189
190 :ok = Conn.open(url, name)
191
192 conn = Connections.checkin(url, name)
193 assert is_pid(conn)
194 assert Process.alive?(conn)
195
196 self = self()
197
198 %Connections{
199 conns: %{
200 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Conn{
201 conn: ^conn,
202 gun_state: :up,
203 used_by: [{^self, _}],
204 conn_state: :active
205 }
206 }
207 } = Connections.get_state(name)
208
209 reused_conn = Connections.checkin(url, name)
210
211 assert conn == reused_conn
212 end
213
214 test "up and down ipv4", %{name: name} do
215 self = self()
216 url = "http://127.0.0.1"
217 :ok = Conn.open(url, name)
218 conn = Connections.checkin(url, name)
219 send(name, {:gun_down, conn, nil, nil, nil})
220 send(name, {:gun_up, conn, nil})
221
222 %Connections{
223 conns: %{
224 "http:127.0.0.1:80" => %Conn{
225 conn: ^conn,
226 gun_state: :up,
227 used_by: [{^self, _}],
228 conn_state: :active
229 }
230 }
231 } = Connections.get_state(name)
232 end
233
234 test "up and down ipv6", %{name: name} do
235 self = self()
236 url = "http://[2a03:2880:f10c:83:face:b00c:0:25de]"
237 :ok = Conn.open(url, name)
238 conn = Connections.checkin(url, name)
239 send(name, {:gun_down, conn, nil, nil, nil})
240 send(name, {:gun_up, conn, nil})
241
242 %Connections{
243 conns: %{
244 "http:2a03:2880:f10c:83:face:b00c:0:25de:80" => %Conn{
245 conn: ^conn,
246 gun_state: :up,
247 used_by: [{^self, _}],
248 conn_state: :active
249 }
250 }
251 } = Connections.get_state(name)
252 end
253
254 test "reuses connection based on protocol", %{name: name} do
255 http_url = "http://some-domain.com"
256 http_key = "http:some-domain.com:80"
257 https_url = "https://some-domain.com"
258 https_key = "https:some-domain.com:443"
259
260 refute Connections.checkin(http_url, name)
261 :ok = Conn.open(http_url, name)
262 conn = Connections.checkin(http_url, name)
263 assert is_pid(conn)
264 assert Process.alive?(conn)
265
266 refute Connections.checkin(https_url, name)
267 :ok = Conn.open(https_url, name)
268 https_conn = Connections.checkin(https_url, name)
269
270 refute conn == https_conn
271
272 reused_https = Connections.checkin(https_url, name)
273
274 refute conn == reused_https
275
276 assert reused_https == https_conn
277
278 %Connections{
279 conns: %{
280 ^http_key => %Conn{
281 conn: ^conn,
282 gun_state: :up
283 },
284 ^https_key => %Conn{
285 conn: ^https_conn,
286 gun_state: :up
287 }
288 }
289 } = Connections.get_state(name)
290 end
291
292 test "connection can't get up", %{name: name} do
293 url = "http://gun-not-up.com"
294
295 assert capture_log(fn ->
296 refute Conn.open(url, name)
297 refute Connections.checkin(url, name)
298 end) =~
299 "Received error on opening connection http://gun-not-up.com {:error, :timeout}"
300 end
301
302 test "process gun_down message and then gun_up", %{name: name} do
303 self = self()
304 url = "http://gun-down-and-up.com"
305 key = "http:gun-down-and-up.com:80"
306 :ok = Conn.open(url, name)
307 conn = Connections.checkin(url, name)
308
309 assert is_pid(conn)
310 assert Process.alive?(conn)
311
312 %Connections{
313 conns: %{
314 ^key => %Conn{
315 conn: ^conn,
316 gun_state: :up,
317 used_by: [{^self, _}]
318 }
319 }
320 } = Connections.get_state(name)
321
322 send(name, {:gun_down, conn, :http, nil, nil})
323
324 %Connections{
325 conns: %{
326 ^key => %Conn{
327 conn: ^conn,
328 gun_state: :down,
329 used_by: [{^self, _}]
330 }
331 }
332 } = Connections.get_state(name)
333
334 send(name, {:gun_up, conn, :http})
335
336 conn2 = Connections.checkin(url, name)
337 assert conn == conn2
338
339 assert is_pid(conn2)
340 assert Process.alive?(conn2)
341
342 %Connections{
343 conns: %{
344 ^key => %Conn{
345 conn: _,
346 gun_state: :up,
347 used_by: [{^self, _}, {^self, _}]
348 }
349 }
350 } = Connections.get_state(name)
351 end
352
353 test "async processes get same conn for same domain", %{name: name} do
354 url = "http://some-domain.com"
355 :ok = Conn.open(url, name)
356
357 tasks =
358 for _ <- 1..5 do
359 Task.async(fn ->
360 Connections.checkin(url, name)
361 end)
362 end
363
364 tasks_with_results = Task.yield_many(tasks)
365
366 results =
367 Enum.map(tasks_with_results, fn {task, res} ->
368 res || Task.shutdown(task, :brutal_kill)
369 end)
370
371 conns = for {:ok, value} <- results, do: value
372
373 %Connections{
374 conns: %{
375 "http:some-domain.com:80" => %Conn{
376 conn: conn,
377 gun_state: :up
378 }
379 }
380 } = Connections.get_state(name)
381
382 assert Enum.all?(conns, fn res -> res == conn end)
383 end
384
385 test "remove frequently used and idle", %{name: name} do
386 self = self()
387 http_url = "http://some-domain.com"
388 https_url = "https://some-domain.com"
389 :ok = Conn.open(https_url, name)
390 :ok = Conn.open(http_url, name)
391
392 conn1 = Connections.checkin(https_url, name)
393
394 [conn2 | _conns] =
395 for _ <- 1..4 do
396 Connections.checkin(http_url, name)
397 end
398
399 http_key = "http:some-domain.com:80"
400
401 %Connections{
402 conns: %{
403 ^http_key => %Conn{
404 conn: ^conn2,
405 gun_state: :up,
406 conn_state: :active,
407 used_by: [{^self, _}, {^self, _}, {^self, _}, {^self, _}]
408 },
409 "https:some-domain.com:443" => %Conn{
410 conn: ^conn1,
411 gun_state: :up,
412 conn_state: :active,
413 used_by: [{^self, _}]
414 }
415 }
416 } = Connections.get_state(name)
417
418 :ok = Connections.checkout(conn1, self, name)
419
420 another_url = "http://another-domain.com"
421 :ok = Conn.open(another_url, name)
422 conn = Connections.checkin(another_url, name)
423
424 %Connections{
425 conns: %{
426 "http:another-domain.com:80" => %Conn{
427 conn: ^conn,
428 gun_state: :up
429 },
430 ^http_key => %Conn{
431 conn: _,
432 gun_state: :up
433 }
434 }
435 } = Connections.get_state(name)
436 end
437
438 describe "with proxy" do
439 test "as ip", %{name: name} do
440 url = "http://proxy-string.com"
441 key = "http:proxy-string.com:80"
442 :ok = Conn.open(url, name, proxy: {{127, 0, 0, 1}, 8123})
443
444 conn = Connections.checkin(url, name)
445
446 %Connections{
447 conns: %{
448 ^key => %Conn{
449 conn: ^conn,
450 gun_state: :up
451 }
452 }
453 } = Connections.get_state(name)
454
455 reused_conn = Connections.checkin(url, name)
456
457 assert reused_conn == conn
458 end
459
460 test "as host", %{name: name} do
461 url = "http://proxy-tuple-atom.com"
462 :ok = Conn.open(url, name, proxy: {'localhost', 9050})
463 conn = Connections.checkin(url, name)
464
465 %Connections{
466 conns: %{
467 "http:proxy-tuple-atom.com:80" => %Conn{
468 conn: ^conn,
469 gun_state: :up
470 }
471 }
472 } = Connections.get_state(name)
473
474 reused_conn = Connections.checkin(url, name)
475
476 assert reused_conn == conn
477 end
478
479 test "as ip and ssl", %{name: name} do
480 url = "https://proxy-string.com"
481
482 :ok = Conn.open(url, name, proxy: {{127, 0, 0, 1}, 8123})
483 conn = Connections.checkin(url, name)
484
485 %Connections{
486 conns: %{
487 "https:proxy-string.com:443" => %Conn{
488 conn: ^conn,
489 gun_state: :up
490 }
491 }
492 } = Connections.get_state(name)
493
494 reused_conn = Connections.checkin(url, name)
495
496 assert reused_conn == conn
497 end
498
499 test "as host and ssl", %{name: name} do
500 url = "https://proxy-tuple-atom.com"
501 :ok = Conn.open(url, name, proxy: {'localhost', 9050})
502 conn = Connections.checkin(url, name)
503
504 %Connections{
505 conns: %{
506 "https:proxy-tuple-atom.com:443" => %Conn{
507 conn: ^conn,
508 gun_state: :up
509 }
510 }
511 } = Connections.get_state(name)
512
513 reused_conn = Connections.checkin(url, name)
514
515 assert reused_conn == conn
516 end
517
518 test "with socks type", %{name: name} do
519 url = "http://proxy-socks.com"
520
521 :ok = Conn.open(url, name, proxy: {:socks5, 'localhost', 1234})
522
523 conn = Connections.checkin(url, name)
524
525 %Connections{
526 conns: %{
527 "http:proxy-socks.com:80" => %Conn{
528 conn: ^conn,
529 gun_state: :up
530 }
531 }
532 } = Connections.get_state(name)
533
534 reused_conn = Connections.checkin(url, name)
535
536 assert reused_conn == conn
537 end
538
539 test "with socks4 type and ssl", %{name: name} do
540 url = "https://proxy-socks.com"
541
542 :ok = Conn.open(url, name, proxy: {:socks4, 'localhost', 1234})
543
544 conn = Connections.checkin(url, name)
545
546 %Connections{
547 conns: %{
548 "https:proxy-socks.com:443" => %Conn{
549 conn: ^conn,
550 gun_state: :up
551 }
552 }
553 } = Connections.get_state(name)
554
555 reused_conn = Connections.checkin(url, name)
556
557 assert reused_conn == conn
558 end
559 end
560
561 describe "crf/3" do
562 setup do
563 crf = Connections.crf(1, 10, 1)
564 {:ok, crf: crf}
565 end
566
567 test "more used will have crf higher", %{crf: crf} do
568 # used 3 times
569 crf1 = Connections.crf(1, 10, crf)
570 crf1 = Connections.crf(1, 10, crf1)
571
572 # used 2 times
573 crf2 = Connections.crf(1, 10, crf)
574
575 assert crf1 > crf2
576 end
577
578 test "recently used will have crf higher on equal references", %{crf: crf} do
579 # used 3 sec ago
580 crf1 = Connections.crf(3, 10, crf)
581
582 # used 4 sec ago
583 crf2 = Connections.crf(4, 10, crf)
584
585 assert crf1 > crf2
586 end
587
588 test "equal crf on equal reference and time", %{crf: crf} do
589 # used 2 times
590 crf1 = Connections.crf(1, 10, crf)
591
592 # used 2 times
593 crf2 = Connections.crf(1, 10, crf)
594
595 assert crf1 == crf2
596 end
597
598 test "recently used will have higher crf", %{crf: crf} do
599 crf1 = Connections.crf(2, 10, crf)
600 crf1 = Connections.crf(1, 10, crf1)
601
602 crf2 = Connections.crf(3, 10, crf)
603 crf2 = Connections.crf(4, 10, crf2)
604 assert crf1 > crf2
605 end
606 end
607
608 describe "get_unused_conns/1" do
609 test "crf is equalent, sorting by reference", %{name: name} do
610 Connections.add_conn(name, "1", %Conn{
611 conn_state: :idle,
612 last_reference: now() - 1
613 })
614
615 Connections.add_conn(name, "2", %Conn{
616 conn_state: :idle,
617 last_reference: now()
618 })
619
620 assert [{"1", _unused_conn} | _others] = Connections.get_unused_conns(name)
621 end
622
623 test "reference is equalent, sorting by crf", %{name: name} do
624 Connections.add_conn(name, "1", %Conn{
625 conn_state: :idle,
626 crf: 1.999
627 })
628
629 Connections.add_conn(name, "2", %Conn{
630 conn_state: :idle,
631 crf: 2
632 })
633
634 assert [{"1", _unused_conn} | _others] = Connections.get_unused_conns(name)
635 end
636
637 test "higher crf and lower reference", %{name: name} do
638 Connections.add_conn(name, "1", %Conn{
639 conn_state: :idle,
640 crf: 3,
641 last_reference: now() - 1
642 })
643
644 Connections.add_conn(name, "2", %Conn{
645 conn_state: :idle,
646 crf: 2,
647 last_reference: now()
648 })
649
650 assert [{"2", _unused_conn} | _others] = Connections.get_unused_conns(name)
651 end
652
653 test "lower crf and lower reference", %{name: name} do
654 Connections.add_conn(name, "1", %Conn{
655 conn_state: :idle,
656 crf: 1.99,
657 last_reference: now() - 1
658 })
659
660 Connections.add_conn(name, "2", %Conn{
661 conn_state: :idle,
662 crf: 2,
663 last_reference: now()
664 })
665
666 assert [{"1", _unused_conn} | _others] = Connections.get_unused_conns(name)
667 end
668 end
669
670 test "count/1", %{name: name} do
671 assert Connections.count(name) == 0
672 Connections.add_conn(name, "1", %Conn{conn: self()})
673 assert Connections.count(name) == 1
674 Connections.remove_conn(name, "1")
675 assert Connections.count(name) == 0
676 end
677
678 defp now do
679 :os.system_time(:second)
680 end
681 end