Skip to content

Commit 034651a

Browse files
Bischoff, Silassbischoff-ai
Bischoff, Silas
authored andcommittedMay 7, 2019
Add backend unit tests and some fixes
1 parent d4a40f1 commit 034651a

File tree

2 files changed

+62
-19
lines changed

2 files changed

+62
-19
lines changed
 

‎pygase/backend.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -192,29 +192,24 @@ def dispatch_event(
192192
def get_ack_callback(connection):
193193
return lambda: ack_callback(connection)
194194

195+
timeout_callback = None
195196
if retries > 0:
196197

197-
def get_timeout_callback(connection):
198-
return lambda: self.dispatch_event(
199-
event_type, *args, connection=connection, retries=retries - 1, ack_callback=ack_callback, **kwargs
200-
) or logger.warning(f"Event of type {event_type} timed out. Retrying to send event to server.")
201-
202-
else:
203-
204-
def get_timeout_callback(connection): # pylint: disable=unused-argument
205-
return None
198+
timeout_callback = lambda: self.dispatch_event(
199+
event_type,
200+
*args,
201+
target_client=target_client,
202+
retries=retries - 1,
203+
ack_callback=ack_callback,
204+
**kwargs,
205+
) or logger.warning(f"Event of type {event_type} timed out. Retrying to send event to server.")
206206

207207
if target_client == "all":
208208
for connection in self.connections.values():
209-
connection.dispatch_event(
210-
event, get_ack_callback(connection), get_timeout_callback(connection), **kwargs
211-
)
209+
connection.dispatch_event(event, get_ack_callback(connection), timeout_callback, **kwargs)
212210
else:
213211
self.connections[target_client].dispatch_event(
214-
event,
215-
get_ack_callback(self.connections[target_client]),
216-
get_timeout_callback(self.connections[target_client]),
217-
**kwargs,
212+
event, get_ack_callback(self.connections[target_client]), timeout_callback, **kwargs
218213
)
219214

220215
# add advanced type checking for handler functions

‎tests/backend_test.py

+51-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from helpers import assert_timeout
88

9-
from pygase.backend import Server, GameStateStore, GameStateMachine
9+
from pygase.backend import Server, GameStateStore, GameStateMachine, Backend
1010
from pygase.gamestate import GameState, GameStateUpdate, GameStatus
1111
from pygase.connection import ClientPackage
1212
from pygase.event import UniversalEventHandler
@@ -15,8 +15,8 @@
1515
class TestServer:
1616
def test_instantiation(self):
1717
server = Server(GameStateStore())
18-
assert server.game_state_store.__class__ == GameStateStore
19-
assert server._universal_event_handler.__class__ == UniversalEventHandler
18+
assert isinstance(server.game_state_store, GameStateStore)
19+
assert isinstance(server._universal_event_handler, UniversalEventHandler)
2020

2121
def test_run_async(self):
2222
server = Server(GameStateStore())
@@ -30,6 +30,35 @@ async def test_task():
3030

3131
assert curio.run(test_task)
3232

33+
def test_dispatch_event(self):
34+
server = Server(GameStateStore())
35+
36+
class MockConnection:
37+
called_with = []
38+
39+
def dispatch_event(self, *args, **kwargs):
40+
self.called_with.append((args, kwargs))
41+
42+
foo_connection = MockConnection()
43+
server.connections[("foo", 1)] = foo_connection
44+
server.connections[("bar", 1)] = MockConnection()
45+
server.dispatch_event("BIZBAZ")
46+
assert len(MockConnection.called_with) == 2
47+
server.dispatch_event("BIZBAZ", "foobar", target_client=("foo", 1), retries=3, ack_callback=id)
48+
assert len(MockConnection.called_with) == 3
49+
foobar_dispatch = MockConnection.called_with[-1]
50+
assert foobar_dispatch[0][0].handler_args == ["foobar"]
51+
assert foobar_dispatch[0][1]() == id(foo_connection)
52+
foobar_dispatch[0][2]()
53+
assert len(MockConnection.called_with) == 4
54+
foobar_dispatch = MockConnection.called_with[-1]
55+
foobar_dispatch[0][2]()
56+
assert len(MockConnection.called_with) == 5
57+
foobar_dispatch = MockConnection.called_with[-1]
58+
foobar_dispatch[0][2]()
59+
assert len(MockConnection.called_with) == 6
60+
assert MockConnection.called_with[-1][0][2] is None
61+
3362

3463
class TestGameStateStore:
3564
def test_instantiation(self):
@@ -61,6 +90,13 @@ def test_safe_concurrent_cache_access(self):
6190
assert counter == 3
6291
assert len(store.get_update_cache()) == 2
6392

93+
def test_cache_size(self):
94+
store = GameStateStore()
95+
for i in range(2 * store._update_cache_size):
96+
assert len(store.get_update_cache()) == min(i + 1, store._update_cache_size)
97+
store.push_update(GameStateUpdate(i + 1))
98+
assert sum(store.get_update_cache()).time_order == i + 1
99+
64100

65101
class TestGameStateMachine:
66102
def test_instantiation(self):
@@ -97,3 +133,15 @@ async def test_task():
97133
assert store.get_game_state().test == 10
98134
assert state_machine.game_time == 10
99135
assert store.get_game_state().game_status == GameStatus.get("Paused")
136+
137+
138+
class TestBackend:
139+
def test_instantiation(self):
140+
time_step = lambda game_state, dt: {}
141+
backend = Backend(initial_game_state=GameState(), time_step_function=time_step)
142+
assert isinstance(backend.game_state_store, GameStateStore)
143+
assert backend.game_state_store._game_state == GameState()
144+
assert isinstance(backend.game_state_machine, GameStateMachine)
145+
assert backend.game_state_machine.time_step == time_step
146+
assert isinstance(backend.server, Server)
147+
assert backend.server.game_state_store == backend.game_state_store

0 commit comments

Comments
 (0)
Please sign in to comment.