2017-06-07 19:01:29 +02:00
|
|
|
import types
|
2018-11-25 17:12:30 +01:00
|
|
|
from unittest import mock
|
2017-06-07 19:01:29 +02:00
|
|
|
from twisted.trial import unittest
|
2017-06-14 23:28:37 +02:00
|
|
|
from twisted.internet import defer, task
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2018-07-05 05:16:52 +02:00
|
|
|
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p import PaymentRateManager
|
|
|
|
from lbrynet.p2p.Error import DownloadDataTimeout, DownloadSDTimeout
|
|
|
|
from lbrynet.p2p.StreamDescriptor import StreamDescriptorIdentifier
|
|
|
|
from lbrynet.p2p.BlobManager import DiskBlobManager
|
|
|
|
from lbrynet.p2p.RateLimiter import DummyRateLimiter
|
2018-11-04 19:44:17 +01:00
|
|
|
from lbrynet.extras.daemon import Downloader
|
|
|
|
from lbrynet.extras.daemon import ExchangeRateManager
|
2018-11-04 20:00:57 +01:00
|
|
|
from lbrynet.extras.daemon.storage import SQLiteStorage
|
2018-11-07 21:15:05 +01:00
|
|
|
from lbrynet.extras.daemon.PeerFinder import DummyPeerFinder
|
2018-11-04 20:55:01 +01:00
|
|
|
from lbrynet.blob.EncryptedFileStatusReport import EncryptedFileStatusReport
|
|
|
|
from lbrynet.blob.EncryptedFileDownloader import ManagedEncryptedFileDownloader
|
2018-11-04 10:42:47 +01:00
|
|
|
from lbrynet.extras.wallet import LbryWalletManager
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2018-08-05 03:08:22 +02:00
|
|
|
from tests.mocks import mock_conf_settings
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2017-09-27 23:47:50 +02:00
|
|
|
|
2018-10-18 12:42:45 +02:00
|
|
|
class MocDownloader:
|
2017-06-14 23:28:37 +02:00
|
|
|
def __init__(self):
|
|
|
|
self.finish_deferred = defer.Deferred(None)
|
|
|
|
self.stop_called = False
|
|
|
|
|
|
|
|
self.name = 'test'
|
|
|
|
self.num_completed = 0
|
|
|
|
self.num_known = 1
|
|
|
|
self.running_status = ManagedEncryptedFileDownloader.STATUS_RUNNING
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def status(self):
|
2017-10-02 18:13:45 +02:00
|
|
|
out = yield EncryptedFileStatusReport(
|
|
|
|
self.name, self.num_completed, self.num_known, self.running_status)
|
2017-06-14 23:28:37 +02:00
|
|
|
defer.returnValue(out)
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
return self.finish_deferred
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.stop_called = True
|
|
|
|
self.finish_deferred.callback(True)
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2017-09-29 12:44:22 +02:00
|
|
|
def moc_initialize(self, stream_info):
|
2017-10-02 18:13:45 +02:00
|
|
|
self.sd_hash = "d5169241150022f996fa7cd6a9a1c421937276a3275eb912" \
|
|
|
|
"790bd07ba7aec1fac5fd45431d226b8fb402691e79aeb24b"
|
2017-06-14 23:28:37 +02:00
|
|
|
return None
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2017-06-14 23:28:37 +02:00
|
|
|
def moc_download_sd_blob(self):
|
|
|
|
return None
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
|
|
|
|
def moc_download(self, sd_blob, name, txid, nout, key_fee, file_name):
|
2017-06-14 23:28:37 +02:00
|
|
|
self.pay_key_fee(key_fee, name)
|
|
|
|
self.downloader = MocDownloader()
|
|
|
|
self.downloader.start()
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2017-06-14 23:28:37 +02:00
|
|
|
def moc_pay_key_fee(self, key_fee, name):
|
|
|
|
self.pay_key_fee_called = True
|
|
|
|
|
2018-02-12 20:19:15 +01:00
|
|
|
|
2017-06-07 19:01:29 +02:00
|
|
|
class GetStreamTests(unittest.TestCase):
|
2018-07-06 22:16:58 +02:00
|
|
|
|
2017-06-07 19:01:29 +02:00
|
|
|
def init_getstream_with_mocs(self):
|
2017-06-13 21:28:49 +02:00
|
|
|
mock_conf_settings(self)
|
2018-07-06 22:16:58 +02:00
|
|
|
|
2017-06-07 19:01:29 +02:00
|
|
|
sd_identifier = mock.Mock(spec=StreamDescriptorIdentifier)
|
2018-08-05 03:08:22 +02:00
|
|
|
wallet = mock.Mock(spec=LbryWalletManager)
|
2017-06-07 19:01:29 +02:00
|
|
|
prm = mock.Mock(spec=PaymentRateManager.NegotiatedPaymentRateManager)
|
|
|
|
exchange_rate_manager = mock.Mock(spec=ExchangeRateManager)
|
2018-08-02 20:34:02 +02:00
|
|
|
storage = mock.Mock(spec=SQLiteStorage)
|
|
|
|
peer_finder = DummyPeerFinder()
|
|
|
|
blob_manager = mock.Mock(spec=DiskBlobManager)
|
|
|
|
max_key_fee = {'currency': "LBC", 'amount': 10, 'address': ''}
|
2017-07-06 20:53:16 +02:00
|
|
|
disable_max_key_fee = False
|
2018-08-02 20:34:02 +02:00
|
|
|
data_rate = {'currency': "LBC", 'amount': 0, 'address': ''}
|
2018-08-03 15:36:03 +02:00
|
|
|
getstream = Downloader.GetStream(
|
|
|
|
sd_identifier, wallet, exchange_rate_manager, blob_manager, peer_finder, DummyRateLimiter(), prm,
|
|
|
|
storage, max_key_fee, disable_max_key_fee, timeout=3, data_rate=data_rate
|
|
|
|
)
|
2017-06-14 23:28:37 +02:00
|
|
|
getstream.pay_key_fee_called = False
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2017-06-14 23:28:37 +02:00
|
|
|
self.clock = task.Clock()
|
|
|
|
getstream.checker.clock = self.clock
|
2017-06-07 19:01:29 +02:00
|
|
|
return getstream
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_init_exception(self):
|
|
|
|
"""
|
2018-10-18 13:40:37 +02:00
|
|
|
test that if initialization would fail, by giving it invalid
|
2017-06-07 19:01:29 +02:00
|
|
|
stream_info, that an exception is thrown
|
|
|
|
"""
|
|
|
|
|
|
|
|
getstream = self.init_getstream_with_mocs()
|
|
|
|
name = 'test'
|
|
|
|
stream_info = None
|
|
|
|
|
|
|
|
with self.assertRaises(AttributeError):
|
2018-02-12 20:19:15 +01:00
|
|
|
yield getstream.start(stream_info, name, "deadbeef" * 12, 0)
|
2017-06-07 19:01:29 +02:00
|
|
|
|
2017-06-14 23:28:37 +02:00
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_sd_blob_download_timeout(self):
|
|
|
|
"""
|
|
|
|
test that if download_sd_blob fails due to timeout,
|
|
|
|
DownloadTimeoutError is raised
|
|
|
|
"""
|
|
|
|
def download_sd_blob(self):
|
2018-01-05 03:28:09 +01:00
|
|
|
raise DownloadSDTimeout(self)
|
2017-06-14 23:28:37 +02:00
|
|
|
|
2017-09-29 12:44:22 +02:00
|
|
|
getstream = self.init_getstream_with_mocs()
|
2017-06-14 23:28:37 +02:00
|
|
|
getstream._initialize = types.MethodType(moc_initialize, getstream)
|
|
|
|
getstream._download_sd_blob = types.MethodType(download_sd_blob, getstream)
|
|
|
|
getstream._download = types.MethodType(moc_download, getstream)
|
|
|
|
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream)
|
2017-09-29 12:44:22 +02:00
|
|
|
name = 'test'
|
2017-06-14 23:28:37 +02:00
|
|
|
stream_info = None
|
2017-09-27 23:47:50 +02:00
|
|
|
with self.assertRaises(DownloadSDTimeout):
|
2018-02-12 20:19:15 +01:00
|
|
|
yield getstream.start(stream_info, name, "deadbeef" * 12, 0)
|
2017-06-14 23:28:37 +02:00
|
|
|
self.assertFalse(getstream.pay_key_fee_called)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_timeout(self):
|
|
|
|
"""
|
2017-09-28 17:04:36 +02:00
|
|
|
test that timeout (set to 3 here) exception is raised
|
2017-06-14 23:28:37 +02:00
|
|
|
when download times out while downloading first blob, and key fee is paid
|
|
|
|
"""
|
2017-09-29 12:44:22 +02:00
|
|
|
getstream = self.init_getstream_with_mocs()
|
2017-06-14 23:28:37 +02:00
|
|
|
getstream._initialize = types.MethodType(moc_initialize, getstream)
|
|
|
|
getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream)
|
|
|
|
getstream._download = types.MethodType(moc_download, getstream)
|
|
|
|
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream)
|
2017-09-29 12:44:22 +02:00
|
|
|
name = 'test'
|
2017-06-14 23:28:37 +02:00
|
|
|
stream_info = None
|
2018-02-12 20:19:15 +01:00
|
|
|
start = getstream.start(stream_info, name, "deadbeef" * 12, 0)
|
2017-06-14 23:28:37 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
self.clock.advance(1)
|
2017-09-28 17:04:36 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
with self.assertRaises(DownloadDataTimeout):
|
2017-06-14 23:28:37 +02:00
|
|
|
yield start
|
|
|
|
self.assertTrue(getstream.pay_key_fee_called)
|
|
|
|
|
|
|
|
@defer.inlineCallbacks
|
|
|
|
def test_finish_one_blob(self):
|
|
|
|
"""
|
|
|
|
test that if we have 1 completed blob, start() returns
|
|
|
|
and key fee is paid
|
|
|
|
"""
|
2017-09-29 12:44:22 +02:00
|
|
|
getstream = self.init_getstream_with_mocs()
|
2017-06-14 23:28:37 +02:00
|
|
|
getstream._initialize = types.MethodType(moc_initialize, getstream)
|
|
|
|
|
|
|
|
getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream)
|
|
|
|
getstream._download = types.MethodType(moc_download, getstream)
|
|
|
|
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream)
|
2017-09-29 12:44:22 +02:00
|
|
|
name = 'test'
|
2017-06-14 23:28:37 +02:00
|
|
|
stream_info = None
|
2018-02-12 20:19:15 +01:00
|
|
|
start = getstream.start(stream_info, name, "deadbeef" * 12, 0)
|
2017-06-14 23:28:37 +02:00
|
|
|
getstream.downloader.num_completed = 1
|
|
|
|
self.clock.advance(1)
|
|
|
|
|
|
|
|
downloader, f_deferred = yield start
|
|
|
|
self.assertTrue(getstream.pay_key_fee_called)
|
|
|
|
|
2017-09-27 23:47:50 +02:00
|
|
|
# @defer.inlineCallbacks
|
|
|
|
# def test_finish_stopped_downloader(self):
|
|
|
|
# """
|
|
|
|
# test that if we have a stopped downloader, beforfe a blob is downloaded,
|
|
|
|
# start() returns
|
|
|
|
# """
|
|
|
|
# getstream = self.init_getstream_with_mocs()
|
|
|
|
# getstream._initialize = types.MethodType(moc_initialize, getstream)
|
|
|
|
# getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream)
|
|
|
|
# getstream._download = types.MethodType(moc_download, getstream)
|
|
|
|
# name='test'
|
|
|
|
# stream_info = None
|
|
|
|
# start = getstream.start(stream_info,name)
|
|
|
|
#
|
|
|
|
# getstream.downloader.running_status = ManagedEncryptedFileDownloader.STATUS_STOPPED
|
|
|
|
# self.clock.advance(1)
|
|
|
|
# downloader, f_deferred = yield start
|