2017-02-08 00:17:03 +01:00
|
|
|
from twisted.trial import unittest
|
2018-05-24 00:28:22 +02:00
|
|
|
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
|
2017-10-23 21:36:50 +02:00
|
|
|
|
2018-05-24 18:56:39 +02:00
|
|
|
|
2018-10-18 12:42:45 +02:00
|
|
|
class MocDHTNode:
|
2018-05-24 00:28:22 +02:00
|
|
|
def __init__(self):
|
2017-02-08 00:17:03 +01:00
|
|
|
self.blobs_announced = 0
|
2018-05-24 18:56:39 +02:00
|
|
|
self.clock = task.Clock()
|
|
|
|
self.peerPort = 3333
|
2017-02-08 00:17:03 +01:00
|
|
|
|
2017-10-27 20:12:52 +02:00
|
|
|
def announceHaveBlob(self, blob):
|
2017-02-08 00:17:03 +01:00
|
|
|
self.blobs_announced += 1
|
2018-05-24 18:56:39 +02:00
|
|
|
d = defer.Deferred()
|
|
|
|
self.clock.callLater(1, d.callback, ['fake'])
|
|
|
|
return d
|
|
|
|
|
2018-05-24 00:28:22 +02:00
|
|
|
|
2018-10-18 12:42:45 +02:00
|
|
|
class MocStorage:
|
2018-05-24 00:28:22 +02:00
|
|
|
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):
|
2018-05-24 00:28:22 +02:00
|
|
|
if not self.announced:
|
|
|
|
self.announced = True
|
|
|
|
return defer.succeed(self.blobs_to_announce)
|
|
|
|
else:
|
|
|
|
return defer.succeed([])
|
2017-02-08 00:17:03 +01:00
|
|
|
|
2018-05-24 18:56:39 +02:00
|
|
|
def update_last_announced_blob(self, blob_hash, now):
|
|
|
|
return defer.succeed(None)
|
|
|
|
|
|
|
|
|
2017-10-23 21:36:50 +02:00
|
|
|
class DHTHashAnnouncerTest(unittest.TestCase):
|
2018-05-24 00:28:22 +02:00
|
|
|
|
2017-02-08 00:17:03 +01:00
|
|
|
def setUp(self):
|
2019-01-21 21:55:50 +01:00
|
|
|
conf = Config()
|
2017-02-08 00:17:03 +01:00
|
|
|
self.num_blobs = 10
|
|
|
|
self.blobs_to_announce = []
|
|
|
|
for i in range(0, self.num_blobs):
|
2017-02-18 01:59:57 +01:00
|
|
|
self.blobs_to_announce.append(random_lbry_hash())
|
2017-02-08 00:17:03 +01:00
|
|
|
self.dht_node = MocDHTNode()
|
2018-05-24 18:56:39 +02:00
|
|
|
self.clock = self.dht_node.clock
|
2018-05-24 00:28:22 +02:00
|
|
|
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)
|
2018-05-24 00:28:22 +02:00
|
|
|
self.clock.advance(1)
|
2018-05-24 18:56:39 +02:00
|
|
|
yield announce_d
|
2017-02-08 00:17:03 +01:00
|
|
|
self.assertEqual(self.dht_node.blobs_announced, self.num_blobs)
|
2018-05-24 00:28:22 +02:00
|
|
|
self.assertEqual(self.announcer.hash_queue_size(), 0)
|