summaryrefslogtreecommitdiff
path: root/tests/integration/test_keepalive.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/integration/test_keepalive.py')
-rw-r--r--tests/integration/test_keepalive.py146
1 files changed, 146 insertions, 0 deletions
diff --git a/tests/integration/test_keepalive.py b/tests/integration/test_keepalive.py
new file mode 100644
index 0000000..f9aa468
--- /dev/null
+++ b/tests/integration/test_keepalive.py
@@ -0,0 +1,146 @@
1"""Holding a connection open across several requests.
2
3The reply path has two shapes. A small answer goes out in one write and the
4connection is done with immediately. A large one is queued and finishes on a
5later pass through the event loop, by which time the workstruct that served
6the request has moved on, so whether to keep the connection has to be
7remembered on the connection itself.
8"""
9
10import time
11import unittest
12import urllib.parse
13
14from harness import TrackerTestCase, bdecode
15
16
17class KeepAlive(TrackerTestCase):
18 def announce_path(self, name, port=6881):
19 return "/announce?info_hash=%s&peer_id=%s&port=%d&left=0" % (
20 urllib.parse.quote_from_bytes(self.hash_for(name), safe=""),
21 "x" * 20,
22 port,
23 )
24
25 def test_two_requests_over_one_connection(self):
26 c = self.tracker.connect()
27 try:
28 c.send(self.announce_path("kaA"))
29 status, _, first = c.reply()
30 self.assertIn(b"200", status)
31 c.send(self.announce_path("kaB"))
32 status, _, second = c.reply()
33 self.assertIn(b"200", status)
34 self.assertIn(b"complete", bdecode(second))
35 finally:
36 c.close()
37
38 def test_close_ends_the_connection(self):
39 c = self.tracker.connect()
40 try:
41 c.send(self.announce_path("kaclose"), connection="close")
42 c.reply()
43 self.assertFalse(c.is_open(), "the tracker was asked to hang up")
44 finally:
45 c.close()
46
47 def test_a_connection_survives_a_reply_too_large_for_one_write(self):
48 """The case that was broken: a queued reply finished draining and the
49 connection was closed regardless of what the client had asked for."""
50 for i in range(400):
51 self.tracker.announce(bytes([i // 256, i % 256]) + b"\0" * 18, port=7000 + i, left=0)
52
53 c = self.tracker.connect()
54 try:
55 c.send("/scrape")
56 status, headers, body = c.reply()
57 self.assertIn(b"200", status)
58 self.assertGreater(len(body), 16384, "this reply has to outgrow one write")
59
60 c.send(self.announce_path("kaafter"))
61 status, _, after = c.reply()
62 self.assertIn(b"200", status)
63 self.assertIn(b"complete", bdecode(after))
64 finally:
65 c.close()
66
67 def test_several_requests_in_a_row(self):
68 c = self.tracker.connect()
69 try:
70 for i in range(20):
71 c.send(self.announce_path("karow%d" % i))
72 status, _, body = c.reply()
73 self.assertIn(b"200", status)
74 self.assertEqual(self.tracker.torrent_count(), 20)
75 finally:
76 c.close()
77
78 def test_without_the_header_the_connection_is_not_held(self):
79 """HTTP/1.1 calls a connection persistent unless told otherwise; this
80 tracker holds one only when asked by name. Pinned as it is."""
81 c = self.tracker.connect()
82 try:
83 c.send(self.announce_path("kanone"), connection=None)
84 c.reply()
85 self.assertFalse(c.is_open())
86 finally:
87 c.close()
88
89 def test_the_error_path_does_not_hold_the_connection(self):
90 c = self.tracker.connect()
91 try:
92 c.send("/announce?info_hash=short")
93 status, _, _ = c.reply()
94 self.assertIn(b"400", status)
95 finally:
96 c.close()
97
98 def test_an_error_after_a_kept_connection_is_still_an_error(self):
99 """The keep-alive decision used to be taken after the handler had run,
100 so an error reply inherited whatever the previous request wanted."""
101 c = self.tracker.connect()
102 try:
103 c.send(self.announce_path("kabefore"))
104 self.assertIn(b"200", c.reply()[0])
105 c.send("/announce?info_hash=short")
106 self.assertIn(b"400", c.reply()[0])
107 finally:
108 c.close()
109
110 def test_a_tiny_split_request_does_not_overread(self):
111 """A request that arrives in pieces and completes as a buffer shorter
112 than the dispatch literals (\"GET /\" etc.) used to read past the
113 exactly sized buffer. It must be rejected without a sanitizer hit."""
114 c = self.tracker.connect()
115 try:
116 c.sock.sendall(b"\r\n")
117 time.sleep(0.05)
118 c.sock.sendall(b"\r\n") # completes as a 4-byte buffer
119 time.sleep(0.1)
120 finally:
121 c.close()
122 # the tracker must still answer; tearDown's stop() surfaces any sanitizer abort
123 answer = self.tracker.announce(self.hash_for("after-tiny"), port=6881, left=0)
124 self.assertNotIn(b"failure reason", answer, "the tracker stopped serving after a tiny request")
125
126 def test_a_split_request_answered_by_a_worker_does_not_spin(self):
127 """A reply that a worker produces does not go through the send path,
128 so the request stays in the buffer the read loop is reading. The loop
129 used to find the same request again on every pass, queue a task for
130 it, and never hand the thread back to the event loop."""
131 for path in ("/stats", "/scrape"):
132 c = self.tracker.connect()
133 try:
134 c.sock.sendall(("GET %s HTTP/1.1\r\nConnection: keep-alive\r\n" % path).encode())
135 time.sleep(0.2)
136 c.sock.sendall(b"\r\n")
137 self.assertIn(b"200", c.reply()[0], "%s was not answered" % path)
138 finally:
139 c.close()
140
141 answer = self.tracker.announce(self.hash_for("after-" + path), port=6881, left=0)
142 self.assertNotIn(b"failure reason", answer, "the tracker stopped serving after %s" % path)
143
144
145if __name__ == "__main__":
146 unittest.main()