A sketchy test for a connection, acting in a suspicious way
This commit is contained in:
parent
f859d696d6
commit
b62f0309ed
|
@ -6,13 +6,42 @@ import unittest
|
|||
import tempfile
|
||||
import time
|
||||
|
||||
from minode import connection, main, shared
|
||||
from minode import connection, main, proofofwork, shared, structure
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='[%(asctime)s] [%(levelname)s] %(message)s')
|
||||
|
||||
|
||||
class PushConnection(connection.ConnectionBase):
|
||||
"""A connection pushing useless objects"""
|
||||
|
||||
def __init__(self, *args):
|
||||
super().__init__(*args)
|
||||
self.objects_setup = 0
|
||||
self.started = time.time()
|
||||
|
||||
def _request_objects(self):
|
||||
"""Make objects to send"""
|
||||
while not shared.vector_advertise_queue.empty():
|
||||
vector = shared.vector_advertise_queue.get()
|
||||
self.vectors_to_send.add(vector)
|
||||
|
||||
if self.objects_setup < 10:
|
||||
if self.objects_setup == 0:
|
||||
self.started = time.time()
|
||||
obj = structure.Object(
|
||||
b'\x00' * 8, int(time.time() + 1800), 42, 1, 1,
|
||||
b'HELLO %i' % self.objects_setup)
|
||||
proofofwork.do_pow_and_publish(obj)
|
||||
self.objects_setup += 1
|
||||
elif (
|
||||
len(shared.objects) == 10 and len(self.vectors_to_send) == 0
|
||||
or time.time() - self.started > 120
|
||||
):
|
||||
self.status = 'disconnecting'
|
||||
|
||||
|
||||
class TestNetwork(unittest.TestCase):
|
||||
"""Test case starting connections"""
|
||||
|
||||
|
@ -29,14 +58,17 @@ class TestNetwork(unittest.TestCase):
|
|||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
def test_connection(self):
|
||||
"""Check a normal connection - should receive objects"""
|
||||
def _make_initial_nodes(self):
|
||||
main.load_data()
|
||||
self.assertGreaterEqual(len(shared.core_nodes), 3)
|
||||
|
||||
main.bootstrap_from_dns()
|
||||
self.assertGreaterEqual(len(shared.unchecked_node_pool), 3)
|
||||
|
||||
def test_connection(self):
|
||||
"""Check a normal connection - should receive objects"""
|
||||
self._make_initial_nodes()
|
||||
|
||||
started = time.time()
|
||||
nodes = list(shared.core_nodes.union(shared.unchecked_node_pool))
|
||||
random.shuffle(nodes)
|
||||
|
@ -65,3 +97,18 @@ class TestNetwork(unittest.TestCase):
|
|||
break
|
||||
else:
|
||||
self.fail('Failed to establish a proper connection')
|
||||
|
||||
def test_push(self):
|
||||
"""Make and push useless objects"""
|
||||
self._make_initial_nodes()
|
||||
|
||||
for node in random.sample(
|
||||
shared.core_nodes.union(shared.unchecked_node_pool), 5
|
||||
):
|
||||
c = PushConnection(*node)
|
||||
c.start()
|
||||
c.join()
|
||||
if shared.objects:
|
||||
if len(shared.objects) == 10:
|
||||
self.fail('Successfuly pushed 10 objects')
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue