def test_is_completely_asynchronous(): conn = TCPAsyncSocketConnection() assertions = 0 for attribute in dir(conn): if attribute.startswith("read_"): assert iscoroutinefunction(conn.__getattribute__(attribute)) assertions += 1 assert assertions > 0, "None of the read_* attributes were async"
async def async_status(self, tries: int = 3, **kwargs): """Asynchronously checks the status of a Minecraft Java Edition server via the ping protocol. :param int tries: How many times to retry if it fails. :param type **kwargs: Passed to a `AsyncServerPinger` instance. :return: Status information in a `PingResponse` instance. :rtype: PingResponse """ connection = TCPAsyncSocketConnection() await connection.connect((self.host, self.port)) exception = None for attempt in range(tries): try: pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs) pinger.handshake() result = await pinger.read_status() result.latency = await pinger.test_ping() return result except Exception as e: exception = e else: raise exception
class TestAsyncSocketConnection: def setup_method(self): self.tcp_async_socket = TCPAsyncSocketConnection() def test_tcp_socket_read(self): try: from asyncio.exceptions import TimeoutError except ImportError: from asyncio import TimeoutError loop = asyncio.get_event_loop() with patch("asyncio.open_connection") as open_conn: open_conn.return_value = (FakeAsyncStream(), None) loop.run_until_complete( self.tcp_async_socket.connect("dummy_address", timeout=0.01)) with pytest.raises(TimeoutError): loop.run_until_complete(self.tcp_async_socket.read(10))
async def async_status(self, tries=3, **kwargs): connection = TCPAsyncSocketConnection() await connection.connect((self.host, self.port)) exception = None for attempt in range(tries): try: pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs) pinger.handshake() result = await pinger.read_status() result.latency = await pinger.test_ping() return result except Exception as e: exception = e else: raise exception
async def async_ping(self, tries: int = 3, **kwargs): """Asynchronously checks the latency between a Minecraft Java Edition server and the client (you). :param int tries: How many times to retry if it fails. :param type **kwargs: Passed to a `AsyncServerPinger` instance. :return: The latency between the Minecraft Server and you. :rtype: float """ connection = TCPAsyncSocketConnection() await connection.connect((self.host, self.port)) exception = None for attempt in range(tries): try: pinger = AsyncServerPinger(connection, host=self.host, port=self.port, **kwargs) pinger.handshake() return await pinger.test_ping() except Exception as e: exception = e else: raise exception
def setup_method(self): self.tcp_async_socket = TCPAsyncSocketConnection()
def test_is_completely_asynchronous(self): conn = TCPAsyncSocketConnection() for attribute in dir(conn): if attribute.startswith("read_"): self.assertTrue(iscoroutinefunction(conn.__getattribute__(attribute)))