summaryrefslogtreecommitdiff
path: root/tests/integration/test_statefile.py
diff options
context:
space:
mode:
authorDirk Engling <erdgeist@erdgeist.org>2026-11-05 13:59:14 +0100
committerDirk Engling <erdgeist@erdgeist.org>2026-11-05 13:59:14 +0100
commitd620e4c3ea42b0df6eb5e8dab92d7a8f031fa617 (patch)
tree86ce2474b0a15689b3feedeabc23c859363cae73 /tests/integration/test_statefile.py
parente9fff370e84134a0e8c43e247be6b0d52e3fe697 (diff)
Take the clock off the wall
Diffstat (limited to 'tests/integration/test_statefile.py')
-rw-r--r--tests/integration/test_statefile.py53
1 files changed, 53 insertions, 0 deletions
diff --git a/tests/integration/test_statefile.py b/tests/integration/test_statefile.py
new file mode 100644
index 0000000..2a2d147
--- /dev/null
+++ b/tests/integration/test_statefile.py
@@ -0,0 +1,53 @@
1"""The state dump and the -l switch that reads it back.
2
3Nothing exercised the load path before, and the dump's second column is now
4how long ago a torrent was last touched rather than when, because the clock
5counts from boot and an absolute stamp would mean nothing to the process
6that reads the file.
7"""
8
9import os
10import unittest
11
12from harness import Tracker, TrackerTestCase
13
14
15class StateFileRoundTrip(TrackerTestCase):
16 def dump(self):
17 return self.tracker.get("/stats?mode=statedump")
18
19 def test_a_dumped_torrent_comes_back_with_its_completed_count(self):
20 info_hash = self.hash_for("state-rt")
21 self.tracker.announce(info_hash, port=6001, left=0, event="completed")
22 self.assertEqual(self.tracker.scrape(info_hash)[b"files"][info_hash][b"downloaded"], 1)
23
24 path = os.path.join(self.tracker.dir, "state")
25 with open(path, "wb") as handle:
26 handle.write(self.dump())
27
28 restarted = Tracker(extra_args=["-l", path])
29 try:
30 files = restarted.scrape(info_hash)[b"files"]
31 self.assertIn(info_hash, files, "the torrent did not survive the restart")
32 entry = files[info_hash]
33 self.assertEqual(entry[b"complete"], 0, "no peers are carried over, only the torrent")
34 self.assertEqual(entry[b"incomplete"], 0)
35 complaints = restarted.stop()
36 self.assertEqual(complaints, [], "the tracker reported:\n" + "\n".join(complaints))
37 finally:
38 restarted._cleanup()
39
40 def test_a_torrent_older_than_the_tracker_would_keep_is_not_loaded(self):
41 """An age past the torrent timeout would be cleaned away on the first
42 sweep, so it is never built in the first place."""
43 info_hash = self.hash_for("state-stale")
44 path = os.path.join(self.tracker.dir, "stale")
45 with open(path, "w") as handle:
46 handle.write("%s:%d:7\n" % (info_hash.hex(), 24 * 60 + 1))
47
48 restarted = Tracker(extra_args=["-l", path])
49 try:
50 self.assertNotIn(info_hash, restarted.scrape(info_hash)[b"files"])
51 restarted.stop()
52 finally:
53 restarted._cleanup()