Added a rough implementation of a connection searching for specific vectors
and used it in test_push() - need to debug.
This commit is contained in:
parent
b62f0309ed
commit
13ecc4c100
|
@ -6,7 +6,7 @@ import unittest
|
|||
import tempfile
|
||||
import time
|
||||
|
||||
from minode import connection, main, proofofwork, shared, structure
|
||||
from minode import connection, main, message, proofofwork, shared, structure
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
|
@ -42,6 +42,33 @@ class PushConnection(connection.ConnectionBase):
|
|||
self.status = 'disconnecting'
|
||||
|
||||
|
||||
class SearchConnection(connection.Connection):
|
||||
"""A connection searching for objects"""
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.started = time.time()
|
||||
self.vectors_to_search = {v for v in shared.objects.keys()}
|
||||
shared.objects.clear()
|
||||
logging.info(
|
||||
'Have %s objects to search for', len(self.vectors_to_search))
|
||||
|
||||
def _process_msg_inv(self, m):
|
||||
inv = message.Inv.from_message(m)
|
||||
to_get = inv.vectors.intersection(self.vectors_to_search)
|
||||
self.vectors_to_get.update(to_get)
|
||||
self.vectors_to_search.difference_update(to_get)
|
||||
logging.info('Found %s vectors', len(to_get))
|
||||
|
||||
def _send_objects(self):
|
||||
if (
|
||||
len(self.vectors_to_search) == len(self.vectors_to_get) == 0
|
||||
or time.time() - self.started > 300
|
||||
):
|
||||
self.status = 'disconnecting'
|
||||
logging.info('disconnecting!')
|
||||
|
||||
|
||||
class TestNetwork(unittest.TestCase):
|
||||
"""Test case starting connections"""
|
||||
|
||||
|
@ -102,13 +129,36 @@ class TestNetwork(unittest.TestCase):
|
|||
"""Make and push useless objects"""
|
||||
self._make_initial_nodes()
|
||||
|
||||
for node in random.sample(
|
||||
shared.core_nodes.union(shared.unchecked_node_pool), 5
|
||||
):
|
||||
nodes = shared.core_nodes.union(shared.unchecked_node_pool)
|
||||
# logging.info('Nodes: %s', nodes)
|
||||
sent_to_nodes = set()
|
||||
for node in random.sample(nodes, 5):
|
||||
c = PushConnection(*node)
|
||||
c.start()
|
||||
c.join()
|
||||
sent_to_nodes.add(node)
|
||||
if shared.objects:
|
||||
if len(shared.objects) == 10:
|
||||
self.fail('Successfuly pushed 10 objects')
|
||||
break
|
||||
else:
|
||||
return
|
||||
|
||||
nodes.difference_update(sent_to_nodes)
|
||||
self.assertGreaterEqual(len(nodes), 1)
|
||||
objects = shared.objects.copy()
|
||||
nodes = list(nodes)
|
||||
random.shuffle(nodes)
|
||||
# logging.info('Nodes: %s', nodes)
|
||||
|
||||
for node in nodes:
|
||||
c = SearchConnection(*node)
|
||||
c.start()
|
||||
c.join()
|
||||
if len(shared.objects) == len(objects):
|
||||
break
|
||||
else:
|
||||
logging.info('Got %s objects', len(shared.objects))
|
||||
shared.objects = objects.copy()
|
||||
else:
|
||||
return
|
||||
|
||||
self.assertNotEqual(shared.objects, objects)
|
||||
|
|
Loading…
Reference in New Issue
Block a user