def test_pub_sub_multi_sockets_remove_one(self): pub1 = self.ctx.socket(zmq.PUB) pub1.bind('inproc://test1') pub2 = self.ctx.socket(zmq.PUB) pub2.bind('inproc://test2') s = SubscriptionService(ctx=self.ctx) s.add_subscription(SocketStruct.from_string('inproc://test1')) s.add_subscription(SocketStruct.from_string('inproc://test2')) async def remove(): s.remove_subscription(SocketStruct.from_string('inproc://test2')) async def delayed_send(): await asyncio.sleep(0.2) pub2.send(b'howdy2') tasks = asyncio.gather( s.serve(), pub1.send(b'howdy'), remove(), stop_server(s, 0.2), delayed_send() ) loop = asyncio.get_event_loop() loop.run_until_complete(tasks) self.assertListEqual(s.received, [(b'howdy', 'inproc://test1')]) self.assertListEqual(s.to_remove, [])
def test_add_subscription_modifies_dict(self): s = SubscriptionService(ctx=self.ctx) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10001')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10002')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10003')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10004')) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10001']) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10002']) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10003']) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10004'])
def test_remove_subscription_deletes_from_dict(self): s = SubscriptionService(ctx=self.ctx) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10001')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10002')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10003')) s.add_subscription(SocketStruct.from_string('tcp://127.0.0.1:10004')) s.remove_subscription('tcp://127.0.0.1:10001') s.remove_subscription('tcp://127.0.0.1:10003') self.assertIsNone(s.subscriptions.get('tcp://127.0.0.1:10001')) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10002']) self.assertIsNone(s.subscriptions.get('tcp://127.0.0.1:10003')) self.assertTrue(s.subscriptions['tcp://127.0.0.1:10004'])
def test_pub_sub_multi_sockets(self): pub1 = self.ctx.socket(zmq.PUB) pub1.bind('inproc://test1') pub2 = self.ctx.socket(zmq.PUB) pub2.bind('inproc://test2') s = SubscriptionService(ctx=self.ctx) s.add_subscription(SocketStruct.from_string('inproc://test1')) s.add_subscription(SocketStruct.from_string('inproc://test2')) tasks = asyncio.gather( s.serve(), pub1.send(b'howdy'), pub2.send(b'howdy2'), stop_server(s, 0.1) ) loop = asyncio.get_event_loop() loop.run_until_complete(tasks) self.assertListEqual(s.received, [(b'howdy', 'inproc://test1'), (b'howdy2', 'inproc://test2')])
def __init__(self, socket_id: SocketStruct, ctx: zmq.Context, wallet=None, linger=1000, poll_timeout=500): if socket_id.protocol == Protocols.TCP: socket_id.id = '*' self.address = str(socket_id) self.wallet = wallet self.ctx = ctx self.socket = None self.linger = linger self.poll_timeout = poll_timeout self.running = False
def test_catchup(self): fake_block_1 = {'blockNum': 1, 'subBlocks': []} fake_block_2 = {'blockNum': 2, 'subBlocks': []} self.c.store_block(fake_block_1) self.c.store_block(fake_block_2) mn_wallets = [Wallet() for _ in range(2)] dl_wallets = [Wallet() for _ in range(2)] constitution = { 'masternodes': [mn.verifying_key().hex() for mn in mn_wallets], 'delegates': [dl.verifying_key().hex() for dl in dl_wallets], 'masternode_min_quorum': 2, 'delegate_min_quorum': 2, } n = Node(socket_base='tcp://127.0.0.1', ctx=self.ctx, constitution=constitution, wallet=mn_wallets[0]) w = Wallet() m = BlockServer(w, 'tcp://127.0.0.1', self.ctx, linger=500, poll_timeout=500) tasks = asyncio.gather( m.serve(), n.catchup(SocketStruct.from_string('tcp://127.0.0.1:10004')), stop_server(m, 1), ) loop = asyncio.get_event_loop() res = loop.run_until_complete(tasks) print(res)
def test_from_string_ipc(self): s = SocketStruct.from_string('ipc:///tmp/something') self.assertEqual(str(s), 'ipc:///tmp/something')
def test_from_string_in_proc(self): s = SocketStruct.from_string('inproc://blahblahblah') self.assertEqual(str(s), 'inproc://blahblahblah')
def test_inproc_sock_serialization(self): s = SocketStruct(Protocols.INPROC, '127.0.0.1', 1000) self.assertEqual(str(s), 'inproc://127.0.0.1')
def test_socket_serialization_properly(self): s = SocketStruct.from_string('tcp://127.0.0.1:1000') self.assertEqual(str(s), 'tcp://127.0.0.1:1000')
def test_socket_structure_serialization(self): s = SocketStruct(Protocols.TCP, '127.0.0.1', 1000) self.assertEqual(str(s), 'tcp://127.0.0.1:1000')
async def remove(): s.remove_subscription(SocketStruct.from_string('inproc://test2'))