Example #1
0
    def test_event_ack_callbacks(self):
        async def sendto(*args):
            pass

        sock = type("socket", (), {"sendto": sendto})()

        class callback:
            count = 0

            def __init__(self):
                callback.count += 1

        connection = Connection(("", 0), None)
        event = Event("TEST")
        connection.dispatch_event(event, ack_callback=callback)
        curio.run(connection._send_next_package, sock)
        curio.run(connection._recv, Package(Header(1, 1, "0" * 32)))
        assert callback.count == 1
        curio.run(connection._recv, Package(Header(2, 1, "0" * 32)))
        assert callback.count == 1
        connection.dispatch_event(event, ack_callback=callback)
        connection.dispatch_event(event)
        connection.dispatch_event(event, ack_callback=callback)
        curio.run(connection._send_next_package, sock)
        assert connection.local_sequence == 2
        curio.run(connection._recv, Package(Header(3, 2, "1" + "0" * 31)))
        assert callback.count == 3
Example #2
0
    def test_event_timeout_calbacks(self):
        async def sendto(*args):
            pass

        sock = type("socket", (), {"sendto": sendto})()

        class callback:
            count = 0

            def __init__(self):
                callback.count += 1

        with freeze_time("2012-01-14 12:00:01") as frozen_time:
            connection = Connection(("", 0), None)
            event = Event("TEST")
            connection.dispatch_event(event, timeout_callback=callback)
            curio.run(connection._send_next_package, sock)
            curio.run(connection._recv, Package(Header(1, 1, "0" * 32)))
            assert callback.count == 0
            frozen_time.tick()
            frozen_time.tick()
            curio.run(connection._recv, Package(Header(2, 1, "0" * 32)))
            assert callback.count == 0
            connection.dispatch_event(event, timeout_callback=callback)
            curio.run(connection._send_next_package, sock)
            frozen_time.tick()
            frozen_time.tick()
            curio.run(connection._recv, Package(Header(3, 1, "0" * 32)))
            assert callback.count == 1
Example #3
0
 def test_recv_duplicate_package_in_sequence(self):
     connection = Connection(("host", 1234), None)
     connection.remote_sequence = Sqn(500)
     connection.ack_bitfield = "1" * 32
     curio.run(
         connection._recv,
         Package(Header(sequence=501, ack=500, ack_bitfield="1" * 32)))
     with pytest.raises(DuplicateSequenceError):
         curio.run(
             connection._recv(
                 Package(
                     Header(sequence=501, ack=500, ack_bitfield="1" * 32))))
Example #4
0
    def test_receive_events(self):
        class EventHandler:
            call_count = 0

            def __init__(self):
                self.events = []

            async def handle(self, event, **kwargs):
                EventHandler.call_count += 1
                self.events.append(event)

            def has_event_type(self, event_type):
                return True

        event_handler = EventHandler()
        connection = Connection(("", 0), event_handler)
        assert connection.event_handler == event_handler
        event = Event("TEST", 1, 2, 3, 4)
        package = Package(Header(1, 1, "1" * 32), [event, event])
        assert package.events == [event, event]
        assert connection.remote_sequence == 0
        curio.run(connection._recv, package)
        assert not connection._incoming_event_queue.empty()
        assert EventHandler.call_count == 0
        curio.run(connection._handle_next_event)
        assert EventHandler.call_count == 1
        assert not connection._incoming_event_queue.empty()
        assert event_handler.events == [event]
        curio.run(connection._handle_next_event)
        assert EventHandler.call_count == 2
        assert connection._incoming_event_queue.empty()
        assert event_handler.events == [event, event]
Example #5
0
 def test_recv_second_package_comes_first(self):
     connection = Connection(("host", 1234), None)
     assert connection.remote_sequence == 0
     assert connection.ack_bitfield == "0" * 32
     curio.run(connection._recv,
               Package(Header(sequence=2, ack=0, ack_bitfield="0" * 32)))
     assert connection.remote_sequence == 2
     assert connection.ack_bitfield == "0" * 32
Example #6
0
 def test_recv_second_package(self):
     connection = Connection(("host", 1234), None)
     connection.remote_sequence = Sqn(1)
     connection.ack_bitfield = "0" * 32
     curio.run(connection._recv,
               Package(Header(sequence=2, ack=1, ack_bitfield="0" * 32)))
     assert connection.remote_sequence == 2
     assert connection.ack_bitfield == "1" + "0" * 31
Example #7
0
 def test_recv_three_packages_arrive_out_of_sequence(self):
     connection = Connection(("host", 1234), None)
     connection.remote_sequence = Sqn(100)
     connection.ack_bitfield = "0110" + "1" * 28
     curio.run(
         connection._recv,
         Package(Header(sequence=101, ack=100, ack_bitfield="1" * 32)))
     assert connection.remote_sequence == 101
     assert connection.ack_bitfield == "10110" + "1" * 27
     curio.run(connection._recv,
               Package(Header(sequence=99, ack=100, ack_bitfield="1" * 32)))
     assert connection.remote_sequence == 101
     assert connection.ack_bitfield == "11110" + "1" * 27
     curio.run(connection._recv,
               Package(Header(sequence=96, ack=101, ack_bitfield="1" * 32)))
     assert connection.remote_sequence == 101
     assert connection.ack_bitfield == "1" * 32
Example #8
0
 def test_recv_first_package(self):
     connection = Connection(("host", 1234), None)
     assert connection.local_sequence == 0
     assert connection.remote_sequence == 0
     assert connection.ack_bitfield == "0" * 32
     curio.run(connection._recv,
               Package(Header(sequence=1, ack=0, ack_bitfield="0" * 32)))
     assert connection.local_sequence == 0
     assert connection.remote_sequence == 1
     assert connection.ack_bitfield == "0" * 32
Example #9
0
    def test_resolve_acks(self):
        async def sendto(*args):
            pass

        sock = type("socket", (), {"sendto": sendto})()
        connection = Connection(("", 0), None)
        curio.run(connection._send_next_package, sock)
        assert connection._pending_acks.keys() == {1}
        assert connection.latency == 0
        curio.run(connection._recv, Package(Header(1, 0, "0" * 32)))
        assert connection._pending_acks
        curio.run(connection._recv, Package(Header(2, 1, "0" * 32)))
        assert not connection._pending_acks
        assert connection.latency > 0
        for _ in range(1, 5):
            curio.run(connection._send_next_package, sock)
        assert connection._pending_acks.keys() == {2, 3, 4, 5}
        curio.run(connection._recv, Package(Header(3, 4, "01" + "0" * 30)))
        assert connection._pending_acks.keys() == {3, 5}
Example #10
0
 def test_add_event(self):
     package = Package(Header(1, 2, "0" * 32))
     event1 = Event("TEST", 1, 2, 3)
     event2 = Event("FOO", "Bar")
     package.add_event(event1)
     package.add_event(event2)
     assert len(package.events) == 2
     assert event1 in package.events and event2 in package.events
     with pytest.raises(OverflowError):
         package.get_bytesize()
         package.add_event(Event("BIG", bytes(2030)))
Example #11
0
    def test_package_timeout(self):
        async def sendto(*args):
            pass

        sock = type("socket", (), {"sendto": sendto})()
        connection = Connection(("", 0), None)
        with freeze_time("2012-01-14 12:00:01") as frozen_time:
            curio.run(connection._send_next_package, sock)
            assert connection._pending_acks.keys() == {1}
            frozen_time.tick()
            frozen_time.tick()
            curio.run(connection._recv, Package(Header(1, 0, "0" * 32)))
            assert not connection._pending_acks
            assert connection.latency == 0
Example #12
0
 def test_bytepacking(self):
     package = ServerPackage(Header(4, 5, "10" * 16), GameStateUpdate(2),
                             [Event("TEST", "Foo", "Bar")])
     datagram = package.to_datagram()
     unpacked_package = ServerPackage.from_datagram(datagram)
     assert package == unpacked_package
Example #13
0
 def test_bytepacking(self):
     package = ClientPackage(Header(4, 5, "10" * 16), 1,
                             [Event("TEST", "Foo", "Bar")])
     datagram = package.to_datagram()
     unpacked_package = ClientPackage.from_datagram(datagram)
     assert package == unpacked_package
Example #14
0
 def test_size_restriction(self):
     with pytest.raises(OverflowError) as error:
         Package(Header(1, 4, "0" * 32),
                 [Event("TEST", bytes(2048 - 13))]).to_datagram()
     assert str(
         error.value) == "Package exceeds the maximum size of 2048 bytes."
Example #15
0
 def test_protocol_ID_check(self):
     package = Package(Header(1, 2, "0" * 32))
     datagram = package.to_datagram()
     datagram = datagram[1:]
     with pytest.raises(ProtocolIDMismatchError):
         Package.from_datagram(datagram)