def test_socketpair(self): server, client = socketpair() self.assertEqual(client.send(b"ciao"), 4) self.assertEqual(server.recv(io.DEFAULT_BUFFER_SIZE), b"ciao") self.assertEqual(server.send(b"ciao a te"), 9) self.assertEqual(client.recv(io.DEFAULT_BUFFER_SIZE), b"ciao a te") close_socket(server) self.assertTrue(is_socket_closed(server)) self.assertTrue(is_socket_closed(None))
def test_socketpair(self): server, client = socketpair() self.assertEqual(client.send(b'ciao'), 4) self.assertEqual(server.recv(io.DEFAULT_BUFFER_SIZE), b'ciao') self.assertEqual(server.send(b'ciao a te'), 9) self.assertEqual(client.recv(io.DEFAULT_BUFFER_SIZE), b'ciao a te') close_socket(server) self.assertTrue(is_socket_closed(server)) self.assertTrue(is_socket_closed(None))
def get_or_create_connection(self, client, connection=None): '''Get or create a new connection for ``client``. If a ``connection`` is given and either * the connection is in the set of available connections * the connection is in the set of concurrent connections but without a protocol consumer then it is chosen ahead of others in the pool. ''' stale_connections = [] with self.lock: if connection: if connection in self._available_connections: self._available_connections.remove(connection) elif not (connection in self._concurrent_connections and connection.current_consumer is None): connection = None if connection: sock = connection.sock closed = is_socket_closed(sock) if closed: if sock: stale_connections.append(connection) connection = None else: self._concurrent_connections.add(connection) if not connection: try: closed = True while closed: connection = self._available_connections.pop() sock = connection.sock closed = is_socket_closed(sock) if closed and sock: stale_connections.append(connection) except KeyError: connection = None else: # we have a connection, lets added it to the concurrent set self._concurrent_connections.add(connection) for sc in stale_connections: sc.transport.close() if connection is None: # build the new connection connection = self.new_connection(client.consumer_factory, producer=client) return connection
def _get(self): queue = self._queue # grab the connection without waiting, important! if queue.qsize(): connection = queue.get_nowait() # wait for one to be available elif self.in_use + self._connecting >= queue._maxsize: if self._timeout: connection = yield future_timeout(queue.get(), self._timeout) else: connection = yield queue.get() else: # must create a new connection self._connecting += 1 try: connection = yield self._creator() finally: self._connecting -= 1 # None signal that a connection was removed form the queue # Go again if connection is None: connection = yield self._get() else: if is_socket_closed(connection.sock): connection.close() connection = yield self._get() else: self._in_use_connections.add(connection) coroutine_return(connection)
def test_echo_serve(self): loop = get_event_loop() server = TcpServer(loop, '127.0.0.1', 0, EchoServerProtocol) yield server.start_serving() sock = server.sock fn = sock.fileno() self.assertFalse(is_socket_closed(sock)) client = Echo() address = sock.getsockname() result = yield client.request(address, b'Hello!') self.assertEqual(result, b'Hello!') self.assertEqual(server.concurrent_connections, 1) result = yield client.request(address, b'ciao') self.assertEqual(result, b'ciao') self.assertEqual(server.concurrent_connections, 1) yield server.stop_serving() yield async_while(3, lambda: not is_socket_closed(sock)) self.assertTrue(is_socket_closed(sock))
def is_connection_closed(self, connection): try: sock = connection.sock except AttributeError: return True if is_socket_closed(sock): connection.close() return True return False
def test_start_serving_ipv6(self): loop = get_event_loop() sockets = yield loop.start_serving(Protocol,'::1', 0) self.assertEqual(len(sockets), 1) sock = sockets[0] self.assertEqual(sock.family, socket.AF_INET6) address = sock.getsockname() faddress = format_address(address) self.assertEqual(faddress, '[::1]:%s' % address[1]) loop.stop_serving(sock) self.assertTrue(is_socket_closed(sock))
def is_connection_closed(self, connection): if not getattr(connection.transport, 'is_closing', call_false)(): try: sock = connection.sock except AttributeError: return True if is_socket_closed(sock): connection.close() return True return False return True
def is_connection_closed(self, connection): is_closing = getattr(connection.transport, 'is_closing', None) if is_closing: return is_closing() else: try: sock = connection.sock except AttributeError: return True if is_socket_closed(sock): connection.close() return True return False return True
def test_start_serving(self): protocol_factory = lambda : Connection() loop = get_event_loop() sockets = yield loop.start_serving(server_protocol,'127.0.0.1', 0) self.assertEqual(len(sockets), 1) socket = sockets[0] fn = socket.fileno() events, read, write, error = loop.io.handlers(fn) self.assertEqual(events, READ) self.assertTrue(read) self.assertFalse(write) self.assertFalse(error) loop.stop_serving(socket) self.assertRaises(KeyError, loop.io.handlers, fn) self.assertTrue(is_socket_closed(socket))
def is_connection_closed(self, connection): if is_socket_closed(connection.sock): connection.close() return True return False