diff options
Diffstat (limited to 'tests/integration/test_statefile.py')
| -rw-r--r-- | tests/integration/test_statefile.py | 53 |
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 | |||
| 3 | Nothing exercised the load path before, and the dump's second column is now | ||
| 4 | how long ago a torrent was last touched rather than when, because the clock | ||
| 5 | counts from boot and an absolute stamp would mean nothing to the process | ||
| 6 | that reads the file. | ||
| 7 | """ | ||
| 8 | |||
| 9 | import os | ||
| 10 | import unittest | ||
| 11 | |||
| 12 | from harness import Tracker, TrackerTestCase | ||
| 13 | |||
| 14 | |||
| 15 | class 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() | ||
