summaryrefslogtreecommitdiff
path: root/tests/integration/test_expiry.py
blob: 5e6dc0ab1f7d01e9f8ee64be7733eb880d37ff8d (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
"""What the expiry sweep finds, and what it must not find.

A plain request is answered and the connection closed by http_close, not by
the sweep. If that path left the descriptor's slot behind, the sweep would
walk into it once the client timeout had passed and free a cookie that is
already gone; the sanitizer build reports that and teardown turns it into a
failure.

Both halves share one tracker and one wait, because the wait is the whole
cost of the test.

The two numbers below mirror OT_CLIENT_TIMEOUT and
OT_CLIENT_TIMEOUT_CHECKINTERVAL in FEATURES_integration.
"""

import socket
import time
import unittest

from harness import TrackerTestCase

CLIENT_TIMEOUT = 5
CHECK_INTERVAL = 1


class ExpirySweep(TrackerTestCase):
    def test_the_sweep_takes_the_idle_and_leaves_the_answered(self):
        idle = socket.create_connection(("127.0.0.1", self.tracker.port), timeout=5)
        try:
            # Enough for a kernel that holds a connection back until data
            # arrives to hand it over, and far too little to be answered, so
            # the tracker has accepted it and is waiting for the rest
            idle.sendall(b"GET /ann")

            self.assertEqual(self.tracker.announce(self.hash_for("expiry-sweep"))[b"complete"], 1)

            time.sleep(CLIENT_TIMEOUT + 3 * CHECK_INTERVAL)

            self.assertEqual(self.tracker.torrent_count(), 1, "the tracker stopped serving while sweeping")

            idle.settimeout(5)
            self.assertEqual(idle.recv(64), b"", "a connection that went quiet mid-request was not swept")
        finally:
            idle.close()