def test_family(self): self.tcp4 = uv.TCP() self.assert_equal(self.tcp4.family, None) self.tcp4.bind((common.TEST_IPV4, common.TEST_PORT1)) self.assert_equal(self.tcp4.family, socket.AF_INET) self.tcp6 = uv.TCP() self.tcp6.bind((common.TEST_IPV6, common.TEST_PORT1)) self.assert_equal(self.tcp6.family, socket.AF_INET6)
def test_tcp_ipv6(self): address = (common.TEST_IPV6, common.TEST_PORT1) self.server = uv.TCP() self.server.bind(address) self.server.listen(5, on_connection=self.on_connection) self.client = uv.TCP() self.client.connect(address, on_connect=self.on_connect) self.run_ping_pong()
def test_buffer_size(self): self.tcp = uv.TCP() self.tcp.bind((common.TEST_IPV4, common.TEST_PORT1)) self.tcp.send_buffer_size = 262144 self.assert_equal(self.tcp.send_buffer_size, 262144) self.tcp.receive_buffer_size = 262144 self.assert_equal(self.tcp.receive_buffer_size, 262144)
def test_sockname_peername(self): address = (common.TEST_IPV4, common.TEST_PORT1) def on_connection(server, status): server.close() def on_connect(request, status): self.assert_equal(request.stream.peername, address) request.stream.close() self.server = uv.TCP() self.server.bind(address) self.server.listen(on_connection=on_connection) self.assert_equal(self.server.sockname, address) self.client = uv.TCP() self.client.connect(address, on_connect=on_connect) self.loop.run()
def main(): loop = uv.Loop.get_current() server = uv.TCP() server.bind(('0.0.0.0', 25000)) server.listen(20, on_connection=on_connection) sigint = uv.Signal() sigint.start(uv.Signals.SIGINT, on_signal=on_quit) loop.run()
def test_tcp_ipv4_icp_client(self): address = (common.TEST_IPV4, common.TEST_PORT1) self.server = uv.TCP() self.server.bind(address) self.server.listen(5, on_connection=self.on_connection) client = uv.TCP() client.connect(address) def on_read(pipe_client, status, data): if pipe_client.pending_count: self.client = pipe_client.pending_accept() self.client.read_start(on_read=self.on_read) pipe_client.close() def on_connection(pipe_server, status): connection = pipe_server.accept(ipc=True) connection.write(b'hello', send_stream=client) connection.close() pipe_server.close() self.pipe_server = uv.Pipe() self.pipe_server.pending_instances(100) self.pipe_server.bind(common.TEST_PIPE1) self.pipe_server.listen(on_connection=on_connection) self.pipe_client = uv.Pipe(ipc=True) self.pipe_client.connect(common.TEST_PIPE1) self.pipe_client.read_start(on_read=on_read) self.loop.run() self.run_ping_pong() client.close() self.server.close() self.client.close()
def test_closed(self): self.tcp = uv.TCP() self.tcp.close() self.assert_raises(uv.ClosedHandleError, self.tcp.open, 0) self.assert_raises(uv.ClosedHandleError, self.tcp.bind, None, 0) self.assert_raises(uv.ClosedHandleError, self.tcp.connect, (common.TEST_IPV4, 42)) with self.should_raise(uv.ClosedHandleError): sockname = self.tcp.sockname with self.should_raise(uv.ClosedHandleError): peername = self.tcp.peername self.assert_raises(uv.ClosedHandleError, self.tcp.set_nodelay, True) self.assert_raises(uv.ClosedHandleError, self.tcp.set_keepalive, True, 10) self.assert_raises(uv.ClosedHandleError, self.tcp.set_simultaneous_accepts, True)
def test_poll(self): def on_shutdown(shutdown_request, _): """ :type shutdown_request: uv.ShutdownRequest """ shutdown_request.stream.close() def on_connection(server, status): """ :type server: uv.TCP """ connection = server.accept() connection.write(b'hello') connection.shutdown(on_shutdown=on_shutdown) server.close() self.buffer = b'' def on_event(poll_handle, status, events): if status == uv.StatusCodes.SUCCESS: self.buffer = self.client.recv(1024) if self.buffer.startswith( b'hello') or status != uv.StatusCodes.SUCCESS: poll_handle.stop() self.tcp = uv.TCP() self.tcp.bind((common.TEST_IPV4, common.TEST_PORT1)) self.tcp.listen(on_connection=on_connection) self.client = socket.socket() self.client.connect((common.TEST_IPV4, common.TEST_PORT1)) self.poll = uv.Poll(self.client.fileno(), on_event=on_event) self.poll.start() self.assert_equal(self.poll.fileno(), self.client.fileno()) self.loop.run() self.assert_equal(self.buffer, b'hello')
def test_closed(self): self.tcp = uv.TCP() self.tcp.close() self.assert_true(self.tcp.closing) with self.should_raise(uv.ClosedHandleError): self.tcp.referenced = True with self.should_raise(uv.ClosedHandleError): self.tcp.referenced = False with self.should_raise(uv.ClosedHandleError): send_buffer_size = self.tcp.send_buffer_size with self.should_raise(uv.ClosedHandleError): self.tcp.send_buffer_size = 42 with self.should_raise(uv.ClosedHandleError): receive_buffer_size = self.tcp.receive_buffer_size with self.should_raise(uv.ClosedHandleError): self.tcp.receive_buffer_size = 42 self.assert_raises(uv.ClosedHandleError, self.tcp.fileno) self.assert_is(self.tcp.close(), None) self.loop.run() self.assert_true(self.tcp.closed) self.assert_false(self.tcp.active) self.assert_false(self.tcp.referenced)
def test_cancel(self): self.tcp = uv.TCP() self.assert_raises(uv.error.ArgumentError, self.tcp.connect(('127.0.0.1', 80)).cancel)
def test_fileno(self): self.tcp = uv.TCP() self.tcp.bind((common.TEST_IPV4, common.TEST_PORT1)) self.assert_is_instance(self.tcp.fileno(), int)
def test_open(self): server = socket.socket() self.tcp = uv.TCP() self.tcp.open(server.fileno())
def test_settings(self): self.tcp = uv.TCP() self.tcp.set_nodelay(True) self.tcp.set_keepalive(False, 10) self.tcp.set_simultaneous_accepts(True)