def test_socket_connect_recv_send(self): def srv_gen(): yield tb.write(b'helo') data = yield tb.read(4 * _SIZE) self.assertEqual(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 self.loop.sock_connect(sock, addr) data = yield from self.recv_all(sock, 4) self.assertEqual(data, b'helo') yield from self.loop.sock_sendall(sock, b'ehlo' * _SIZE) data = yield from self.recv_all(sock, 2) self.assertEqual(data, b'OK') with tb.tcp_server(srv_gen) as srv: sock = socket.socket() with sock: sock.setblocking(False) self.loop.run_until_complete(client(sock, srv.addr))
def test_socket_connect_recv_send(self): if self.is_asyncio_loop() and sys.version_info[:3] == (3, 5, 2): # See https://github.com/python/asyncio/pull/366 for details. raise unittest.SkipTest() def srv_gen(): yield tb.write(b'helo') data = yield tb.read(4 * _SIZE) self.assertEqual(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 self.loop.sock_connect(sock, addr) data = yield from self.recv_all(sock, 4) self.assertEqual(data, b'helo') yield from self.loop.sock_sendall(sock, b'ehlo' * _SIZE) data = yield from self.recv_all(sock, 2) self.assertEqual(data, b'OK') with tb.tcp_server(srv_gen) as srv: sock = socket.socket() with sock: sock.setblocking(False) self.loop.run_until_complete(client(sock, srv.addr))
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)) self.loop.run_until_complete( asyncio.gather(*tasks, loop=self.loop)) srv.join() self.assertEqual(CNT, TOTAL_CNT)
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)) self.loop.run_until_complete(asyncio.gather(*tasks, loop=self.loop)) srv.join() self.assertEqual(CNT, TOTAL_CNT)
def test_socket_connect_recv_send(self): def srv_gen(): yield tb.write(b'helo') data = yield tb.read(4 * _SIZE) self.assertEqual(data, b'ehlo' * _SIZE) yield tb.write(b'O') yield tb.write(b'K') async def client(sock, addr): await self.loop.sock_connect(sock, addr) data = await self.recv_all(sock, 4) self.assertEqual(data, b'helo') await self.loop.sock_sendall(sock, b'ehlo' * _SIZE) data = await self.recv_all(sock, 2) self.assertEqual(data, b'OK') with tb.tcp_server(srv_gen) as srv: sock = socket.socket() with sock: sock.setblocking(False) self.loop.run_until_complete(client(sock, srv.addr))