def test_echo_data(self): # Echo a bunch of data and ensure it is echoed identically server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) md1 = hashlib.sha256() md2 = hashlib.sha256() def produce(): for i in range(1000): chunk = os.urandom(1024) client.write(chunk) md1.update(chunk) client.write_eof() def consume(): while True: buf = client.read1(1024) if not buf: break md2.update(buf) f1 = gruvi.spawn(produce) f2 = gruvi.spawn(consume) f1.join(); f2.join() self.assertEqual(md1.digest(), md2.digest()) server.close() client.close()
def test_read_timeout(self): server = StreamServer(echo_handler) server.listen(self.pipename()) client = StreamClient(timeout=0.01) client.connect(server.addresses[0]) self.assertRaises(Timeout, client.readline) server.close() client.close()
def test_echo_tcp(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) client.write(b'foo\n') client.write_eof() self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_echo_pipe(self): server = StreamServer(echo_handler) server.listen(self.pipename()) client = StreamClient() client.connect(server.addresses[0]) client.write(b'foo\n') client.write_eof() self.assertEqual(client.readline(), b'foo\n') self.assertEqual(client.readline(), b'') server.close() client.close()
def test_illegal_request(self): server = HttpServer(hello_app) server.listen(('localhost', 0)) addr = server.addresses[0] client = StreamClient() client.connect(addr) client.write(b'foo\r\n') client.write_eof() buf = client.read() self.assertEqual(buf, b'') server.close() client.close()
def test_echo_tcp_ssl(self): server = StreamServer(echo_handler) context = self.get_ssl_context() server.listen(('127.0.0.1', 0), ssl=context) client = StreamClient() client.connect(server.addresses[0], ssl=context) client.write(b'foo\n') self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_pipe(self): server = StreamServer(echo_handler) path = self.pipename('temp.sock') server.listen(path) client = StreamClient() client.connect(path) buf = b'x' * 1024 client.write(buf) result = client.read(1024) assert result == buf client.close()
def test_tcp(self): server = StreamServer(echo_handler) server.listen(('localhost', 0)) addr = server.transport.getsockname() client = StreamClient() client.connect(addr) buf = b'x' * 1024 client.write(buf) result = client.read(1024) assert result == buf client.close()
def test_send_random(self): server = JsonRpcServer(echo_app) server.listen(('127.0.0.1', 0)) addr = server.transport.getsockname() hub = gruvi.get_hub() client = StreamClient() client.connect(addr) try: while True: chunk = os.urandom(1024) client.write(chunk) except pyuv.error.TCPError as e: error = e assert error.args[0] in (pyuv.errno.UV_ECONNRESET, pyuv.errno.UV_EPIPE, pyuv.errno.UV_ECANCELED)
def test_echo_tcp_ssl(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0), **self.ssl_s_args) client = StreamClient() client.connect(server.addresses[0], **self.ssl_c_args) client.write(b'foo\n') self.assertEqual(client.readline(), b'foo\n') server.close() client.close()
def test_connection_limit(self): server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) addr = server.addresses[0] server.max_connections = 10 clients = [] try: for i in range(15): client = StreamClient() client.connect(addr) client.write(b'foo\n') buf = client.readline() if buf == b'': # conneciton closed: EOF client.close() clients.append(client) except TransportError: pass self.assertLessEqual(len(server.connections), server.max_connections) for client in clients: client.close() server.close()
def test_echo_data(self): # Echo a bunch of data and ensure it is echoed identically server = StreamServer(echo_handler) server.listen(('127.0.0.1', 0)) client = StreamClient() client.connect(server.addresses[0]) md1 = hashlib.sha256() md2 = hashlib.sha256() def produce(): for i in range(1000): chunk = os.urandom(1024) client.write(chunk) md1.update(chunk) client.write_eof() def consume(): while True: buf = client.read1(1024) if not buf: break md2.update(buf) f1 = gruvi.spawn(produce) f2 = gruvi.spawn(consume) f1.join() f2.join() self.assertEqual(md1.digest(), md2.digest()) server.close() client.close()
def test_stream(self): def echo_handler(stream, protocol, client): while True: buf = stream.read(4096) if not buf: break nbytes = stream.write(buf) server = StreamServer(echo_handler) server.listen(('localhost', 0), ssl=True, keyfile=self.certname, certfile=self.certname) addr = server.transport.getsockname() client = StreamClient() client.connect(addr, ssl=True) buf = b'x' * 1024 client.write(buf) result = b'' while len(result) != 1024: result += client.read(1024) assert result == buf client.close() server.close()
import argparse import logging import pyuv from gruvi import get_hub, util from gruvi.pyuv import TTY from gruvi.stream import StreamClient logging.basicConfig() parser = argparse.ArgumentParser() parser.add_argument('hostname') parser.add_argument('port', type=int) args = parser.parse_args() client = StreamClient() client.connect((args.hostname, args.port)) peer = client.transport.getpeername() print('Connected to {0!s}'.format(util.saddr(peer)), file=sys.stderr) stdin = StreamClient() stdin.connect(TTY(sys.stdin)) stdout = StreamClient() stdout.connect(TTY(sys.stdout)) def read_stdin(): while True: buf = stdin.read(4096) if not buf: logger.debug('Got EOF on stdin')
def test_inherit_handle(self): # Ensure that it's possible to pass a handle to the child. # Note: The "ipc" flag doubles as a read/write flag. hub = get_hub() handle = pyuv.Pipe(hub.loop, True) proc = Process() proc.spawn(python_script(['catn', '3']), extra_handles=[handle]) stream = StreamClient() stream.connect(handle) stream.write(b'Foo\n') self.assertEqual(stream.readline(), b'Foo\n') stream.write_eof() self.assertEqual(stream.readline(), b'') stream.close() proc.wait() self.assertEqual(proc.returncode, 0) proc.close()