def test_BitcoinPeerProtocol_multiplex(): peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) next_message_list = [peer.new_get_next_message_f() for i in range(50)] COUNT = 0 @asyncio.coroutine def async_test(next_message): name, data = yield from next_message() assert name == 'version' assert data == VERSION_MSG name, data = yield from next_message() assert name == 'verack' assert data == {} nonlocal COUNT COUNT += 1 peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) asyncio.get_event_loop().run_until_complete( asyncio.wait( [asyncio.Task(async_test(nm)) for nm in next_message_list])) assert COUNT == 50
def test_eof(): peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) COUNT = 0 @asyncio.coroutine def async_listen(next_message): count = 0 try: while True: name, data = yield from next_message() count += 1 except EOFError: pass assert count == 2 nonlocal COUNT COUNT += 1 tasks = [ asyncio.Task(async_listen(peer.new_get_next_message_f())) for i in range(50) ] peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) # end of stream peer.connection_lost(None) # give everyone a chance to run (but no one finishes) asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks)) assert COUNT == 50
def test_BitcoinPeerProtocol_read(): DATA = [] def write_f(data): DATA.append(data) peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(write_f) peer.connection_made(pt) next_message = peer.new_get_next_message_f() @asyncio.coroutine def async_test(): t = [] name, data = yield from next_message() t.append((name, data)) name, data = yield from next_message() t.append((name, data)) return t peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) t = asyncio.get_event_loop().run_until_complete(async_test()) assert len(t) == 2 assert t[0] == ('version', VERSION_MSG) assert t[1] == ('verack', {})
def test_BitcoinPeerProtocol_read(): DATA = [] def write_f(data): DATA.append(data) peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(write_f) peer.connection_made(pt) next_message = peer.new_get_next_message_f() @asyncio.coroutine def async_test(): t = [] name, data = yield from next_message() t.append((name, data)) name, data = yield from next_message() t.append((name, data)) return t peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) t = asyncio.get_event_loop().run_until_complete(async_test()) assert len(t) == 2 assert t[0] == ('version', VERSION_MSG) assert t[1] == ('verack', {})
def test_eof(): peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) COUNT = 0 @asyncio.coroutine def async_listen(next_message): count = 0 try: while True: name, data = yield from next_message() count += 1 except EOFError: pass assert count == 2 nonlocal COUNT COUNT += 1 tasks = [asyncio.Task(async_listen(peer.new_get_next_message_f())) for i in range(50)] peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) # end of stream peer.connection_lost(None) # give everyone a chance to run (but no one finishes) asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks)) assert COUNT == 50
def test_queue_gc(): # create a peer # add 50 listeners # receive 100 messages # first 10 listeners will stop listening after two messages # check GC peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) next_message_list = [peer.new_get_next_message_f() for i in range(50)] assert len(peer.message_queues) == 50 next_message_list = None assert len(peer.message_queues) == 0 @asyncio.coroutine def async_listen(next_message, delay=0): for i in range(101): name, data = yield from next_message() yield from asyncio.sleep(delay) for i in range(3): peer.add_task(async_listen(peer.new_get_next_message_f(), delay=60)) tasks = [ asyncio.Task(async_listen(peer.new_get_next_message_f(), delay=1)) for i in range(50) ] peer.data_received(VERSION_MSG_BIN) for i in range(100): peer.data_received(VERACK_MSG_BIN) # give everyone a chance to run (but no one finishes) asyncio.get_event_loop().run_until_complete( asyncio.wait(tasks, timeout=0.1)) assert len(peer.message_queues) == 53 # now let all 50 finish. They should be collected. asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks)) ## you would think this number would be 3, but it's not. ## oh well. This is close enough. assert len(peer.message_queues) <= 4
def test_queue_gc(): # create a peer # add 50 listeners # receive 100 messages # first 10 listeners will stop listening after two messages # check GC peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) next_message_list = [peer.new_get_next_message_f() for i in range(50)] assert len(peer.message_queues) == 50 next_message_list = None assert len(peer.message_queues) == 0 @asyncio.coroutine def async_listen(next_message, delay=0): for i in range(101): name, data = yield from next_message() yield from asyncio.sleep(delay) for i in range(3): peer.add_task(async_listen(peer.new_get_next_message_f(), delay=60)) tasks = [asyncio.Task(async_listen(peer.new_get_next_message_f(), delay=1)) for i in range(50)] peer.data_received(VERSION_MSG_BIN) for i in range(100): peer.data_received(VERACK_MSG_BIN) # give everyone a chance to run (but no one finishes) asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks, timeout=0.1)) assert len(peer.message_queues) == 53 # now let all 50 finish. They should be collected. asyncio.get_event_loop().run_until_complete(asyncio.wait(tasks)) ## you would think this number would be 3, but it's not. ## oh well. This is close enough. assert len(peer.message_queues) <= 4
def test_BitcoinPeerProtocol_multiplex(): peer = BitcoinPeerProtocol(MAGIC_HEADER) pt = PeerTransport(None) peer.connection_made(pt) next_message_list = [peer.new_get_next_message_f() for i in range(50)] COUNT = 0 @asyncio.coroutine def async_test(next_message): name, data = yield from next_message() assert name == 'version' assert data == VERSION_MSG name, data = yield from next_message() assert name == 'verack' assert data == {} nonlocal COUNT COUNT += 1 peer.data_received(VERSION_MSG_BIN) peer.data_received(VERACK_MSG_BIN) asyncio.get_event_loop().run_until_complete(asyncio.wait([asyncio.Task(async_test(nm)) for nm in next_message_list])) assert COUNT == 50