summaryrefslogtreecommitdiffstats
path: root/deluge/tests/test_torrent.py
diff options
context:
space:
mode:
authorCalum Lind <calumlind+deluge@gmail.com>2015-12-12 14:23:00 +0000
committerCalum Lind <calumlind+deluge@gmail.com>2015-12-12 14:29:07 +0000
commit080d137af8542ebd854c53ff40269b75941be53a (patch)
tree4fa6e47753c0dc27f700b6dee9109eb8c7de92a9 /deluge/tests/test_torrent.py
parent02f6bfd578f3a5a52d8bef21e11e9602c9816a31 (diff)
downloaddeluge-080d137af8542ebd854c53ff40269b75941be53a.tar.gz
deluge-080d137af8542ebd854c53ff40269b75941be53a.tar.bz2
deluge-080d137af8542ebd854c53ff40269b75941be53a.zip
[Tests] Move test_torrent_error code into test_torrent
Diffstat (limited to 'deluge/tests/test_torrent.py')
-rw-r--r--deluge/tests/test_torrent.py83
1 files changed, 82 insertions, 1 deletions
diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py
index 90774713e..9e31ab408 100644
--- a/deluge/tests/test_torrent.py
+++ b/deluge/tests/test_torrent.py
@@ -1,22 +1,28 @@
from __future__ import print_function
+import base64
import os
import sys
+import time
+from twisted.internet import reactor
+from twisted.internet.task import deferLater
from twisted.trial import unittest
import deluge.component as component
import deluge.core.torrent
+import deluge.tests.common as common
from deluge._libtorrent import lt
from deluge.core.core import Core
from deluge.core.rpcserver import RPCServer
from deluge.core.torrent import Torrent
+from deluge.core.torrentmanager import TorrentState
-from . import common
config_setup = False
core = None
rpcserver = None
+eventmanager = None
# This is called by torrent.py when calling component.get("...")
@@ -27,6 +33,8 @@ def get(key):
return rpcserver
elif key is "EventManager":
return core.eventmanager
+ elif key is "TorrentManager":
+ return core.torrentmanager
else:
return None
@@ -70,6 +78,10 @@ class TorrentTestCase(unittest.TestCase):
tmp += "%s" % p
print(tmp)
+ def assert_state(self, torrent, state):
+ torrent.update_state()
+ self.assertEquals(torrent.state, state)
+
def get_torrent_atp(self, filename):
filename = os.path.join(os.path.dirname(__file__), filename)
e = lt.bdecode(open(filename, 'rb').read())
@@ -132,3 +144,72 @@ class TorrentTestCase(unittest.TestCase):
self.assertEquals(priorities[i], 1)
# self.print_priority_list(priorities)
+
+ def test_torrent_error_data_missing(self):
+ options = {"seed_mode": True}
+ filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
+ torrent_id = core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ torrent = core.torrentmanager.torrents[torrent_id]
+
+ self.assert_state(torrent, "Seeding")
+
+ # Force an error by reading (non-existant) piece from disk
+ torrent.handle.read_piece(0)
+ time.sleep(0.2) # Delay to wait for alert from lt
+ self.assert_state(torrent, "Error")
+
+ def test_torrent_error_resume_original_state(self):
+ options = {"seed_mode": True, "add_paused": True}
+ filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
+ torrent_id = core.add_torrent_file(filename, base64.encodestring(open(filename).read()), options)
+ torrent = core.torrentmanager.torrents[torrent_id]
+
+ orig_state = "Paused"
+ self.assert_state(torrent, orig_state)
+
+ # Force an error by reading (non-existant) piece from disk
+ torrent.handle.read_piece(0)
+ time.sleep(0.2) # Delay to wait for alert from lt
+ self.assert_state(torrent, "Error")
+
+ # Clear error and verify returned to original state
+ torrent.force_recheck()
+ return deferLater(reactor, 0.1, self.assert_state, torrent, orig_state)
+
+ def test_torrent_error_resume_data_unaltered(self):
+ resume_data = {'active_time': 13399, 'num_incomplete': 16777215, 'announce_to_lsd': 1, 'seed_mode': 0,
+ 'pieces': '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01', 'paused': 0,
+ 'seeding_time': 13399, 'last_scrape': 13399,
+ 'info-hash': '-\xc5\xd0\xe7\x1af\xfeid\x9ad\r9\xcb\x00\xa2YpIs', 'max_uploads': 16777215,
+ 'max_connections': 16777215, 'num_downloaders': 16777215, 'total_downloaded': 0,
+ 'file-format': 'libtorrent resume file', 'peers6': '', 'added_time': 1411826665,
+ 'banned_peers6': '', 'file_priority': [1], 'last_seen_complete': 0, 'total_uploaded': 0,
+ 'piece_priority': '\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01',
+ 'file-version': 1, 'announce_to_dht': 1, 'auto_managed': 1, 'upload_rate_limit': 0,
+ 'completed_time': 1411826665, 'allocation': 'sparse', 'blocks per piece': 2,
+ 'download_rate_limit': 0, 'libtorrent-version': '0.16.17.0', 'banned_peers': '',
+ 'num_seeds': 16777215, 'sequential_download': 0, 'announce_to_trackers': 1,
+ 'peers': '\n\x00\x02\x0f=\xc6SC\x17]\xd8}\x7f\x00\x00\x01=\xc6', 'finished_time': 13399,
+ 'last_upload': 13399, 'trackers': [[]], 'super_seeding': 0,
+ 'file sizes': [[512000, 1411826586]], 'last_download': 13399}
+ torrent_state = TorrentState(
+ torrent_id='2dc5d0e71a66fe69649a640d39cb00a259704973',
+ filename='test_torrent.file.torrent',
+ name='',
+ save_path='/home/ubuntu/Downloads',
+ file_priorities=[1],
+ is_finished=True,
+ )
+
+ filename = os.path.join(os.path.dirname(__file__), "test_torrent.file.torrent")
+ filedump = open(filename).read()
+ torrent_id = core.torrentmanager.add(state=torrent_state, filedump=filedump,
+ resume_data=lt.bencode(resume_data))
+ torrent = core.torrentmanager.torrents[torrent_id]
+
+ def assert_resume_data():
+ self.assert_state(torrent, "Error")
+ tm_resume_data = lt.bdecode(core.torrentmanager.resume_data[torrent.torrent_id])
+ self.assertEquals(tm_resume_data, resume_data)
+
+ return deferLater(reactor, 0.5, assert_resume_data)