2018-07-17 05:32:37 +02:00
|
|
|
from io import BytesIO
|
2018-11-25 17:12:30 +01:00
|
|
|
from unittest import mock
|
2016-07-11 04:56:00 +02:00
|
|
|
|
2016-10-13 19:35:55 +02:00
|
|
|
from twisted.internet import defer
|
2016-07-11 04:56:00 +02:00
|
|
|
from twisted.test import proto_helpers
|
2016-07-11 00:47:36 +02:00
|
|
|
from twisted.trial import unittest
|
|
|
|
|
2018-11-04 20:06:29 +01:00
|
|
|
from lbrynet.p2p import Peer
|
|
|
|
from lbrynet.p2p.PaymentRateManager import NegotiatedPaymentRateManager, BasePaymentRateManager
|
2019-01-21 21:55:50 +01:00
|
|
|
from lbrynet.conf import Config
|
|
|
|
from tests.mocks import BlobAvailabilityTracker as DummyBlobAvailabilityTracker
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestBlobRequestHandlerQueries(unittest.TestCase):
|
|
|
|
def setUp(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
conf = Config()
|
2016-07-11 00:47:36 +02:00
|
|
|
self.blob_manager = mock.Mock()
|
2016-09-30 06:26:27 +02:00
|
|
|
self.payment_rate_manager = NegotiatedPaymentRateManager(
|
2019-01-21 21:55:50 +01:00
|
|
|
BasePaymentRateManager(0.001, conf.min_info_rate), DummyBlobAvailabilityTracker(), conf.is_generous_host
|
|
|
|
)
|
2019-01-07 08:52:53 +01:00
|
|
|
from lbrynet.p2p.server import BlobRequestHandler
|
2016-09-30 06:26:27 +02:00
|
|
|
self.handler = BlobRequestHandler.BlobRequestHandler(
|
|
|
|
self.blob_manager, None, self.payment_rate_manager, None)
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_empty_response_when_empty_query(self):
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual({}, self.handler.handle_queries({}))
|
2016-07-11 04:56:00 +02:00
|
|
|
|
2016-07-11 00:47:36 +02:00
|
|
|
def test_error_set_when_rate_is_missing(self):
|
|
|
|
query = {'requested_blob': 'blob'}
|
|
|
|
response = {'incoming_blob': {'error': 'RATE_UNSET'}}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_error_set_when_rate_too_low(self):
|
|
|
|
query = {
|
2018-07-22 03:12:33 +02:00
|
|
|
'blob_data_payment_rate': -1.0,
|
2016-07-11 00:47:36 +02:00
|
|
|
'requested_blob': 'blob'
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
'blob_data_payment_rate': 'RATE_TOO_LOW',
|
|
|
|
'incoming_blob': {'error': 'RATE_UNSET'}
|
|
|
|
}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_response_when_rate_too_low(self):
|
|
|
|
query = {
|
2018-07-22 03:12:33 +02:00
|
|
|
'blob_data_payment_rate': -1.0,
|
2016-07-11 00:47:36 +02:00
|
|
|
}
|
|
|
|
response = {
|
|
|
|
'blob_data_payment_rate': 'RATE_TOO_LOW',
|
|
|
|
}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_blob_unavailable_when_blob_not_validated(self):
|
|
|
|
blob = mock.Mock()
|
2017-09-20 20:02:34 +02:00
|
|
|
blob.get_is_verified.return_value = False
|
2016-07-11 00:47:36 +02:00
|
|
|
self.blob_manager.get_blob.return_value = defer.succeed(blob)
|
|
|
|
query = {
|
2016-10-03 08:44:58 +02:00
|
|
|
'blob_data_payment_rate': 1.0,
|
2016-07-11 00:47:36 +02:00
|
|
|
'requested_blob': 'blob'
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
'blob_data_payment_rate': 'RATE_ACCEPTED',
|
|
|
|
'incoming_blob': {'error': 'BLOB_UNAVAILABLE'}
|
|
|
|
}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_blob_unavailable_when_blob_cannot_be_opened(self):
|
|
|
|
blob = mock.Mock()
|
2017-09-20 20:02:34 +02:00
|
|
|
blob.get_is_verified.return_value = True
|
2016-07-11 00:47:36 +02:00
|
|
|
blob.open_for_reading.return_value = None
|
|
|
|
self.blob_manager.get_blob.return_value = defer.succeed(blob)
|
|
|
|
query = {
|
2016-10-03 08:44:58 +02:00
|
|
|
'blob_data_payment_rate': 0.0,
|
2016-07-11 00:47:36 +02:00
|
|
|
'requested_blob': 'blob'
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
'blob_data_payment_rate': 'RATE_ACCEPTED',
|
|
|
|
'incoming_blob': {'error': 'BLOB_UNAVAILABLE'}
|
|
|
|
}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
|
|
|
def test_blob_details_are_set_when_all_conditions_are_met(self):
|
|
|
|
blob = mock.Mock()
|
2017-09-20 20:02:34 +02:00
|
|
|
blob.get_is_verified.return_value = True
|
2016-07-11 00:47:36 +02:00
|
|
|
blob.open_for_reading.return_value = True
|
|
|
|
blob.blob_hash = 'DEADBEEF'
|
|
|
|
blob.length = 42
|
2016-10-03 08:44:58 +02:00
|
|
|
peer = mock.Mock()
|
|
|
|
peer.host = "1.2.3.4"
|
|
|
|
self.handler.peer = peer
|
2016-07-11 00:47:36 +02:00
|
|
|
self.blob_manager.get_blob.return_value = defer.succeed(blob)
|
|
|
|
query = {
|
2016-10-03 08:44:58 +02:00
|
|
|
'blob_data_payment_rate': 1.0,
|
2016-07-11 00:47:36 +02:00
|
|
|
'requested_blob': 'blob'
|
|
|
|
}
|
|
|
|
response = {
|
|
|
|
'blob_data_payment_rate': 'RATE_ACCEPTED',
|
|
|
|
'incoming_blob': {
|
|
|
|
'blob_hash': 'DEADBEEF',
|
|
|
|
'length': 42
|
|
|
|
}
|
|
|
|
}
|
2019-01-07 08:52:53 +01:00
|
|
|
self.assertEqual(response, self.handler.handle_queries(query))
|
2016-07-11 00:47:36 +02:00
|
|
|
|
2016-07-11 04:56:00 +02:00
|
|
|
|
|
|
|
class TestBlobRequestHandlerSender(unittest.TestCase):
|
|
|
|
def test_nothing_happens_if_not_currently_uploading(self):
|
2019-01-07 08:52:53 +01:00
|
|
|
from lbrynet.p2p.server import BlobRequestHandler
|
2016-09-30 06:26:27 +02:00
|
|
|
handler = BlobRequestHandler.BlobRequestHandler(None, None, None, None)
|
2016-07-11 04:56:00 +02:00
|
|
|
handler.currently_uploading = None
|
|
|
|
deferred = handler.send_blob_if_requested(None)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertTrue(self.successResultOf(deferred))
|
2016-07-11 04:56:00 +02:00
|
|
|
|
|
|
|
def test_file_is_sent_to_consumer(self):
|
|
|
|
# TODO: also check that the expected payment values are set
|
|
|
|
consumer = proto_helpers.StringTransport()
|
2018-07-17 05:32:37 +02:00
|
|
|
test_file = BytesIO(b'test')
|
2019-01-07 08:52:53 +01:00
|
|
|
from lbrynet.p2p.server import BlobRequestHandler
|
2017-04-26 20:15:38 +02:00
|
|
|
handler = BlobRequestHandler.BlobRequestHandler(None, None, None, None)
|
2016-07-11 04:56:00 +02:00
|
|
|
handler.peer = mock.create_autospec(Peer.Peer)
|
|
|
|
handler.currently_uploading = mock.Mock()
|
|
|
|
handler.read_handle = test_file
|
|
|
|
handler.send_blob_if_requested(consumer)
|
|
|
|
while consumer.producer:
|
|
|
|
consumer.producer.resumeProducing()
|
2018-07-22 03:12:33 +02:00
|
|
|
self.assertEqual(consumer.value(), b'test')
|