def run(coro): nonlocal CNT CNT = 0 srv = tb.tcp_server(server, max_clients=TOTAL_CNT, backlog=TOTAL_CNT) srv.start() tasks = [] for _ in range(TOTAL_CNT): tasks.append(coro(srv.addr)) loop.run_until_complete(asyncio.gather(*tasks, loop=loop)) srv.join() assert CNT == TOTAL_CNT
def test_socket_connect_recv_send(loop): def srv_gen(): yield tb.write(b'helo') data = yield tb.read(4 * _SIZE) assert data == b'ehlo' * _SIZE yield tb.write(b'O') yield tb.write(b'K') # We use @asyncio.coroutine & `yield from` to test # the compatibility of Cython's 'async def' coroutines. @asyncio.coroutine def client(sock, addr): yield from loop.sock_connect(sock, addr) data = yield from recv_all(loop, sock, 4) assert data == b'helo' yield from loop.sock_sendall(sock, b'ehlo' * _SIZE) data = yield from recv_all(loop, sock, 2) assert data == b'OK' with tb.tcp_server(srv_gen) as srv: sock = socket.socket() with sock: sock.setblocking(False) loop.run_until_complete(client(sock, srv.addr))