本文整理汇总了Python中Tribler.Test.Core.base_test.MockObject.torrent_status方法的典型用法代码示例。如果您正苦于以下问题:Python MockObject.torrent_status方法的具体用法?Python MockObject.torrent_status怎么用?Python MockObject.torrent_status使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Tribler.Test.Core.base_test.MockObject
的用法示例。
在下文中一共展示了MockObject.torrent_status方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_basic_policy_run
# 需要导入模块: from Tribler.Test.Core.base_test import MockObject [as 别名]
# 或者: from Tribler.Test.Core.base_test.MockObject import torrent_status [as 别名]
def test_basic_policy_run(self):
"""
Test running an iteration of basic policy.
Scenario: There are 10 torrents with infohashes ordered as 0-9 and the torrents with odd infohashes
are downloading while the rest are stopped. In the next iteration, we assume that all the
torrents with infohashes as multiple of 3 are scheduled to start and the rest to be stopped.
The scenario is represented in the table below:
Infohash Status To Start -> Result
0 STOPPED True Started
1 DOWNLOADING False Stopped
2 STOPPED False Do Nothing
3 DOWNLOADING True Do Nothing
4 STOPPED False Do Nothing
5 DOWNLOADING False Stopped
6 STOPPED True Started
7 DOWNLOADING False Stopped
8 STOPPED False Do Nothing
9 DOWNLOADING True Do Nothing
At the end of the iteration, the following result is expected:
Started = 2
Stopped = 3
"""
scenario = MockObject()
scenario.to_start = [True, False, False, True, False, False, True, False, False, True]
scenario.torrent_status = [DLSTATUS_STOPPED, DLSTATUS_DOWNLOADING, DLSTATUS_STOPPED,
DLSTATUS_DOWNLOADING, DLSTATUS_STOPPED, DLSTATUS_DOWNLOADING,
DLSTATUS_STOPPED, DLSTATUS_DOWNLOADING, DLSTATUS_STOPPED,
DLSTATUS_DOWNLOADING]
# Any BasicPolicy implementation is fine.
policy = UploadPolicy()
def get_status(scenario, index):
return scenario.torrent_status[index]
for i, torrent in enumerate(self.torrents):
torrent.download = MockObject()
torrent.download.state = MockObject()
torrent.download.state.get_status = lambda _scenario=scenario, index=i: get_status(_scenario, index)
torrent.download.get_state = lambda _torrent=torrent: _torrent.download.state
torrent.download.restart = lambda: None
torrent.download.stop = lambda: None
policy.schedule(torrent, to_start=scenario.to_start[i])
policy.run()
self.assertEqual(policy.started_in_iteration, 2)
self.assertEqual(policy.stopped_in_iteration, 3)
示例2: test_investment_policy_run
# 需要导入模块: from Tribler.Test.Core.base_test import MockObject [as 别名]
# 或者: from Tribler.Test.Core.base_test.MockObject import torrent_status [as 别名]
def test_investment_policy_run(self):
"""
Test running an iteration of investment policy.
Scenario
------------------------------------------------
Infohash, Level, Download, Upload, To start, ETA, Status --> Expected Result
0 5 20 16 Yes 0 Downloading Upload mode
1 0 4 2 Yes 1 Downloading Stop
2 1 5 8 Yes 1 Seeding Promote -> Download mode
3 9 22 23 Yes 1 Seeding Promote -> Download mode
4 9 30 25 No 1 Downloading Stop
5 0 4 3 Yes 1 Downloading Stop; stale
6 12 40 35 Yes 1 Stopped Download mode
7 15 50 40 No 1 Seeding Stop
8 18 160 120 Yes 1 Downloading Do nothing -> Download mode
9 19 163 170 Yes 1 Stopped Promote -> Upload mode
At the end of the iteration, the following result is expected:
Started = 6 # Includes downloading and seeding torrents
Stopped = 4
Upload mode = 2 # New torrents set in upload mode
Download mode = 3 # New torrents set in download mode
"""
scenario = MockObject()
scenario.downloads = [20, 4, 5, 22, 30, 4, 40, 50, 160, 163]
scenario.uploads = [16, 2, 8, 23, 25, 3, 35, 40, 120, 170]
scenario.levels = [5, 0, 1, 9, 9, 0, 12, 15, 18, 19]
scenario.to_start = [True, False, True, True, False, True, True, False, True, True]
scenario.torrent_status = [DLSTATUS_DOWNLOADING, DLSTATUS_DOWNLOADING, DLSTATUS_SEEDING,
DLSTATUS_SEEDING, DLSTATUS_DOWNLOADING, DLSTATUS_DOWNLOADING,
DLSTATUS_STOPPED, DLSTATUS_SEEDING, DLSTATUS_DOWNLOADING,
DLSTATUS_STOPPED]
def get_status(scenario, torrent):
return scenario.torrent_status[torrent.infohash]
def get_eta(torrent):
return 0 if torrent.infohash == 0 else 1
def get_total_transferred(scenario, torrent, transfer_type):
if transfer_type == UPLOAD:
return scenario.uploads[torrent.infohash] * MB
return scenario.downloads[torrent.infohash] * MB
def get_mining_state(scenario, torrent):
mining_state = dict()
mining_state['initial_upload'] = 0
mining_state['initial_download'] = 0
mining_state['state_id'] = scenario.levels[torrent.infohash]
return mining_state
for torrent in self.torrents:
torrent.download = MockObject()
torrent.download.state = MockObject()
torrent.download.state.get_eta = lambda _torrent=torrent: get_eta(_torrent)
torrent.download.state.get_total_transferred = lambda transfer_type, _torrent=torrent, _scenario=scenario: \
get_total_transferred(_scenario, _torrent, transfer_type)
torrent.mining_state = get_mining_state(scenario, torrent)
torrent.download.state.get_status = lambda _scenario=scenario, _torrent=torrent: \
get_status(_scenario, _torrent)
torrent.download.get_state = lambda _torrent=torrent: _torrent.download.state
torrent.download.set_upload_mode = lambda _: None
torrent.download.restart = lambda: None
torrent.download.stop = lambda: None
torrent.start_time = time.time() - WEEK - 1 if torrent.infohash == 5 else time.time()
# Schedule torrent to start or stop
self.policy.schedule(torrent, to_start=scenario.to_start[torrent.infohash])
# Torrents are ready, run the policy
self.policy.run()
self.assertEqual(self.policy.started_in_iteration, 6)
self.assertEqual(self.policy.stopped_in_iteration, 4)
self.assertEqual(self.policy.num_downloading_in_iteration, 3)
self.assertEqual(self.policy.num_uploading_in_iteration, 2)