From f74f075b4eb2d285cec5efd6f0242f4b4bcc8e4c Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Sun, 10 Jul 2016 17:47:36 -0500 Subject: [PATCH 1/2] add tests for BlobRequestHandler and run on travis --- .../install_dependencies_and_run_tests.sh | 4 +- tests/__init__.py | 0 tests/lbrynet/__init__.py | 0 tests/lbrynet/core/__init__.py | 0 tests/lbrynet/core/server/__init__.py | 0 .../core/server/test_BlobRequestHandler.py | 104 ++++++++++++++++++ 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 tests/__init__.py create mode 100644 tests/lbrynet/__init__.py create mode 100644 tests/lbrynet/core/__init__.py create mode 100644 tests/lbrynet/core/server/__init__.py create mode 100644 tests/lbrynet/core/server/test_BlobRequestHandler.py diff --git a/packaging/travis/install_dependencies_and_run_tests.sh b/packaging/travis/install_dependencies_and_run_tests.sh index f55ab609b..6014087e1 100755 --- a/packaging/travis/install_dependencies_and_run_tests.sh +++ b/packaging/travis/install_dependencies_and_run_tests.sh @@ -38,8 +38,8 @@ rm get-pip.py pip install -r requirements.txt -pip install nose coverage coveralls pylint -nosetests --with-coverage --cover-package=lbrynet -v -I functional_tests.py tests/ +pip install mock pylint +trial tests # TODO: submit coverage report to coveralls # TODO: as code quality improves, make pylint be more strict diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/lbrynet/__init__.py b/tests/lbrynet/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/lbrynet/core/__init__.py b/tests/lbrynet/core/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/lbrynet/core/server/__init__.py b/tests/lbrynet/core/server/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/tests/lbrynet/core/server/test_BlobRequestHandler.py b/tests/lbrynet/core/server/test_BlobRequestHandler.py new file mode 100644 index 000000000..4cd04259a --- /dev/null +++ b/tests/lbrynet/core/server/test_BlobRequestHandler.py @@ -0,0 +1,104 @@ +import mock +from twisted.internet import defer +from twisted.trial import unittest + +from lbrynet.core.server import BlobRequestHandler + + +class TestBlobRequestHandlerQueries(unittest.TestCase): + def setUp(self): + self.blob_manager = mock.Mock() + self.payment_rate_manager = mock.Mock() + self.handler = BlobRequestHandler.BlobRequestHandler( + self.blob_manager, None, self.payment_rate_manager) + + def test_empty_response_when_empty_query(self): + self.assertEqual({}, self.successResultOf(self.handler.handle_queries({}))) + + def test_error_set_when_rate_is_missing(self): + query = {'requested_blob': 'blob'} + deferred = self.handler.handle_queries(query) + response = {'incoming_blob': {'error': 'RATE_UNSET'}} + self.assertEqual(response, self.successResultOf(deferred)) + + def test_error_set_when_rate_too_low(self): + self.payment_rate_manager.accept_rate_blob_data.return_value = False + query = { + 'blob_data_payment_rate': 'way_too_low', + 'requested_blob': 'blob' + } + deferred = self.handler.handle_queries(query) + response = { + 'blob_data_payment_rate': 'RATE_TOO_LOW', + 'incoming_blob': {'error': 'RATE_UNSET'} + } + self.assertEqual(response, self.successResultOf(deferred)) + + def test_response_when_rate_too_low(self): + self.payment_rate_manager.accept_rate_blob_data.return_value = False + query = { + 'blob_data_payment_rate': 'way_too_low', + } + deferred = self.handler.handle_queries(query) + response = { + 'blob_data_payment_rate': 'RATE_TOO_LOW', + } + self.assertEqual(response, self.successResultOf(deferred)) + + def test_blob_unavailable_when_blob_not_validated(self): + self.payment_rate_manager.accept_rate_blob_data.return_value = True + blob = mock.Mock() + blob.is_validated.return_value = False + self.blob_manager.get_blob.return_value = defer.succeed(blob) + query = { + 'blob_data_payment_rate': 'rate', + 'requested_blob': 'blob' + } + deferred = self.handler.handle_queries(query) + response = { + 'blob_data_payment_rate': 'RATE_ACCEPTED', + 'incoming_blob': {'error': 'BLOB_UNAVAILABLE'} + } + self.assertEqual(response, self.successResultOf(deferred)) + + def test_blob_unavailable_when_blob_cannot_be_opened(self): + self.payment_rate_manager.accept_rate_blob_data.return_value = True + blob = mock.Mock() + blob.is_validated.return_value = True + blob.open_for_reading.return_value = None + self.blob_manager.get_blob.return_value = defer.succeed(blob) + query = { + 'blob_data_payment_rate': 'rate', + 'requested_blob': 'blob' + } + deferred = self.handler.handle_queries(query) + response = { + 'blob_data_payment_rate': 'RATE_ACCEPTED', + 'incoming_blob': {'error': 'BLOB_UNAVAILABLE'} + } + self.assertEqual(response, self.successResultOf(deferred)) + + def test_blob_details_are_set_when_all_conditions_are_met(self): + self.payment_rate_manager.accept_rate_blob_data.return_value = True + blob = mock.Mock() + blob.is_validated.return_value = True + blob.open_for_reading.return_value = True + blob.blob_hash = 'DEADBEEF' + blob.length = 42 + self.blob_manager.get_blob.return_value = defer.succeed(blob) + query = { + 'blob_data_payment_rate': 'rate', + 'requested_blob': 'blob' + } + deferred = self.handler.handle_queries(query) + response = { + 'blob_data_payment_rate': 'RATE_ACCEPTED', + 'incoming_blob': { + 'blob_hash': 'DEADBEEF', + 'length': 42 + } + } + self.assertEqual(response, self.successResultOf(deferred)) + + + From 53bf1fe4fd7da700051b8c4a75d953361cc99747 Mon Sep 17 00:00:00 2001 From: Job Evers-Meltzer Date: Sun, 10 Jul 2016 21:56:00 -0500 Subject: [PATCH 2/2] add send_blob tests --- lbrynet/core/server/BlobRequestHandler.py | 3 +- .../core/server/test_BlobRequestHandler.py | 33 ++++++++++++++++--- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lbrynet/core/server/BlobRequestHandler.py b/lbrynet/core/server/BlobRequestHandler.py index 2ce3688b4..36f67d797 100644 --- a/lbrynet/core/server/BlobRequestHandler.py +++ b/lbrynet/core/server/BlobRequestHandler.py @@ -140,6 +140,7 @@ class BlobRequestHandler(object): def set_expected_payment(): log.info("Setting expected payment") if self.blob_bytes_uploaded != 0 and self.blob_data_payment_rate is not None: + # TODO: explain why 2**20 self.wallet.add_expected_payment(self.peer, self.currently_uploading.length * 1.0 * self.blob_data_payment_rate / 2**20) @@ -156,4 +157,4 @@ class BlobRequestHandler(object): if reason is not None and isinstance(reason, Failure): log.info("Upload has failed. Reason: %s", reason.getErrorMessage()) - return _send_file() \ No newline at end of file + return _send_file() diff --git a/tests/lbrynet/core/server/test_BlobRequestHandler.py b/tests/lbrynet/core/server/test_BlobRequestHandler.py index 4cd04259a..5c55af574 100644 --- a/tests/lbrynet/core/server/test_BlobRequestHandler.py +++ b/tests/lbrynet/core/server/test_BlobRequestHandler.py @@ -1,7 +1,11 @@ +import StringIO + import mock -from twisted.internet import defer +from twisted.internet import defer, protocol +from twisted.test import proto_helpers from twisted.trial import unittest +from lbrynet.core import Peer from lbrynet.core.server import BlobRequestHandler @@ -13,8 +17,9 @@ class TestBlobRequestHandlerQueries(unittest.TestCase): self.blob_manager, None, self.payment_rate_manager) def test_empty_response_when_empty_query(self): - self.assertEqual({}, self.successResultOf(self.handler.handle_queries({}))) - + self.assertEqual( + {}, self.successResultOf(self.handler.handle_queries({}))) + def test_error_set_when_rate_is_missing(self): query = {'requested_blob': 'blob'} deferred = self.handler.handle_queries(query) @@ -99,6 +104,24 @@ class TestBlobRequestHandlerQueries(unittest.TestCase): } } self.assertEqual(response, self.successResultOf(deferred)) - - + +class TestBlobRequestHandlerSender(unittest.TestCase): + def test_nothing_happens_if_not_currently_uploading(self): + handler = BlobRequestHandler.BlobRequestHandler(None, None, None) + handler.currently_uploading = None + deferred = handler.send_blob_if_requested(None) + self.assertEqual(True, self.successResultOf(deferred)) + + def test_file_is_sent_to_consumer(self): + # TODO: also check that the expected payment values are set + consumer = proto_helpers.StringTransport() + test_file = StringIO.StringIO('test') + handler = BlobRequestHandler.BlobRequestHandler(None, None, None) + 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() + self.assertEqual(consumer.value(), 'test')