update (and refactor) tests to not use Session

This commit is contained in:
Jack Robison 2018-08-02 14:34:02 -04:00
parent 6359fc608e
commit 6389e961b9
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
6 changed files with 169 additions and 228 deletions

View file

@ -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 twisted.trial import unittest
from lbrynet import conf
from lbrynet.core.StreamDescriptor import get_sd_info from lbrynet.core.StreamDescriptor import get_sd_info
from lbrynet import reflector from lbrynet import reflector
from lbrynet.core import BlobManager, PeerManager from lbrynet.core import BlobManager, PeerManager
from lbrynet.core import Session
from lbrynet.core import StreamDescriptor from lbrynet.core import StreamDescriptor
from lbrynet.lbry_file.client import EncryptedFileOptions
from lbrynet.file_manager import EncryptedFileCreator 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 import mocks
from lbrynet.tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir from lbrynet.tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir
class TestReflector(unittest.TestCase): class TestReflector(unittest.TestCase):
def setUp(self): 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.reflector_port = None
self.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() wallet = mocks.Wallet()
peer_manager = PeerManager.PeerManager() peer_manager = PeerManager.PeerManager()
peer_finder = mocks.PeerFinder(5553, peer_manager, 2) 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 = [ self.expected_blobs = [
( (
@ -46,60 +59,18 @@ class TestReflector(unittest.TestCase):
1015056 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, d = self.server_storage.setup()
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.addCallback(lambda _: self.server_blob_manager.setup()) d.addCallback(lambda _: self.server_blob_manager.setup())
d.addCallback(lambda _: self.server_lbry_file_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 @defer.inlineCallbacks
def verify_equal(sd_info, stream_hash): def verify_equal(sd_info, stream_hash):
self.assertDictEqual(mocks.create_stream_sd_file, sd_info) 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) defer.returnValue(sd_hash)
def save_sd_blob_hash(sd_hash): def save_sd_blob_hash(sd_hash):
@ -108,7 +79,7 @@ class TestReflector(unittest.TestCase):
def verify_stream_descriptor_file(stream_hash): def verify_stream_descriptor_file(stream_hash):
self.stream_hash = 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(verify_equal, stream_hash)
d.addCallback(save_sd_blob_hash) d.addCallback(save_sd_blob_hash)
return d return d
@ -116,8 +87,7 @@ class TestReflector(unittest.TestCase):
def create_stream(): def create_stream():
test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) test_file = mocks.GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)]))
d = EncryptedFileCreator.create_lbry_file( d = EncryptedFileCreator.create_lbry_file(
self.session, self.client_blob_manager, self.client_storage, prm, self.client_lbry_file_manager,
self.lbry_file_manager,
"test_file", "test_file",
test_file, test_file,
key="0123456701234567", key="0123456701234567",
@ -127,9 +97,8 @@ class TestReflector(unittest.TestCase):
return d return d
def start_server(): def start_server():
server_factory = reflector.ServerFactory( server_factory = reflector.ServerFactory(peer_manager, self.server_blob_manager,
peer_manager, self.server_blob_manager, self.server_lbry_file_manager)
self.server_lbry_file_manager)
from twisted.internet import reactor from twisted.internet import reactor
port = 8943 port = 8943
while self.reflector_port is None: while self.reflector_port is None:
@ -144,29 +113,31 @@ class TestReflector(unittest.TestCase):
d.addCallback(lambda _: start_server()) d.addCallback(lambda _: start_server())
return d return d
def take_down_env(self): @defer.inlineCallbacks
d = defer.succeed(True) def tearDown(self):
## Close client classes ## lbry_files = self.client_lbry_file_manager.lbry_files
d.addCallback(lambda _: self.lbry_file_manager.stop()) for lbry_file in lbry_files:
d.addCallback(lambda _: self.session.shut_down()) yield self.client_lbry_file_manager.delete_lbry_file(lbry_file)
yield self.client_lbry_file_manager.stop()
## Close server classes ## yield self.client_blob_manager.stop()
d.addCallback(lambda _: self.server_blob_manager.stop()) yield self.client_storage.stop()
d.addCallback(lambda _: self.server_lbry_file_manager.stop()) self.reflector_port.stopListening()
d.addCallback(lambda _: self.server_session.shut_down()) lbry_files = self.server_lbry_file_manager.lbry_files
for lbry_file in lbry_files:
d.addCallback(lambda _: self.reflector_port.stopListening()) yield self.server_lbry_file_manager.delete_lbry_file(lbry_file)
yield self.server_lbry_file_manager.stop()
def delete_test_env(): yield self.server_blob_manager.stop()
try: yield self.server_storage.stop()
rm_db_and_blob_dir(self.db_dir, self.blob_dir) try:
rm_db_and_blob_dir(self.server_db_dir, self.server_blob_dir) rm_db_and_blob_dir(self.client_db_dir, self.client_blob_dir)
except: except Exception as err:
raise unittest.SkipTest("TODO: fix this for windows") raise unittest.SkipTest("TODO: fix this for windows")
try:
d.addCallback(lambda _: threads.deferToThread(delete_test_env)) rm_db_and_blob_dir(self.server_db_dir, self.server_blob_dir)
d.addErrback(lambda err: str(err)) except Exception as err:
return d raise unittest.SkipTest("TODO: fix this for windows")
if os.path.exists("test_file"):
os.remove("test_file")
def test_stream_reflector(self): def test_stream_reflector(self):
def verify_blob_on_reflector(): def verify_blob_on_reflector():
@ -178,16 +149,15 @@ class TestReflector(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def verify_stream_on_reflector(): def verify_stream_on_reflector():
# check stream_info_manager has all the right information # 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(1, len(streams))
self.assertEqual(self.stream_hash, streams[0]) 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] 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] 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) self.assertEqual(expected_blob_hashes, blob_hashes)
sd_hash = yield self.server_session.storage.get_sd_blob_hash_for_stream(streams[0]) sd_hash = yield self.server_storage.get_sd_blob_hash_for_stream(streams[0])
expected_sd_hash = self.expected_blobs[-1][0]
self.assertEqual(self.sd_hash, sd_hash) self.assertEqual(self.sd_hash, sd_hash)
# check lbry file manager has the file # check lbry file manager has the file
@ -195,14 +165,14 @@ class TestReflector(unittest.TestCase):
self.assertEqual(0, len(files)) 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)) 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(self.sd_hash, stream_info[3])
self.assertEqual('test_file'.encode('hex'), stream_info[0]) self.assertEqual('test_file'.encode('hex'), stream_info[0])
# check should_announce blobs on blob_manager # 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)) self.assertSetEqual({self.sd_hash, expected_blob_hashes[0]}, set(blob_hashes))
def verify_have_blob(blob_hash, blob_size): def verify_have_blob(blob_hash, blob_size):
@ -211,7 +181,7 @@ class TestReflector(unittest.TestCase):
return d return d
def send_to_server(): 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 from twisted.internet import reactor
reactor.connectTCP('localhost', self.port, factory) reactor.connectTCP('localhost', self.port, factory)
@ -241,7 +211,7 @@ class TestReflector(unittest.TestCase):
def send_to_server(blob_hashes_to_send): def send_to_server(blob_hashes_to_send):
factory = reflector.BlobClientFactory( factory = reflector.BlobClientFactory(
self.session.blob_manager, self.client_blob_manager,
blob_hashes_to_send blob_hashes_to_send
) )
@ -261,10 +231,10 @@ class TestReflector(unittest.TestCase):
@defer.inlineCallbacks @defer.inlineCallbacks
def verify_stream_on_reflector(): def verify_stream_on_reflector():
# this protocol should not have any impact on stream info manager # 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)) self.assertEqual(0, len(streams))
# there should be no should announce blobs here # 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)) self.assertEqual(0, len(blob_hashes))
def verify_data_on_reflector(): def verify_data_on_reflector():
@ -280,7 +250,7 @@ class TestReflector(unittest.TestCase):
def send_to_server(blob_hashes_to_send): def send_to_server(blob_hashes_to_send):
factory = reflector.BlobClientFactory( factory = reflector.BlobClientFactory(
self.session.blob_manager, self.client_blob_manager,
blob_hashes_to_send blob_hashes_to_send
) )
factory.protocol_version = 0 factory.protocol_version = 0
@ -311,20 +281,20 @@ class TestReflector(unittest.TestCase):
def verify_stream_on_reflector(): def verify_stream_on_reflector():
# check stream_info_manager has all the right information # 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(1, len(streams))
self.assertEqual(self.stream_hash, streams[0]) 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] 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] 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) 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.stream_hash)
self.assertEqual(self.sd_hash, sd_hash) self.assertEqual(self.sd_hash, sd_hash)
# check should_announce blobs on blob_manager # 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]}) self.assertSetEqual(set(to_announce), {self.sd_hash, expected_blob_hashes[0]})
def verify_have_blob(blob_hash, blob_size): 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): def send_to_server_as_blobs(blob_hashes_to_send):
factory = reflector.BlobClientFactory( factory = reflector.BlobClientFactory(
self.session.blob_manager, self.client_blob_manager,
blob_hashes_to_send blob_hashes_to_send
) )
factory.protocol_version = 0 factory.protocol_version = 0
@ -344,7 +314,7 @@ class TestReflector(unittest.TestCase):
return factory.finished_deferred return factory.finished_deferred
def send_to_server_as_stream(result): 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 from twisted.internet import reactor
reactor.connectTCP('localhost', self.port, factory) reactor.connectTCP('localhost', self.port, factory)

View file

@ -1,21 +1,18 @@
import os import os
import shutil import shutil
import tempfile import tempfile
from hashlib import md5 from hashlib import md5
from twisted.trial.unittest import TestCase from twisted.trial.unittest import TestCase
from twisted.internet import defer, threads 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.core.StreamDescriptor import StreamDescriptorIdentifier
from lbrynet.file_manager.EncryptedFileCreator import create_lbry_file from lbrynet.core.BlobManager import DiskBlobManager
from lbrynet.lbry_file.client.EncryptedFileOptions import add_lbry_file_to_sd_identifier
from lbrynet.core.StreamDescriptor import get_sd_info from lbrynet.core.StreamDescriptor import get_sd_info
from lbrynet.core.PeerManager import PeerManager from lbrynet.core.PeerManager import PeerManager
from lbrynet.core.RateLimiter import DummyRateLimiter 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 from lbrynet.tests import mocks
@ -38,47 +35,40 @@ class TestStreamify(TestCase):
self.is_generous = True self.is_generous = True
self.db_dir = tempfile.mkdtemp() self.db_dir = tempfile.mkdtemp()
self.blob_dir = os.path.join(self.db_dir, "blobfiles") self.blob_dir = os.path.join(self.db_dir, "blobfiles")
os.mkdir(self.blob_dir)
self.dht_node = FakeNode() self.dht_node = FakeNode()
self.wallet = FakeWallet() self.wallet = FakeWallet()
self.peer_manager = PeerManager() self.peer_manager = PeerManager()
self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2) self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2)
self.rate_limiter = DummyRateLimiter() self.rate_limiter = DummyRateLimiter()
self.sd_identifier = StreamDescriptorIdentifier() 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 @defer.inlineCallbacks
def tearDown(self): def tearDown(self):
lbry_files = self.lbry_file_manager.lbry_files lbry_files = self.lbry_file_manager.lbry_files
for lbry_file in lbry_files: for lbry_file in lbry_files:
yield self.lbry_file_manager.delete_lbry_file(lbry_file) yield self.lbry_file_manager.delete_lbry_file(lbry_file)
if self.lbry_file_manager is not None: yield self.lbry_file_manager.stop()
yield self.lbry_file_manager.stop() yield self.storage.stop()
if self.session is not None:
yield self.session.shut_down()
yield self.session.storage.stop()
yield threads.deferToThread(shutil.rmtree, self.db_dir) yield threads.deferToThread(shutil.rmtree, self.db_dir)
if os.path.exists("test_file"): if os.path.exists("test_file"):
os.remove("test_file") os.remove("test_file")
def test_create_stream(self): 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): def verify_equal(sd_info):
self.assertEqual(sd_info, test_create_stream_sd_file) self.assertEqual(sd_info, test_create_stream_sd_file)
def verify_stream_descriptor_file(stream_hash): 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) d.addCallback(verify_equal)
return d return d
@ -90,39 +80,24 @@ class TestStreamify(TestCase):
def create_stream(): def create_stream():
test_file = GenFile(5209343, b''.join([chr(i + 3) for i in xrange(0, 64, 6)])) 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, d = create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager, "test_file",
key="0123456701234567", iv_generator=iv_generator()) test_file, key="0123456701234567", iv_generator=iv_generator())
d.addCallback(lambda lbry_file: lbry_file.stream_hash) d.addCallback(lambda lbry_file: lbry_file.stream_hash)
return d return d
d.addCallback(lambda _: create_stream()) d = create_stream()
d.addCallback(verify_stream_descriptor_file) d.addCallback(verify_stream_descriptor_file)
return d return d
@defer.inlineCallbacks
def test_create_and_combine_stream(self): def test_create_and_combine_stream(self):
test_file = GenFile(53209343, b''.join([chr(i + 5) for i in xrange(0, 64, 6)]))
self.session = Session( lbry_file = yield create_lbry_file(self.blob_manager, self.storage, self.prm, self.lbry_file_manager,
conf.ADJUSTABLE_SETTINGS['data_rate'][1], db_dir=self.db_dir, node_id="abcd", peer_finder=self.peer_finder, "test_file", test_file)
blob_dir=self.blob_dir, peer_port=5553, rate_limiter=self.rate_limiter, wallet=self.wallet, sd_hash = yield self.storage.get_sd_blob_hash_for_stream(lbry_file.stream_hash)
external_ip="127.0.0.1", dht_node=self.dht_node self.assertTrue(lbry_file.sd_hash, sd_hash)
) yield lbry_file.start()
f = open('test_file')
self.lbry_file_manager = EncryptedFileManager(self.session, self.sd_identifier) hashsum = md5()
hashsum.update(f.read())
@defer.inlineCallbacks self.assertEqual(hashsum.hexdigest(), "68959747edc73df45e45db6379dd7b3b")
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

View file

@ -7,9 +7,7 @@ from twisted.internet import defer
from twisted.trial import unittest from twisted.trial import unittest
from lbrynet import conf from lbrynet import conf
from lbrynet.database.storage import SQLiteStorage, open_file_for_writing 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.EncryptedFileDownloader import ManagedEncryptedFileDownloader
from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager
from lbrynet.tests.util import random_lbry_hash from lbrynet.tests.util import random_lbry_hash
log = logging.getLogger() log = logging.getLogger()
@ -67,7 +65,6 @@ fake_claim_info = {
} }
class FakeAnnouncer(object): class FakeAnnouncer(object):
def __init__(self): def __init__(self):
self._queue_size = 0 self._queue_size = 0
@ -245,12 +242,8 @@ class FileStorageTests(StorageTest):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_store_file(self): def test_store_file(self):
session = MocSession(self.storage)
session.db_dir = self.db_dir
sd_identifier = StreamDescriptorIdentifier()
download_directory = self.db_dir download_directory = self.db_dir
manager = EncryptedFileManager(session, sd_identifier) out = yield self.storage.get_all_lbry_files()
out = yield manager.session.storage.get_all_lbry_files()
self.assertEqual(len(out), 0) self.assertEqual(len(out), 0)
stream_hash = random_lbry_hash() stream_hash = random_lbry_hash()
@ -268,33 +261,29 @@ class FileStorageTests(StorageTest):
blob_data_rate = 0 blob_data_rate = 0
file_name = "test file" 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 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) 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)) 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) self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_STOPPED)
running = ManagedEncryptedFileDownloader.STATUS_RUNNING running = ManagedEncryptedFileDownloader.STATUS_RUNNING
yield manager.session.storage.change_file_status(rowid, running) yield self.storage.change_file_status(rowid, running)
status = yield manager.session.storage.get_lbry_file_status(rowid) status = yield self.storage.get_lbry_file_status(rowid)
self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_RUNNING) self.assertEqual(status, ManagedEncryptedFileDownloader.STATUS_RUNNING)
class ContentClaimStorageTests(StorageTest): class ContentClaimStorageTests(StorageTest):
@defer.inlineCallbacks @defer.inlineCallbacks
def test_store_content_claim(self): def test_store_content_claim(self):
session = MocSession(self.storage)
session.db_dir = self.db_dir
sd_identifier = StreamDescriptorIdentifier()
download_directory = self.db_dir download_directory = self.db_dir
manager = EncryptedFileManager(session, sd_identifier) out = yield self.storage.get_all_lbry_files()
out = yield manager.session.storage.get_all_lbry_files()
self.assertEqual(len(out), 0) self.assertEqual(len(out), 0)
stream_hash = random_lbry_hash() 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) yield self.make_and_store_fake_stream(blob_count=2, stream_hash=stream_hash, sd_hash=sd_hash)
blob_data_rate = 0 blob_data_rate = 0
file_name = "test file" 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 stream_hash, file_name, download_directory, blob_data_rate
) )
yield self.storage.save_claims([fake_claim_info]) yield self.storage.save_claims([fake_claim_info])

View file

@ -1,18 +1,29 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from cryptography.hazmat.primitives.ciphers.algorithms import AES from cryptography.hazmat.primitives.ciphers.algorithms import AES
import mock
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import defer from twisted.internet import defer
from lbrynet.database.storage import SQLiteStorage
from lbrynet.core.StreamDescriptor import get_sd_info, BlobStreamDescriptorReader from lbrynet.core.StreamDescriptor import get_sd_info, BlobStreamDescriptorReader
from lbrynet.core import BlobManager from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier
from lbrynet.core import Session 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 EncryptedFileCreator
from lbrynet.file_manager import EncryptedFileManager from lbrynet.file_manager.EncryptedFileManager import EncryptedFileManager
from lbrynet.tests import mocks from lbrynet.tests import mocks
from lbrynet.tests.util import mk_db_and_blob_dir, rm_db_and_blob_dir 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 MB = 2**20
@ -24,31 +35,35 @@ def iv_generator():
class CreateEncryptedFileTest(unittest.TestCase): class CreateEncryptedFileTest(unittest.TestCase):
timeout = 5 timeout = 5
@defer.inlineCallbacks
def setUp(self): def setUp(self):
mocks.mock_conf_settings(self) mocks.mock_conf_settings(self)
self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir() self.tmp_db_dir, self.tmp_blob_dir = mk_db_and_blob_dir()
self.wallet = FakeWallet()
self.session = mock.Mock(spec=Session.Session)(None, None) self.peer_manager = PeerManager()
self.session.payment_rate_manager.min_blob_data_payment_rate = 0 self.peer_finder = FakePeerFinder(5553, self.peer_manager, 2)
self.blob_manager = BlobManager.DiskBlobManager(self.tmp_blob_dir, SQLiteStorage(self.tmp_db_dir)) self.rate_limiter = DummyRateLimiter()
self.session.blob_manager = self.blob_manager self.sd_identifier = StreamDescriptorIdentifier()
self.session.storage = self.session.blob_manager.storage self.storage = SQLiteStorage(self.tmp_db_dir)
self.file_manager = EncryptedFileManager.EncryptedFileManager(self.session, object()) self.blob_manager = DiskBlobManager(self.tmp_blob_dir, self.storage)
yield self.session.blob_manager.storage.setup() self.prm = OnlyFreePaymentsManager()
yield self.session.blob_manager.setup() 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 @defer.inlineCallbacks
def tearDown(self): def tearDown(self):
yield self.lbry_file_manager.stop()
yield self.blob_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) rm_db_and_blob_dir(self.tmp_db_dir, self.tmp_blob_dir)
@defer.inlineCallbacks @defer.inlineCallbacks
def create_file(self, filename): def create_file(self, filename):
handle = mocks.GenFile(3*MB, '1') handle = mocks.GenFile(3*MB, '1')
key = '2' * (AES.block_size / 8) 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()) key, iv_generator())
defer.returnValue(out) defer.returnValue(out)
@ -60,7 +75,7 @@ class CreateEncryptedFileTest(unittest.TestCase):
"c8728fe0534dd06fbcacae92b0891787ad9b68ffc8d20c1" "c8728fe0534dd06fbcacae92b0891787ad9b68ffc8d20c1"
filename = 'test.file' filename = 'test.file'
lbry_file = yield self.create_file(filename) 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 # read the sd blob file
sd_blob = self.blob_manager.blobs[sd_hash] sd_blob = self.blob_manager.blobs[sd_hash]
@ -68,7 +83,7 @@ class CreateEncryptedFileTest(unittest.TestCase):
sd_file_info = yield sd_reader.get_info() sd_file_info = yield sd_reader.get_info()
# this comes from the database, the blobs returned are sorted # 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.assertDictEqual(sd_info, sd_file_info)
self.assertListEqual(sd_info['blobs'], sd_file_info['blobs']) self.assertListEqual(sd_info['blobs'], sd_file_info['blobs'])
self.assertEqual(sd_info['stream_hash'], expected_stream_hash) self.assertEqual(sd_info['stream_hash'], expected_stream_hash)

View file

@ -11,18 +11,17 @@ from faker import Faker
from lbryschema.decode import smart_decode from lbryschema.decode import smart_decode
from lbryum.wallet import NewWallet from lbryum.wallet import NewWallet
from lbrynet import conf from lbrynet import conf
from lbrynet.core import Session, PaymentRateManager, Wallet from lbrynet.core import Wallet
from lbrynet.database.storage import SQLiteStorage from lbrynet.database.storage import SQLiteStorage
from lbrynet.daemon.ComponentManager import ComponentManager 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 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.Components import PEER_PROTOCOL_SERVER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT
from lbrynet.daemon.Daemon import Daemon as LBRYDaemon from lbrynet.daemon.Daemon import Daemon as LBRYDaemon
from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader
from lbrynet.tests import util from lbrynet.tests import util
from lbrynet.tests.mocks import mock_conf_settings, FakeNetwork, FakeFileManager 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 ExchangeRateManager as DummyExchangeRateManager
from lbrynet.tests.mocks import BTCLBCFeed, USDBTCFeed from lbrynet.tests.mocks import BTCLBCFeed, USDBTCFeed
from lbrynet.tests.util import is_android 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} 'USDBTC': {'spot': 2.0, 'ts': util.DEFAULT_ISO_TIME + 2}
} }
daemon = LBRYDaemon(None) daemon = LBRYDaemon(None)
daemon.session = mock.Mock(spec=Session.Session)
daemon.wallet = mock.Mock(spec=Wallet.LBRYumWallet) daemon.wallet = mock.Mock(spec=Wallet.LBRYumWallet)
daemon.wallet.wallet = mock.Mock(spec=NewWallet) daemon.wallet.wallet = mock.Mock(spec=NewWallet)
daemon.wallet.wallet.use_encryption = False daemon.wallet.wallet.use_encryption = False
daemon.wallet.network = FakeNetwork() daemon.wallet.network = FakeNetwork()
daemon.session.storage = mock.Mock(spec=SQLiteStorage) daemon.storage = mock.Mock(spec=SQLiteStorage)
market_feeds = [BTCLBCFeed(), USDBTCFeed()] market_feeds = [BTCLBCFeed(), USDBTCFeed()]
daemon.exchange_rate_manager = DummyExchangeRateManager(market_feeds, rates) 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 = { metadata = {
"author": "fake author", "author": "fake author",
@ -146,7 +140,7 @@ class TestFileListSorting(unittest.TestCase):
self.faker.seed(66410) self.faker.seed(66410)
self.test_daemon = get_test_daemon() self.test_daemon = get_test_daemon()
component_manager = ComponentManager( 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, PEER_PROTOCOL_SERVER_COMPONENT, REFLECTOR_COMPONENT, HASH_ANNOUNCER_COMPONENT,
STREAM_IDENTIFIER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT], STREAM_IDENTIFIER_COMPONENT, EXCHANGE_RATE_MANAGER_COMPONENT],
file_manager=FakeFileManager file_manager=FakeFileManager

View file

@ -3,16 +3,18 @@ import mock
from twisted.trial import unittest from twisted.trial import unittest
from twisted.internet import defer, task 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.core.Error import DownloadDataTimeout, DownloadSDTimeout
from lbrynet.daemon import Downloader from lbrynet.daemon import Downloader
from lbrynet.core.StreamDescriptor import StreamDescriptorIdentifier 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.EncryptedFileStatusReport import EncryptedFileStatusReport
from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader from lbrynet.file_manager.EncryptedFileDownloader import ManagedEncryptedFileDownloader
from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager from lbrynet.daemon.ExchangeRateManager import ExchangeRateManager
from lbrynet.tests.mocks import ExchangeRateManager as DummyExchangeRateManager
from lbrynet.tests.mocks import mock_conf_settings 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): class GetStreamTests(unittest.TestCase):
def init_getstream_with_mocs(self): def init_getstream_with_mocs(self):
mock_conf_settings(self) mock_conf_settings(self)
sd_identifier = mock.Mock(spec=StreamDescriptorIdentifier) sd_identifier = mock.Mock(spec=StreamDescriptorIdentifier)
session = mock.Mock(spec=Session.Session) wallet = mock.Mock(spec=Wallet.LBRYumWallet)
session.wallet = mock.Mock(spec=Wallet.LBRYumWallet)
prm = mock.Mock(spec=PaymentRateManager.NegotiatedPaymentRateManager) 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) 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 disable_max_key_fee = False
data_rate = {'currency':"LBC", 'amount':0, 'address':''} data_rate = {'currency': "LBC", 'amount': 0, 'address': ''}
getstream = Downloader.GetStream(sd_identifier, wallet, exchange_rate_manager, blob_manager, peer_finder,
getstream = Downloader.GetStream(sd_identifier, session, DummyRateLimiter(), prm, storage, max_key_fee, disable_max_key_fee,
exchange_rate_manager, max_key_fee, disable_max_key_fee, timeout=3, data_rate=data_rate) timeout=3, data_rate=data_rate)
getstream.pay_key_fee_called = False getstream.pay_key_fee_called = False
self.clock = task.Clock() self.clock = task.Clock()