lbry-sdk/tests/unit/dht/test_hash_announcer.py

60 lines
1.9 KiB
Python
Raw Normal View History

from twisted.trial import unittest
from twisted.internet import defer, task
2018-11-07 21:15:05 +01:00
from lbrynet import utils
2019-01-21 21:55:50 +01:00
from lbrynet.conf import Config
2018-11-07 15:54:09 +01:00
from lbrynet.extras.daemon.HashAnnouncer import DHTHashAnnouncer
2018-11-07 21:15:05 +01:00
from tests.test_utils import random_lbry_hash
2018-05-24 18:56:39 +02:00
class MocDHTNode:
def __init__(self):
self.blobs_announced = 0
2018-05-24 18:56:39 +02:00
self.clock = task.Clock()
self.peerPort = 3333
def announceHaveBlob(self, blob):
self.blobs_announced += 1
2018-05-24 18:56:39 +02:00
d = defer.Deferred()
self.clock.callLater(1, d.callback, ['fake'])
return d
class MocStorage:
def __init__(self, blobs_to_announce):
self.blobs_to_announce = blobs_to_announce
self.announced = False
2018-05-24 18:56:39 +02:00
def get_blobs_to_announce(self):
if not self.announced:
self.announced = True
return defer.succeed(self.blobs_to_announce)
else:
return defer.succeed([])
2018-05-24 18:56:39 +02:00
def update_last_announced_blob(self, blob_hash, now):
return defer.succeed(None)
class DHTHashAnnouncerTest(unittest.TestCase):
def setUp(self):
2019-01-21 21:55:50 +01:00
conf = Config()
self.num_blobs = 10
self.blobs_to_announce = []
for i in range(0, self.num_blobs):
self.blobs_to_announce.append(random_lbry_hash())
self.dht_node = MocDHTNode()
2018-05-24 18:56:39 +02:00
self.clock = self.dht_node.clock
utils.call_later = self.clock.callLater
2018-05-24 18:56:39 +02:00
self.storage = MocStorage(self.blobs_to_announce)
2019-01-21 21:55:50 +01:00
self.announcer = DHTHashAnnouncer(conf, self.dht_node, self.storage)
2018-05-24 18:56:39 +02:00
@defer.inlineCallbacks
def test_immediate_announce(self):
announce_d = self.announcer.immediate_announce(self.blobs_to_announce)
self.assertEqual(self.announcer.hash_queue_size(), self.num_blobs)
self.clock.advance(1)
2018-05-24 18:56:39 +02:00
yield announce_d
self.assertEqual(self.dht_node.blobs_announced, self.num_blobs)
self.assertEqual(self.announcer.hash_queue_size(), 0)