Exemplo n.º 1
0
class TCPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertRaises(TypeError, self.connection.remaining)

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with self.assertRaises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.send.assert_called_once_with(bytearray.fromhex("7FAA"))
Exemplo n.º 2
0
 def status(self, retries=3, **kwargs):
     exception = None
     for attempt in range(retries):
         try:
             connection = TCPSocketConnection((self.host, self.port))
             pinger = ServerPinger(connection,
                                   host=self.host,
                                   port=self.port,
                                   **kwargs)
             pinger.handshake()
             result = pinger.read_status()
             try:
                 result.latency = pinger.test_ping()
             except IOError:
                 # Some servers have an animated MOTD, which is nonstandard and
                 # breaks the protocol. We don't get latency for these servers.
                 result.latency = 0.00
             return result
         except Exception as e:
             exception = e
         finally:
             try:
                 connection.close()
             except:
                 pass
     else:
         raise exception
Exemplo n.º 3
0
 def setup_method(self):
     socket = Mock()
     socket.recv = Mock()
     socket.send = Mock()
     with patch("socket.create_connection") as create_connection:
         create_connection.return_value = socket
         self.connection = TCPSocketConnection(("localhost", 1234))
Exemplo n.º 4
0
class TCPSocketConnectionTest(TestCase):
    def setUp(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        self.assertRaises(TypeError, self.connection.flush)

    def test_receive(self):
        self.assertRaises(TypeError, self.connection.receive, "")

    def test_remaining(self):
        self.assertRaises(TypeError, self.connection.remaining)

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        self.assertEqual(self.connection.read(2), bytearray.fromhex("7FAA"))

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with self.assertRaises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        self.connection.socket.send.assert_called_once_with(
            bytearray.fromhex("7FAA"))
Exemplo n.º 5
0
 def setUp(self):
     socket = Mock()
     socket.recv = Mock()
     socket.send = Mock()
     with patch("socket.create_connection") as create_connection:
         create_connection.return_value = socket
         self.connection = TCPSocketConnection(("localhost", 1234))
Exemplo n.º 6
0
    def status(self, tries: int = 3, **kwargs):
        """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 `ServerPinger` instance.
        :return: Status information in a `PingResponse` instance.
        :rtype: PingResponse
        """

        connection = TCPSocketConnection((self.host, self.port))
        exception = None
        for attempt in range(tries):
            try:
                pinger = ServerPinger(connection,
                                      host=self.host,
                                      port=self.port,
                                      **kwargs)
                pinger.handshake()
                result = pinger.read_status()
                result.latency = pinger.test_ping()
                return result
            except Exception as e:
                exception = e
        else:
            raise exception
Exemplo n.º 7
0
 def ping(self, retries=3, **kwargs):
     exception = None
     for attempt in range(retries):
         try:
             connection = TCPSocketConnection((self.host, self.port))
             pinger = ServerPinger(connection,
                                   host=self.host,
                                   port=self.port,
                                   **kwargs)
             pinger.handshake()
             result = pinger.test_ping()
             return result
         except Exception as e:
             exception = e
         finally:
             try:
                 connection.close()
             except:
                 pass
     else:
         raise exception
Exemplo n.º 8
0
 def ping(self, tries=3, **kwargs):
     connection = TCPSocketConnection((self.host, self.port))
     exception = None
     for attempt in range(tries):
         try:
             pinger = ServerPinger(connection, host=self.host, port=self.port, **kwargs)
             pinger.handshake()
             return pinger.test_ping()
         except Exception as e:
             exception = e
     else:
         raise exception
Exemplo n.º 9
0
 def status(self, retries=3, **kwargs):
     connection = TCPSocketConnection((self.host, self.port))
     exception = None
     for attempt in range(retries):
         try:
             pinger = ServerPinger(connection, host=self.host, port=self.port, **kwargs)
             pinger.handshake()
             result = pinger.read_status()
             result.latency = pinger.test_ping()
             return result
         except Exception as e:
             exception = e
     else:
         raise exception
Exemplo n.º 10
0
    def ping(self, tries: int = 3, **kwargs):
        """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 `ServerPinger` instance.
        :return: The latency between the Minecraft Server and you.
        :rtype: float
        """

        connection = TCPSocketConnection((self.host, self.port))
        exception = None
        for attempt in range(tries):
            try:
                pinger = ServerPinger(connection, host=self.host, port=self.port, **kwargs)
                pinger.handshake()
                return pinger.test_ping()
            except Exception as e:
                exception = e
        else:
            raise exception
Exemplo n.º 11
0
class TCPSocketConnectionTest:
    def setup_method(self):
        socket = Mock()
        socket.recv = Mock()
        socket.send = Mock()
        with patch("socket.create_connection") as create_connection:
            create_connection.return_value = socket
            self.connection = TCPSocketConnection(("localhost", 1234))

    def test_flush(self):
        with pytest.raises(TypeError):
            self.connection.flush()

    def test_receive(self):
        with pytest.raises(TypeError):
            self.connection.receive("")

    def test_remaining(self):
        with pytest.raises(TypeError):
            self.connection.remaining()

    def test_read(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("7FAA")

        assert self.connection.read(2) == bytearray.fromhex("7FAA")

    def test_read_empty(self):
        self.connection.socket.recv.return_value = bytearray.fromhex("")

        with pytest.raises(IOError):
            self.connection.read(2)

    def test_write(self):
        self.connection.write(bytearray.fromhex("7FAA"))

        # pytype: disable=attribute-error
        self.connection.socket.send.assert_called_once_with(
            bytearray.fromhex("7FAA"))