adding immediate announce option to hash announce

This commit is contained in:
Kay Kurokawa 2017-02-09 10:09:58 -05:00
parent aa45b0e121
commit 34f5bc93ae
2 changed files with 14 additions and 4 deletions

View file

@ -39,7 +39,7 @@ class DHTHashAnnouncer(object):
def immediate_announce(self, blob_hashes): def immediate_announce(self, blob_hashes):
if self.peer_port is not None: if self.peer_port is not None:
return self._announce_hashes(blob_hashes) return self._announce_hashes(blob_hashes, immediate=True)
else: else:
return defer.succeed(False) return defer.succeed(False)
@ -56,7 +56,7 @@ class DHTHashAnnouncer(object):
dl = defer.DeferredList(ds) dl = defer.DeferredList(ds)
return dl return dl
def _announce_hashes(self, hashes): def _announce_hashes(self, hashes, immediate=False):
if not hashes: if not hashes:
return return
log.debug('Announcing %s hashes', len(hashes)) log.debug('Announcing %s hashes', len(hashes))
@ -67,7 +67,10 @@ class DHTHashAnnouncer(object):
for h in hashes: for h in hashes:
announce_deferred = defer.Deferred() announce_deferred = defer.Deferred()
ds.append(announce_deferred) ds.append(announce_deferred)
self.hash_queue.append((h, announce_deferred)) if immediate:
self.hash_queue.appendleft((h, announce_deferred))
else:
self.hash_queue.append((h, announce_deferred))
log.debug('There are now %s hashes remaining to be announced', self.hash_queue_size()) log.debug('There are now %s hashes remaining to be announced', self.hash_queue_size())
def announce(): def announce():

View file

@ -42,9 +42,16 @@ class DHTHashAnnouncerTest(unittest.TestCase):
def test_basic(self): def test_basic(self):
self.announcer._announce_available_hashes() self.announcer._announce_available_hashes()
self.assertEqual(self.announcer.hash_queue_size(),self.announcer.CONCURRENT_ANNOUNCERS)
self.clock.advance(1) self.clock.advance(1)
self.assertEqual(self.dht_node.blobs_announced, self.num_blobs) self.assertEqual(self.dht_node.blobs_announced, self.num_blobs)
self.assertEqual(self.announcer.hash_queue_size(), 0) self.assertEqual(self.announcer.hash_queue_size(), 0)
def test_immediate_announce(self):
# Test that immediate announce puts a hash at the front of the queue
self.announcer._announce_available_hashes()
blob_hash = binascii.b2a_hex(os.urandom(32))
self.announcer.immediate_announce([blob_hash])
self.assertEqual(self.announcer.hash_queue_size(),self.announcer.CONCURRENT_ANNOUNCERS+1)
self.assertEqual(blob_hash, self.announcer.hash_queue[0][0])