From 6389e961b9a9d2c1070411afc85563dc6e2b32b5 Mon Sep 17 00:00:00 2001 From: Jack Robison Date: Thu, 2 Aug 2018 14:34:02 -0400 Subject: [PATCH] update (and refactor) tests to not use Session --- lbrynet/tests/functional/test_reflector.py | 184 ++++++++---------- lbrynet/tests/functional/test_streamify.py | 87 +++------ .../tests/unit/database/test_SQLiteStorage.py | 29 +-- .../test_EncryptedFileCreator.py | 53 +++-- .../tests/unit/lbrynet_daemon/test_Daemon.py | 14 +- .../unit/lbrynet_daemon/test_Downloader.py | 30 ++- 6 files changed, 169 insertions(+), 228 deletions(-) diff --git a/lbrynet/tests/functional/test_reflector.py b/lbrynet/tests/functional/test_reflector.py index 082d9d74a..6cc87053b 100644 --- a/lbrynet/tests/functional/test_reflector.py +++ b/lbrynet/tests/functional/test_reflector.py @@ -1,33 +1,46 @@ -from twisted.internet import defer, threads, error +import os +from twisted.internet import defer, error from twisted.trial import unittest - -from lbrynet import conf from lbrynet.core.StreamDescriptor import get_sd_info from lbrynet import reflector from lbrynet.core import BlobManager, PeerManager -from lbrynet.core import Session from lbrynet.core import StreamDescriptor -from lbrynet.lbry_file.client import EncryptedFileOptions from lbrynet.file_manager import EncryptedFileCreator -from lbrynet.file_manager import EncryptedFileManager - +from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager +from lbrynet.core.RateLimiter import DummyRateLimiter +from lbrynet.database.storage import SQLiteStorage +from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager from lbrynet.tests import mocks from lbrynet.tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir class TestReflector(unittest.TestCase): def setUp(self): - mocks.mock_conf_settings(self) - self.session = None - self.lbry_file_manager = None - self.server_blob_manager = None self.reflector_port = None self.port = None - self.addCleanup(self.take_down_env) + mocks.mock_conf_settings(self) + self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir() + self.client_db_dir, self.client_blob_dir = mk_db_and_blob_dir() + + prm = OnlyFreePaymentsManager() wallet = mocks.Wallet() peer_manager = PeerManager.PeerManager() peer_finder = mocks.PeerFinder(5553, peer_manager, 2) - sd_identifier = StreamDescriptor.StreamDescriptorIdentifier() + + self.server_storage = SQLiteStorage(self.server_db_dir) + self.server_blob_manager = BlobManager.DiskBlobManager(self.server_blob_dir, self.server_storage) + + self.client_storage = SQLiteStorage(self.client_db_dir) + self.client_blob_manager = BlobManager.DiskBlobManager(self.client_blob_dir, self.client_storage) + + self.server_lbry_file_manager = EncryptedFileManager(peer_finder, DummyRateLimiter(), + self.server_blob_manager, wallet, prm, + self.server_storage, + StreamDescriptor.StreamDescriptorIdentifier()) + self.client_lbry_file_manager = EncryptedFileManager(peer_finder, DummyRateLimiter(), + self.client_blob_manager, wallet, prm, + self.client_storage, + StreamDescriptor.StreamDescriptorIdentifier()) self.expected_blobs = [ ( @@ -46,60 +59,18 @@ class TestReflector(unittest.TestCase): 1015056 ), ] - ## Setup reflector client classes ## - self.db_dir, self.blob_dir = mk_db_and_blob_dir() - self.session = Session.Session( - conf.settings['data_rate'], - db_dir=self.db_dir, - node_id="abcd", - peer_finder=peer_finder, - peer_manager=peer_manager, - blob_dir=self.blob_dir, - peer_port=5553, - dht_node_port=4444, - wallet=wallet, - external_ip="127.0.0.1", - dht_node=mocks.Node(), - hash_announcer=mocks.Announcer(), - ) - self.lbry_file_manager = EncryptedFileManager.EncryptedFileManager(self.session, - sd_identifier) - - ## Setup reflector server classes ## - self.server_db_dir, self.server_blob_dir = mk_db_and_blob_dir() - self.server_session = Session.Session( - conf.settings['data_rate'], - db_dir=self.server_db_dir, - node_id="abcd", - peer_finder=peer_finder, - peer_manager=peer_manager, - blob_dir=self.server_blob_dir, - peer_port=5554, - dht_node_port=4443, - wallet=wallet, - external_ip="127.0.0.1", - dht_node=mocks.Node(), - hash_announcer=mocks.Announcer(), - ) - - self.server_blob_manager = BlobManager.DiskBlobManager(self.server_blob_dir, - self.server_session.storage) - - self.server_lbry_file_manager = EncryptedFileManager.EncryptedFileManager( - self.server_session, sd_identifier) - - d = self.session.setup() - d.addCallback(lambda _: EncryptedFileOptions.add_lbry_file_to_sd_identifier(sd_identifier)) - d.addCallback(lambda _: self.lbry_file_manager.setup()) - d.addCallback(lambda _: self.server_session.setup()) + d = self.server_storage.setup() d.addCallback(lambda _: self.server_blob_manager.setup()) d.addCallback(lambda _: self.server_lbry_file_manager.setup()) + d.addCallback(lambda _: self.client_storage.setup()) + d.addCallback(lambda _: self.client_blob_manager.setup()) + d.addCallback(lambda _: self.client_lbry_file_manager.setup()) @defer.inlineCallbacks def verify_equal(sd_info, stream_hash): self.assertDictEqual(mocks.create_stream_sd_file, sd_info) - sd_hash = yield self.session.storage.get_sd_blob_hash_for_stream(stream_hash) + sd_hash = yield self.client_storage.get_sd_blob_hash_for_stream(stream_hash) defer.returnValue(sd_hash) def save_sd_blob_hash(sd_hash): @@ -108,7 +79,7 @@ class TestReflector(unittest.TestCase): def verify_stream_descriptor_file(stream_hash): self.stream_hash = stream_hash - d = get_sd_info(self.lbry_file_manager.session.storage, stream_hash, True) + d = get_sd_info(self.client_storage, stream_hash, True) d.addCallback(verify_equal, stream_hash) d.addCallback(save_sd_blob_hash) return d @@ -116,8 +87,7 @@ class TestReflector(unittest.TestCase): def create_stream(): test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) d = EncryptedFileCreator.create_lbry_file( - self.session, - self.lbry_file_manager, + self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager, "test_file", test_file, key="0123456701234567", @@ -127,9 +97,8 @@ class TestReflector(unittest.TestCase): return d def start_server(): - server_factory = reflector.ServerFactory( - peer_manager, self.server_blob_manager, - self.server_lbry_file_manager) + server_factory = reflector.ServerFactory(peer_manager, self.server_blob_manager, + self.server_lbry_file_manager) from twisted.internet import reactor port = 8943 while self.reflector_port is None: @@ -144,29 +113,31 @@ class TestReflector(unittest.TestCase): d.addCallback(lambda _: start_server()) return d - def take_down_env(self): - d = defer.succeed(True) - ## Close client classes ## - d.addCallback(lambda _: self.lbry_file_manager.stop()) - d.addCallback(lambda _: self.session.shut_down()) - - ## Close server classes ## - d.addCallback(lambda _: self.server_blob_manager.stop()) - d.addCallback(lambda _: self.server_lbry_file_manager.stop()) - d.addCallback(lambda _: self.server_session.shut_down()) - - d.addCallback(lambda _: self.reflector_port.stopListening()) - - def delete_test_env(): - try: - rm_db_and_blob_dir(self.db_dir, self.blob_dir) - rm_db_and_blob_dir(self.server_db_dir, self.server_blob_dir) - except: - raise unittest.SkipTest("TODO: fix this for windows") - - d.addCallback(lambda _: threads.deferToThread(delete_test_env)) - d.addErrback(lambda err: str(err)) - return d + @defer.inlineCallbacks + def tearDown(self): + lbry_files = self.client_lbry_file_manager.lbry_files + for lbry_file in lbry_files: + yield self.client_lbry_file_manager.delete_lbry_file(lbry_file) + yield self.client_lbry_file_manager.stop() + yield self.client_blob_manager.stop() + yield self.client_storage.stop() + self.reflector_port.stopListening() + lbry_files = self.server_lbry_file_manager.lbry_files + for lbry_file in lbry_files: + yield self.server_lbry_file_manager.delete_lbry_file(lbry_file) + yield self.server_lbry_file_manager.stop() + yield self.server_blob_manager.stop() + yield self.server_storage.stop() + try: + rm_db_and_blob_dir(self.client_db_dir, self.client_blob_dir) + except Exception as err: + raise unittest.SkipTest("TODO: fix this for windows") + try: + rm_db_and_blob_dir(self.server_db_dir, self.server_blob_dir) + except Exception as err: + raise unittest.SkipTest("TODO: fix this for windows") + if os.path.exists("test_file"): + os.remove("test_file") def test_stream_reflector(self): def verify_blob_on_reflector(): @@ -178,16 +149,15 @@ class TestReflector(unittest.TestCase): @defer.inlineCallbacks def verify_stream_on_reflector(): # check stream_info_manager has all the right information - streams = yield self.server_session.storage.get_all_streams() + streams = yield self.server_storage.get_all_streams() self.assertEqual(1, len(streams)) self.assertEqual(self.stream_hash, streams[0]) - blobs = yield self.server_session.storage.get_blobs_for_stream(self.stream_hash) + blobs = yield self.server_storage.get_blobs_for_stream(self.stream_hash) blob_hashes = [b.blob_hash for b in blobs if b.blob_hash is not None] expected_blob_hashes = [b[0] for b in self.expected_blobs[:-1] if b[0] is not None] self.assertEqual(expected_blob_hashes, blob_hashes) - sd_hash = yield self.server_session.storage.get_sd_blob_hash_for_stream(streams[0]) - expected_sd_hash = self.expected_blobs[-1][0] + sd_hash = yield self.server_storage.get_sd_blob_hash_for_stream(streams[0]) self.assertEqual(self.sd_hash, sd_hash) # check lbry file manager has the file @@ -195,14 +165,14 @@ class TestReflector(unittest.TestCase): self.assertEqual(0, len(files)) - streams = yield self.server_lbry_file_manager.storage.get_all_streams() + streams = yield self.server_storage.get_all_streams() self.assertEqual(1, len(streams)) - stream_info = yield self.server_lbry_file_manager.storage.get_stream_info(self.stream_hash) + stream_info = yield self.server_storage.get_stream_info(self.stream_hash) self.assertEqual(self.sd_hash, stream_info[3]) self.assertEqual('test_file'.encode('hex'), stream_info[0]) # check should_announce blobs on blob_manager - blob_hashes = yield self.server_blob_manager.storage.get_all_should_announce_blobs() + blob_hashes = yield self.server_storage.get_all_should_announce_blobs() self.assertSetEqual({self.sd_hash, expected_blob_hashes[0]}, set(blob_hashes)) def verify_have_blob(blob_hash, blob_size): @@ -211,7 +181,7 @@ class TestReflector(unittest.TestCase): return d def send_to_server(): - factory = reflector.ClientFactory(self.session.blob_manager, self.stream_hash, self.sd_hash) + factory = reflector.ClientFactory(self.client_blob_manager, self.stream_hash, self.sd_hash) from twisted.internet import reactor reactor.connectTCP('localhost', self.port, factory) @@ -241,7 +211,7 @@ class TestReflector(unittest.TestCase): def send_to_server(blob_hashes_to_send): factory = reflector.BlobClientFactory( - self.session.blob_manager, + self.client_blob_manager, blob_hashes_to_send ) @@ -261,10 +231,10 @@ class TestReflector(unittest.TestCase): @defer.inlineCallbacks def verify_stream_on_reflector(): # this protocol should not have any impact on stream info manager - streams = yield self.server_session.storage.get_all_streams() + streams = yield self.server_storage.get_all_streams() self.assertEqual(0, len(streams)) # there should be no should announce blobs here - blob_hashes = yield self.server_blob_manager.storage.get_all_should_announce_blobs() + blob_hashes = yield self.server_storage.get_all_should_announce_blobs() self.assertEqual(0, len(blob_hashes)) def verify_data_on_reflector(): @@ -280,7 +250,7 @@ class TestReflector(unittest.TestCase): def send_to_server(blob_hashes_to_send): factory = reflector.BlobClientFactory( - self.session.blob_manager, + self.client_blob_manager, blob_hashes_to_send ) factory.protocol_version = 0 @@ -311,20 +281,20 @@ class TestReflector(unittest.TestCase): def verify_stream_on_reflector(): # check stream_info_manager has all the right information - streams = yield self.server_session.storage.get_all_streams() + streams = yield self.server_storage.get_all_streams() self.assertEqual(1, len(streams)) self.assertEqual(self.stream_hash, streams[0]) - blobs = yield self.server_session.storage.get_blobs_for_stream(self.stream_hash) + blobs = yield self.server_storage.get_blobs_for_stream(self.stream_hash) blob_hashes = [b.blob_hash for b in blobs if b.blob_hash is not None] expected_blob_hashes = [b[0] for b in self.expected_blobs[:-1] if b[0] is not None] self.assertEqual(expected_blob_hashes, blob_hashes) - sd_hash = yield self.server_session.storage.get_sd_blob_hash_for_stream( + sd_hash = yield self.server_storage.get_sd_blob_hash_for_stream( self.stream_hash) self.assertEqual(self.sd_hash, sd_hash) # check should_announce blobs on blob_manager - to_announce = yield self.server_blob_manager.storage.get_all_should_announce_blobs() + to_announce = yield self.server_storage.get_all_should_announce_blobs() self.assertSetEqual(set(to_announce), {self.sd_hash, expected_blob_hashes[0]}) def verify_have_blob(blob_hash, blob_size): @@ -334,7 +304,7 @@ class TestReflector(unittest.TestCase): def send_to_server_as_blobs(blob_hashes_to_send): factory = reflector.BlobClientFactory( - self.session.blob_manager, + self.client_blob_manager, blob_hashes_to_send ) factory.protocol_version = 0 @@ -344,7 +314,7 @@ class TestReflector(unittest.TestCase): return factory.finished_deferred def send_to_server_as_stream(result): - factory = reflector.ClientFactory(self.session.blob_manager, self.stream_hash, self.sd_hash) + factory = reflector.ClientFactory(self.client_blob_manager, self.stream_hash, self.sd_hash) from twisted.internet import reactor reactor.connectTCP('localhost', self.port, factory) diff --git a/lbrynet/tests/functional/test_streamify.py b/lbrynet/tests/functional/test_streamify.py index 566427bd3..e8fcbaf67 100644 --- a/lbrynet/tests/functional/test_streamify.py +++ b/lbrynet/tests/functional/test_streamify.py @@ -1,21 +1,18 @@ import os import shutil import tempfile - from hashlib import md5 from twisted.trial.unittest import TestCase from twisted.internet import defer, threads - -from lbrynet import conf -from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager -from lbrynet.core.Session import Session from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier -from lbrynet.file_manager.EncryptedFileCreator import create_lbry_file -from lbrynet.lbry_file.client.EncryptedFileOptions import add_lbry_file_to_sd_identifier +from lbrynet.core.BlobManager import DiskBlobManager from lbrynet.core.StreamDescriptor import get_sd_info from lbrynet.core.PeerManager import PeerManager from lbrynet.core.RateLimiter import DummyRateLimiter - +from lbrynet.database.storage import SQLiteStorage +from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager +from lbrynet.file_manager.EncryptedFileCreator import create_lbry_file +from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager from lbrynet.tests import mocks @@ -38,47 +35,40 @@ class TestStreamify(TestCase): self.is_generous = True self.db_dir = tempfile.mkdtemp() self.blob_dir = os.path.join(self.db_dir, "blobfiles") + os.mkdir(self.blob_dir) self.dht_node = FakeNode() self.wallet = FakeWallet() self.peer_manager = PeerManager() self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2) self.rate_limiter = DummyRateLimiter() self.sd_identifier = StreamDescriptorIdentifier() - os.mkdir(self.blob_dir) + self.storage = SQLiteStorage(self.db_dir) + self.blob_manager = DiskBlobManager(self.blob_dir, self.storage, self.dht_node._dataStore) + self.prm = OnlyFreePaymentsManager() + self.lbry_file_manager = EncryptedFileManager(self.peer_finder, self.rate_limiter, self.blob_manager, + self.wallet, self.prm, self.storage, self.sd_identifier) + d = self.storage.setup() + d.addCallback(lambda _: self.lbry_file_manager.setup()) + return d @defer.inlineCallbacks def tearDown(self): lbry_files = self.lbry_file_manager.lbry_files for lbry_file in lbry_files: yield self.lbry_file_manager.delete_lbry_file(lbry_file) - if self.lbry_file_manager is not None: - yield self.lbry_file_manager.stop() - if self.session is not None: - yield self.session.shut_down() - yield self.session.storage.stop() + yield self.lbry_file_manager.stop() + yield self.storage.stop() yield threads.deferToThread(shutil.rmtree, self.db_dir) if os.path.exists("test_file"): os.remove("test_file") def test_create_stream(self): - self.session = Session( - conf.ADJUSTABLE_SETTINGS['data_rate'][1], db_dir=self.db_dir, node_id="abcd", peer_finder=self.peer_finder, - blob_dir=self.blob_dir, peer_port=5553, rate_limiter=self.rate_limiter, wallet=self.wallet, - external_ip="127.0.0.1", dht_node=self.dht_node - ) - - self.lbry_file_manager = EncryptedFileManager(self.session, self.sd_identifier) - - d = self.session.setup() - d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier)) - d.addCallback(lambda _: self.lbry_file_manager.setup()) - def verify_equal(sd_info): self.assertEqual(sd_info, test_create_stream_sd_file) def verify_stream_descriptor_file(stream_hash): - d = get_sd_info(self.session.storage, stream_hash, True) + d = get_sd_info(self.storage, stream_hash, True) d.addCallback(verify_equal) return d @@ -90,39 +80,24 @@ class TestStreamify(TestCase): def create_stream(): test_file = GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) - d = create_lbry_file(self.session, self.lbry_file_manager, "test_file", test_file, - key="0123456701234567", iv_generator=iv_generator()) + d = create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager, "test_file", + test_file, key="0123456701234567", iv_generator=iv_generator()) d.addCallback(lambda lbry_file: lbry_file.stream_hash) return d - d.addCallback(lambda _: create_stream()) + d = create_stream() d.addCallback(verify_stream_descriptor_file) return d + @defer.inlineCallbacks def test_create_and_combine_stream(self): - - self.session = Session( - conf.ADJUSTABLE_SETTINGS['data_rate'][1], db_dir=self.db_dir, node_id="abcd", peer_finder=self.peer_finder, - blob_dir=self.blob_dir, peer_port=5553, rate_limiter=self.rate_limiter, wallet=self.wallet, - external_ip="127.0.0.1", dht_node=self.dht_node - ) - - self.lbry_file_manager = EncryptedFileManager(self.session, self.sd_identifier) - - @defer.inlineCallbacks - def create_stream(): - test_file = GenFile(53209343, b''.join([chr(i + 5) for i in xrange(0, 64, 6)])) - lbry_file = yield create_lbry_file(self.session, self.lbry_file_manager, "test_file", test_file) - sd_hash = yield self.session.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash) - self.assertTrue(lbry_file.sd_hash, sd_hash) - yield lbry_file.start() - f = open('test_file') - hashsum = md5() - hashsum.update(f.read()) - self.assertEqual(hashsum.hexdigest(), "68959747edc73df45e45db6379dd7b3b") - - d = self.session.setup() - d.addCallback(lambda _: add_lbry_file_to_sd_identifier(self.sd_identifier)) - d.addCallback(lambda _: self.lbry_file_manager.setup()) - d.addCallback(lambda _: create_stream()) - return d + test_file = GenFile(53209343, b''.join([chr(i + 5) for i in xrange(0, 64, 6)])) + lbry_file = yield create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager, + "test_file", test_file) + sd_hash = yield self.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash) + self.assertTrue(lbry_file.sd_hash, sd_hash) + yield lbry_file.start() + f = open('test_file') + hashsum = md5() + hashsum.update(f.read()) + self.assertEqual(hashsum.hexdigest(), "68959747edc73df45e45db6379dd7b3b") diff --git a/lbrynet/tests/unit/database/test_SQLiteStorage.py b/lbrynet/tests/unit/database/test_SQLiteStorage.py index 0e5328813..06dbec21b 100644 --- a/lbrynet/tests/unit/database/test_SQLiteStorage.py +++ b/lbrynet/tests/unit/database/test_SQLiteStorage.py @@ -7,9 +7,7 @@ from twisted.internet import defer from twisted.trial import unittest from lbrynet import conf from lbrynet.database.storage import SQLiteStorage, open_file_for_writing -from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader -from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager from lbrynet.tests.util import random_lbry_hash log = logging.getLogger() @@ -67,7 +65,6 @@ fake_claim_info = { } - class FakeAnnouncer(object): def __init__(self): self._queue_size = 0 @@ -245,12 +242,8 @@ class FileStorageTests(StorageTest): @defer.inlineCallbacks def test_store_file(self): - session = MocSession(self.storage) - session.db_dir = self.db_dir - sd_identifier = StreamDescriptorIdentifier() download_directory = self.db_dir - manager = EncryptedFileManager(session, sd_identifier) - out = yield manager.session.storage.get_all_lbry_files() + out = yield self.storage.get_all_lbry_files() self.assertEqual(len(out), 0) stream_hash = random_lbry_hash() @@ -268,33 +261,29 @@ class FileStorageTests(StorageTest): blob_data_rate = 0 file_name = "test file" - out = yield manager.session.storage.save_published_file( + out = yield self.storage.save_published_file( stream_hash, file_name, download_directory, blob_data_rate ) - rowid = yield manager.session.storage.get_rowid_for_stream_hash(stream_hash) + rowid = yield self.storage.get_rowid_for_stream_hash(stream_hash) self.assertEqual(out, rowid) - files = yield manager.session.storage.get_all_lbry_files() + files = yield self.storage.get_all_lbry_files() self.assertEqual(1, len(files)) - status = yield manager.session.storage.get_lbry_file_status(rowid) + status = yield self.storage.get_lbry_file_status(rowid) self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_STOPPED) running = ManagedEncryptedFileDownloader.STATUS_RUNNING - yield manager.session.storage.change_file_status(rowid, running) - status = yield manager.session.storage.get_lbry_file_status(rowid) + yield self.storage.change_file_status(rowid, running) + status = yield self.storage.get_lbry_file_status(rowid) self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_RUNNING) class ContentClaimStorageTests(StorageTest): @defer.inlineCallbacks def test_store_content_claim(self): - session = MocSession(self.storage) - session.db_dir = self.db_dir - sd_identifier = StreamDescriptorIdentifier() download_directory = self.db_dir - manager = EncryptedFileManager(session, sd_identifier) - out = yield manager.session.storage.get_all_lbry_files() + out = yield self.storage.get_all_lbry_files() self.assertEqual(len(out), 0) stream_hash = random_lbry_hash() @@ -307,7 +296,7 @@ class ContentClaimStorageTests(StorageTest): yield self.make_and_store_fake_stream(blob_count=2, stream_hash=stream_hash, sd_hash=sd_hash) blob_data_rate = 0 file_name = "test file" - yield manager.session.storage.save_published_file( + yield self.storage.save_published_file( stream_hash, file_name, download_directory, blob_data_rate ) yield self.storage.save_claims([fake_claim_info]) diff --git a/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py b/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py index 6a4dcc8fd..29dfddc9b 100644 --- a/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py +++ b/lbrynet/tests/unit/lbryfilemanager/test_EncryptedFileCreator.py @@ -1,18 +1,29 @@ # -*- coding: utf-8 -*- from cryptography.hazmat.primitives.ciphers.algorithms import AES -import mock from twisted.trial import unittest from twisted.internet import defer -from lbrynet.database.storage import SQLiteStorage from lbrynet.core.StreamDescriptor import get_sd_info, BlobStreamDescriptorReader -from lbrynet.core import BlobManager -from lbrynet.core import Session +from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier +from lbrynet.core.BlobManager import DiskBlobManager +from lbrynet.core.PeerManager import PeerManager +from lbrynet.core.RateLimiter import DummyRateLimiter +from lbrynet.core.PaymentRateManager import OnlyFreePaymentsManager +from lbrynet.database.storage import SQLiteStorage from lbrynet.file_manager import EncryptedFileCreator -from lbrynet.file_manager import EncryptedFileManager +from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager from lbrynet.tests import mocks from lbrynet.tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir + +FakeNode = mocks.Node +FakeWallet = mocks.Wallet +FakePeerFinder = mocks.PeerFinder +FakeAnnouncer = mocks.Announcer +GenFile = mocks.GenFile +test_create_stream_sd_file = mocks.create_stream_sd_file +DummyBlobAvailabilityTracker = mocks.BlobAvailabilityTracker + MB = 2**20 @@ -24,31 +35,35 @@ def iv_generator(): class CreateEncryptedFileTest(unittest.TestCase): timeout = 5 - @defer.inlineCallbacks def setUp(self): mocks.mock_conf_settings(self) self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir() - - self.session = mock.Mock(spec=Session.Session)(None, None) - self.session.payment_rate_manager.min_blob_data_payment_rate = 0 - self.blob_manager = BlobManager.DiskBlobManager(self.tmp_blob_dir, SQLiteStorage(self.tmp_db_dir)) - self.session.blob_manager = self.blob_manager - self.session.storage = self.session.blob_manager.storage - self.file_manager = EncryptedFileManager.EncryptedFileManager(self.session, object()) - yield self.session.blob_manager.storage.setup() - yield self.session.blob_manager.setup() + self.wallet = FakeWallet() + self.peer_manager = PeerManager() + self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2) + self.rate_limiter = DummyRateLimiter() + self.sd_identifier = StreamDescriptorIdentifier() + self.storage = SQLiteStorage(self.tmp_db_dir) + self.blob_manager = DiskBlobManager(self.tmp_blob_dir, self.storage) + self.prm = OnlyFreePaymentsManager() + self.lbry_file_manager = EncryptedFileManager(self.peer_finder, self.rate_limiter, self.blob_manager, + self.wallet, self.prm, self.storage, self.sd_identifier) + d = self.storage.setup() + d.addCallback(lambda _: self.lbry_file_manager.setup()) + return d @defer.inlineCallbacks def tearDown(self): + yield self.lbry_file_manager.stop() yield self.blob_manager.stop() - yield self.session.storage.stop() + yield self.storage.stop() rm_db_and_blob_dir(self.tmp_db_dir, self.tmp_blob_dir) @defer.inlineCallbacks def create_file(self, filename): handle = mocks.GenFile(3*MB, '1') key = '2' * (AES.block_size / 8) - out = yield EncryptedFileCreator.create_lbry_file(self.session, self.file_manager, filename, handle, + out = yield EncryptedFileCreator.create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager, filename, handle, key, iv_generator()) defer.returnValue(out) @@ -60,7 +75,7 @@ class CreateEncryptedFileTest(unittest.TestCase): "c8728fe0534dd06fbcacae92b0891787ad9b68ffc8d20c1" filename = 'test.file' lbry_file = yield self.create_file(filename) - sd_hash = yield self.session.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash) + sd_hash = yield self.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash) # read the sd blob file sd_blob = self.blob_manager.blobs[sd_hash] @@ -68,7 +83,7 @@ class CreateEncryptedFileTest(unittest.TestCase): sd_file_info = yield sd_reader.get_info() # this comes from the database, the blobs returned are sorted - sd_info = yield get_sd_info(self.session.storage, lbry_file.stream_hash, include_blobs=True) + sd_info = yield get_sd_info(self.storage, lbry_file.stream_hash, include_blobs=True) self.assertDictEqual(sd_info, sd_file_info) self.assertListEqual(sd_info['blobs'], sd_file_info['blobs']) self.assertEqual(sd_info['stream_hash'], expected_stream_hash) diff --git a/lbrynet/tests/unit/lbrynet_daemon/test_Daemon.py b/lbrynet/tests/unit/lbrynet_daemon/test_Daemon.py index 8722611a5..17a82a94f 100644 --- a/lbrynet/tests/unit/lbrynet_daemon/test_Daemon.py +++ b/lbrynet/tests/unit/lbrynet_daemon/test_Daemon.py @@ -11,18 +11,17 @@ from faker import Faker from lbryschema.decode import smart_decode from lbryum.wallet import NewWallet from lbrynet import conf -from lbrynet.core import Session, PaymentRateManager, Wallet +from lbrynet.core import Wallet from lbrynet.database.storage import SQLiteStorage from lbrynet.daemon.ComponentManager import ComponentManager from lbrynet.daemon.Components import DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT, STREAM_IDENTIFIER_COMPONENT -from lbrynet.daemon.Components import HASH_ANNOUNCER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT, SESSION_COMPONENT +from lbrynet.daemon.Components import HASH_ANNOUNCER_COMPONENT, REFLECTOR_COMPONENT, UPNP_COMPONENT from lbrynet.daemon.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT from lbrynet.daemon.Daemon import Daemon as LBRYDaemon from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader from lbrynet.tests import util from lbrynet.tests.mocks import mock_conf_settings, FakeNetwork, FakeFileManager -from lbrynet.tests.mocks import BlobAvailabilityTracker as DummyBlobAvailabilityTracker from lbrynet.tests.mocks import ExchangeRateManager as DummyExchangeRateManager from lbrynet.tests.mocks import BTCLBCFeed, USDBTCFeed from lbrynet.tests.util import is_android @@ -41,18 +40,13 @@ def get_test_daemon(data_rate=None, generous=True, with_fee=False): 'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2} } daemon = LBRYDaemon(None) - daemon.session = mock.Mock(spec=Session.Session) daemon.wallet = mock.Mock(spec=Wallet.LBRYumWallet) daemon.wallet.wallet = mock.Mock(spec=NewWallet) daemon.wallet.wallet.use_encryption = False daemon.wallet.network = FakeNetwork() - daemon.session.storage = mock.Mock(spec=SQLiteStorage) + daemon.storage = mock.Mock(spec=SQLiteStorage) market_feeds = [BTCLBCFeed(), USDBTCFeed()] daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates) - base_prm = PaymentRateManager.BasePaymentRateManager(rate=data_rate) - prm = PaymentRateManager.NegotiatedPaymentRateManager(base_prm, DummyBlobAvailabilityTracker(), - generous=generous) - daemon.session.payment_rate_manager = prm metadata = { "author": "fake author", @@ -146,7 +140,7 @@ class TestFileListSorting(unittest.TestCase): self.faker.seed(66410) self.test_daemon = get_test_daemon() component_manager = ComponentManager( - skip_components=[DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT, SESSION_COMPONENT, UPNP_COMPONENT, + skip_components=[DATABASE_COMPONENT, DHT_COMPONENT, WALLET_COMPONENT, UPNP_COMPONENT, PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT, HASH_ANNOUNCER_COMPONENT, STREAM_IDENTIFIER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT], file_manager=FakeFileManager diff --git a/lbrynet/tests/unit/lbrynet_daemon/test_Downloader.py b/lbrynet/tests/unit/lbrynet_daemon/test_Downloader.py index 43ec70a6f..7e62e9eaf 100644 --- a/lbrynet/tests/unit/lbrynet_daemon/test_Downloader.py +++ b/lbrynet/tests/unit/lbrynet_daemon/test_Downloader.py @@ -3,16 +3,18 @@ import mock from twisted.trial import unittest from twisted.internet import defer, task -from lbrynet.core import Session, PaymentRateManager, Wallet +from lbrynet.core import PaymentRateManager, Wallet from lbrynet.core.Error import DownloadDataTimeout, DownloadSDTimeout from lbrynet.daemon import Downloader from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier - +from lbrynet.database.storage import SQLiteStorage +from lbrynet.core.BlobManager import DiskBlobManager +from lbrynet.dht.peerfinder import DummyPeerFinder +from lbrynet.core.RateLimiter import DummyRateLimiter from lbrynet.file_manager.EncryptedFileStatusReport import EncryptedFileStatusReport from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager -from lbrynet.tests.mocks import ExchangeRateManager as DummyExchangeRateManager from lbrynet.tests.mocks import mock_conf_settings @@ -61,25 +63,21 @@ def moc_pay_key_fee(self, key_fee, name): class GetStreamTests(unittest.TestCase): - def init_getstream_with_mocs(self): mock_conf_settings(self) - sd_identifier = mock.Mock(spec=StreamDescriptorIdentifier) - session = mock.Mock(spec=Session.Session) - session.wallet = mock.Mock(spec=Wallet.LBRYumWallet) + wallet = mock.Mock(spec=Wallet.LBRYumWallet) prm = mock.Mock(spec=PaymentRateManager.NegotiatedPaymentRateManager) - session.payment_rate_manager = prm - market_feeds = [] - rates = {} - exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates) exchange_rate_manager = mock.Mock(spec=ExchangeRateManager) - max_key_fee = {'currency':"LBC", 'amount':10, 'address':''} + storage = mock.Mock(spec=SQLiteStorage) + peer_finder = DummyPeerFinder() + blob_manager = mock.Mock(spec=DiskBlobManager) + max_key_fee = {'currency': "LBC", 'amount': 10, 'address': ''} disable_max_key_fee = False - data_rate = {'currency':"LBC", 'amount':0, 'address':''} - - getstream = Downloader.GetStream(sd_identifier, session, - exchange_rate_manager, max_key_fee, disable_max_key_fee, timeout=3, data_rate=data_rate) + data_rate = {'currency': "LBC", 'amount': 0, 'address': ''} + 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) getstream.pay_key_fee_called = False self.clock = task.Clock()