lbry-sdk/tests/unit/core/test_BlobManager.py

137 lines
4.7 KiB
Python
Raw Normal View History

2017-06-16 19:13:41 +02:00
import tempfile
import shutil
import os
import random
import string
from twisted.trial import unittest
from twisted.internet import defer
2017-06-16 19:13:41 +02:00
from tests.util import random_lbry_hash
from lbrynet.p2p.BlobManager import DiskBlobManager
from lbrynet.extras.daemon.storage import SQLiteStorage
from lbrynet.p2p.Peer import Peer
from lbrynet.extras.daemon import conf
from lbrynet.p2p.cryptoutils import get_lbry_hash_obj
2017-06-16 19:13:41 +02:00
2017-06-16 19:13:41 +02:00
class BlobManagerTest(unittest.TestCase):
@defer.inlineCallbacks
2017-06-16 19:13:41 +02:00
def setUp(self):
2018-05-02 20:45:01 +02:00
conf.initialize_settings(False)
2017-06-16 19:13:41 +02:00
self.blob_dir = tempfile.mkdtemp()
self.db_dir = tempfile.mkdtemp()
2018-03-27 23:35:31 +02:00
self.bm = DiskBlobManager(self.blob_dir, SQLiteStorage(self.db_dir))
2017-09-29 12:44:22 +02:00
self.peer = Peer('somehost', 22)
yield self.bm.storage.setup()
2017-06-16 19:13:41 +02:00
@defer.inlineCallbacks
2017-06-16 19:13:41 +02:00
def tearDown(self):
yield self.bm.stop()
yield self.bm.storage.stop()
shutil.rmtree(self.blob_dir)
shutil.rmtree(self.db_dir)
2017-06-16 19:13:41 +02:00
@defer.inlineCallbacks
def _create_and_add_blob(self, should_announce=False):
2017-06-16 19:13:41 +02:00
# create and add blob to blob manager
2017-09-29 12:44:22 +02:00
data_len = random.randint(1, 1000)
data = b''.join(random.choice(string.ascii_lowercase).encode() for _ in range(data_len))
2017-06-16 19:13:41 +02:00
hashobj = get_lbry_hash_obj()
hashobj.update(data)
2017-09-29 12:44:22 +02:00
out = hashobj.hexdigest()
blob_hash = out
2017-06-16 19:13:41 +02:00
# create new blob
yield self.bm.setup()
2017-09-29 12:44:22 +02:00
blob = yield self.bm.get_blob(blob_hash, len(data))
2017-06-16 19:13:41 +02:00
writer, finished_d = yield blob.open_for_writing(self.peer)
yield writer.write(data)
yield self.bm.blob_completed(blob, should_announce)
2017-06-16 19:13:41 +02:00
# check to see if blob is there
2017-09-29 12:44:22 +02:00
self.assertTrue(os.path.isfile(os.path.join(self.blob_dir, blob_hash)))
2017-06-16 19:13:41 +02:00
blobs = yield self.bm.get_all_verified_blobs()
self.assertIn(blob_hash, blobs)
2017-06-16 19:13:41 +02:00
defer.returnValue(blob_hash)
@defer.inlineCallbacks
def test_create_blob(self):
blob_hashes = []
# create a bunch of blobs
2017-09-29 12:44:22 +02:00
for i in range(0, 10):
2017-06-16 19:13:41 +02:00
blob_hash = yield self._create_and_add_blob()
blob_hashes.append(blob_hash)
blobs = yield self.bm.get_all_verified_blobs()
2017-09-29 12:44:22 +02:00
self.assertEqual(10, len(blobs))
2017-06-16 19:13:41 +02:00
@defer.inlineCallbacks
def test_delete_blob(self):
# create blob
2017-09-29 12:44:22 +02:00
blob_hash = yield self._create_and_add_blob()
2017-06-16 19:13:41 +02:00
blobs = yield self.bm.get_all_verified_blobs()
2017-09-29 12:44:22 +02:00
self.assertEqual(len(blobs), 1)
2017-06-16 19:13:41 +02:00
2017-09-29 12:44:22 +02:00
# delete blob
2017-06-16 19:13:41 +02:00
yield self.bm.delete_blobs([blob_hash])
2017-09-29 12:44:22 +02:00
self.assertFalse(os.path.isfile(os.path.join(self.blob_dir, blob_hash)))
2017-06-16 19:13:41 +02:00
blobs = yield self.bm.get_all_verified_blobs()
2017-09-29 12:44:22 +02:00
self.assertEqual(len(blobs), 0)
blobs = yield self.bm.storage.get_all_blob_hashes()
2017-09-29 12:44:22 +02:00
self.assertEqual(len(blobs), 0)
self.assertNotIn(blob_hash, self.bm.blobs)
# delete blob that was already deleted once
yield self.bm.delete_blobs([blob_hash])
2017-06-16 19:13:41 +02:00
# delete blob that does not exist, nothing will
# happen
2017-09-29 12:44:22 +02:00
blob_hash = random_lbry_hash()
yield self.bm.delete_blobs([blob_hash])
2017-06-16 19:13:41 +02:00
@defer.inlineCallbacks
def test_delete_open_blob(self):
# Test that a blob that is opened for writing will not be deleted
# create blobs
2017-09-29 12:44:22 +02:00
blob_hashes = []
for i in range(0, 10):
blob_hash = yield self._create_and_add_blob()
2017-06-16 19:13:41 +02:00
blob_hashes.append(blob_hash)
blobs = yield self.bm.get_all_verified_blobs()
2017-09-29 12:44:22 +02:00
self.assertEqual(len(blobs), 10)
2017-06-16 19:13:41 +02:00
# open the last blob
blob = yield self.bm.get_blob(blob_hashes[-1])
2018-07-24 02:59:57 +02:00
w, finished_d = yield blob.open_for_writing(self.peer)
# schedule a close, just to leave the reactor clean
finished_d.addBoth(lambda x:None)
self.addCleanup(w.close)
2017-06-30 21:49:42 +02:00
2017-06-16 19:13:41 +02:00
# delete the last blob and check if it still exists
yield self.bm.delete_blobs([blob_hash])
2017-06-16 19:13:41 +02:00
blobs = yield self.bm.get_all_verified_blobs()
2017-09-29 12:44:22 +02:00
self.assertEqual(len(blobs), 10)
self.assertIn(blob_hashes[-1], blobs)
2017-09-29 12:44:22 +02:00
self.assertTrue(os.path.isfile(os.path.join(self.blob_dir, blob_hashes[-1])))
@defer.inlineCallbacks
def test_should_announce(self):
# create blob with should announce
blob_hash = yield self._create_and_add_blob(should_announce=True)
out = yield self.bm.get_should_announce(blob_hash)
self.assertTrue(out)
count = yield self.bm.count_should_announce_blobs()
self.assertEqual(1, count)
2018-10-18 13:40:37 +02:00
# set should announce to False
yield self.bm.set_should_announce(blob_hash, should_announce=False)
out = yield self.bm.get_should_announce(blob_hash)
self.assertFalse(out)
count = yield self.bm.count_should_announce_blobs()
self.assertEqual(0, count)