This commit is contained in:
Jack Robison 2018-11-16 10:50:08 -05:00
parent 9dc4608a62
commit 434c7b57b1
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 28 additions and 24 deletions

View file

@ -6,8 +6,7 @@ from lbrynet import conf
from lbrynet.schema.fee import Fee from lbrynet.schema.fee import Fee
from lbrynet.p2p.Error import InsufficientFundsError, KeyFeeAboveMaxAllowed, InvalidStreamDescriptorError from lbrynet.p2p.Error import InsufficientFundsError, KeyFeeAboveMaxAllowed, InvalidStreamDescriptorError
from lbrynet.p2p.Error import DownloadDataTimeout, DownloadCanceledError, DownloadSDTimeout from lbrynet.p2p.Error import DownloadDataTimeout, DownloadCanceledError
from lbrynet.utils import safe_start_looping_call, safe_stop_looping_call
from lbrynet.p2p.StreamDescriptor import download_sd_blob from lbrynet.p2p.StreamDescriptor import download_sd_blob
from lbrynet.blob.EncryptedFileDownloader import ManagedEncryptedFileDownloaderFactory from lbrynet.blob.EncryptedFileDownloader import ManagedEncryptedFileDownloaderFactory
from torba.client.constants import COIN from torba.client.constants import COIN
@ -181,7 +180,6 @@ class GetStream:
downloader - instance of ManagedEncryptedFileDownloader downloader - instance of ManagedEncryptedFileDownloader
finished_deferred - deferred callbacked when download is finished finished_deferred - deferred callbacked when download is finished
""" """
self.set_status(INITIALIZING_CODE, name) self.set_status(INITIALIZING_CODE, name)
key_fee = yield self._initialize(stream_info) key_fee = yield self._initialize(stream_info)
self.set_status(DOWNLOAD_METADATA_CODE, name) self.set_status(DOWNLOAD_METADATA_CODE, name)
@ -191,7 +189,11 @@ class GetStream:
self.set_status(DOWNLOAD_RUNNING_CODE, name) self.set_status(DOWNLOAD_RUNNING_CODE, name)
log.info("Downloading lbry://%s (%s) --> %s", name, self.sd_hash[:6], self.download_path) log.info("Downloading lbry://%s (%s) --> %s", name, self.sd_hash[:6], self.download_path)
self.data_downloading_deferred.addTimeout(self.timeout, self.reactor) self.data_downloading_deferred.addTimeout(self.timeout, self.reactor)
yield self.data_downloading_deferred try:
yield self.data_downloading_deferred
self.wrote_data = True
except defer.TimeoutError:
raise DownloadDataTimeout("data download timed out")
except (DownloadDataTimeout, InvalidStreamDescriptorError) as err: except (DownloadDataTimeout, InvalidStreamDescriptorError) as err:
raise err raise err

View file

@ -9,6 +9,7 @@ from lbrynet.p2p.Error import DownloadDataTimeout, DownloadSDTimeout
from lbrynet.p2p.StreamDescriptor import StreamDescriptorIdentifier from lbrynet.p2p.StreamDescriptor import StreamDescriptorIdentifier
from lbrynet.p2p.BlobManager import DiskBlobManager from lbrynet.p2p.BlobManager import DiskBlobManager
from lbrynet.p2p.RateLimiter import DummyRateLimiter from lbrynet.p2p.RateLimiter import DummyRateLimiter
from lbrynet.p2p.client.DownloadManager import DownloadManager
from lbrynet.extras.daemon import Downloader from lbrynet.extras.daemon import Downloader
from lbrynet.extras.daemon import ExchangeRateManager from lbrynet.extras.daemon import ExchangeRateManager
from lbrynet.extras.daemon.storage import SQLiteStorage from lbrynet.extras.daemon.storage import SQLiteStorage
@ -24,7 +25,7 @@ class MocDownloader:
def __init__(self): def __init__(self):
self.finish_deferred = defer.Deferred(None) self.finish_deferred = defer.Deferred(None)
self.stop_called = False self.stop_called = False
self.file_name = 'test'
self.name = 'test' self.name = 'test'
self.num_completed = 0 self.num_completed = 0
self.num_known = 1 self.num_known = 1
@ -60,8 +61,10 @@ def moc_download(self, sd_blob, name, txid, nout, key_fee, file_name):
self.downloader.start() self.downloader.start()
def moc_pay_key_fee(self, key_fee, name): def moc_pay_key_fee(d):
self.pay_key_fee_called = True def _moc_pay_key_fee(self, key_fee, name):
d.callback(True)
return _moc_pay_key_fee
class GetStreamTests(unittest.TestCase): class GetStreamTests(unittest.TestCase):
@ -83,10 +86,7 @@ class GetStreamTests(unittest.TestCase):
sd_identifier, wallet, exchange_rate_manager, blob_manager, peer_finder, DummyRateLimiter(), prm, 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 storage, max_key_fee, disable_max_key_fee, timeout=3, data_rate=data_rate
) )
getstream.pay_key_fee_called = False getstream.download_manager = mock.Mock(spec=DownloadManager)
self.clock = task.Clock()
getstream.checker.clock = self.clock
return getstream return getstream
@defer.inlineCallbacks @defer.inlineCallbacks
@ -112,16 +112,18 @@ class GetStreamTests(unittest.TestCase):
def download_sd_blob(self): def download_sd_blob(self):
raise DownloadSDTimeout(self) raise DownloadSDTimeout(self)
called_pay_fee = defer.Deferred()
getstream = self.init_getstream_with_mocs() getstream = self.init_getstream_with_mocs()
getstream._initialize = types.MethodType(moc_initialize, getstream) getstream._initialize = types.MethodType(moc_initialize, getstream)
getstream._download_sd_blob = types.MethodType(download_sd_blob, getstream) getstream._download_sd_blob = types.MethodType(download_sd_blob, getstream)
getstream._download = types.MethodType(moc_download, getstream) getstream._download = types.MethodType(moc_download, getstream)
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream) getstream.pay_key_fee = types.MethodType(moc_pay_key_fee(called_pay_fee), getstream)
name = 'test' name = 'test'
stream_info = None stream_info = None
with self.assertRaises(DownloadSDTimeout): with self.assertRaises(DownloadSDTimeout):
yield getstream.start(stream_info, name, "deadbeef" * 12, 0) yield getstream.start(stream_info, name, "deadbeef" * 12, 0)
self.assertFalse(getstream.pay_key_fee_called) self.assertFalse(called_pay_fee.called)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_timeout(self): def test_timeout(self):
@ -129,20 +131,18 @@ class GetStreamTests(unittest.TestCase):
test that timeout (set to 3 here) exception is raised test that timeout (set to 3 here) exception is raised
when download times out while downloading first blob, and key fee is paid when download times out while downloading first blob, and key fee is paid
""" """
called_pay_fee = defer.Deferred()
getstream = self.init_getstream_with_mocs() getstream = self.init_getstream_with_mocs()
getstream._initialize = types.MethodType(moc_initialize, getstream) getstream._initialize = types.MethodType(moc_initialize, getstream)
getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream) getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream)
getstream._download = types.MethodType(moc_download, getstream) getstream._download = types.MethodType(moc_download, getstream)
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream) getstream.pay_key_fee = types.MethodType(moc_pay_key_fee(called_pay_fee), getstream)
name = 'test' name = 'test'
stream_info = None stream_info = None
start = getstream.start(stream_info, name, "deadbeef" * 12, 0) start = getstream.start(stream_info, name, "deadbeef" * 12, 0)
self.clock.advance(1)
self.clock.advance(1)
self.clock.advance(1)
with self.assertRaises(DownloadDataTimeout): with self.assertRaises(DownloadDataTimeout):
yield start yield start
self.assertTrue(getstream.pay_key_fee_called)
@defer.inlineCallbacks @defer.inlineCallbacks
def test_finish_one_blob(self): def test_finish_one_blob(self):
@ -150,20 +150,22 @@ class GetStreamTests(unittest.TestCase):
test that if we have 1 completed blob, start() returns test that if we have 1 completed blob, start() returns
and key fee is paid and key fee is paid
""" """
called_pay_fee = defer.Deferred()
getstream = self.init_getstream_with_mocs() getstream = self.init_getstream_with_mocs()
getstream._initialize = types.MethodType(moc_initialize, getstream) getstream._initialize = types.MethodType(moc_initialize, getstream)
getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream) getstream._download_sd_blob = types.MethodType(moc_download_sd_blob, getstream)
getstream._download = types.MethodType(moc_download, getstream) getstream._download = types.MethodType(moc_download, getstream)
getstream.pay_key_fee = types.MethodType(moc_pay_key_fee, getstream) getstream.pay_key_fee = types.MethodType(moc_pay_key_fee(called_pay_fee), getstream)
name = 'test' name = 'test'
stream_info = None stream_info = None
start = getstream.start(stream_info, name, "deadbeef" * 12, 0) self.assertFalse(getstream.wrote_data)
getstream.downloader.num_completed = 1 getstream.data_downloading_deferred.callback(None)
self.clock.advance(1) yield getstream.start(stream_info, name, "deadbeef" * 12, 0)
self.assertTrue(getstream.wrote_data)
downloader, f_deferred = yield start # self.assertTrue(getstream.pay_key_fee_called)
self.assertTrue(getstream.pay_key_fee_called)
# @defer.inlineCallbacks # @defer.inlineCallbacks
# def test_finish_stopped_downloader(self): # def test_finish_stopped_downloader(self):