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-05-24 18:56:39 +02:00
|
|
|
from lbrynet import conf
|
2018-05-24 00:28:22 +02:00
|
|
|
from lbrynet.core import utils
|
2018-05-24 18:56:39 +02:00
|
|
|
from lbrynet.dht.hashannouncer import DHTHashAnnouncer
|
2018-07-06 22:16:58 +02:00
|
|
|
from tests.util import random_lbry_hash
|
2018-07-24 02:59:57 +02:00
|
|
|
from tests.mocks import mock_conf_settings
|
2017-10-23 21:36:50 +02:00
|
|
|
|
2018-05-24 18:56:39 +02:00
|
|
|
|
2017-02-08 00:17:03 +01:00
|
|
|
class MocDHTNode(object):
|
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-05-24 18:56:39 +02:00
|
|
|
class MocStorage(object):
|
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):
|
2018-07-24 02:59:57 +02:00
|
|
|
mock_conf_settings(self)
|
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)
|
|
|
|
self.announcer = DHTHashAnnouncer(self.dht_node, self.storage)
|
|
|
|
|
|
|
|
@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)
|