6
6
7
7
from helpers import assert_timeout
8
8
9
- from pygase .backend import Server , GameStateStore , GameStateMachine
9
+ from pygase .backend import Server , GameStateStore , GameStateMachine , Backend
10
10
from pygase .gamestate import GameState , GameStateUpdate , GameStatus
11
11
from pygase .connection import ClientPackage
12
12
from pygase .event import UniversalEventHandler
15
15
class TestServer :
16
16
def test_instantiation (self ):
17
17
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 )
20
20
21
21
def test_run_async (self ):
22
22
server = Server (GameStateStore ())
@@ -30,6 +30,35 @@ async def test_task():
30
30
31
31
assert curio .run (test_task )
32
32
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
+
33
62
34
63
class TestGameStateStore :
35
64
def test_instantiation (self ):
@@ -61,6 +90,13 @@ def test_safe_concurrent_cache_access(self):
61
90
assert counter == 3
62
91
assert len (store .get_update_cache ()) == 2
63
92
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
+
64
100
65
101
class TestGameStateMachine :
66
102
def test_instantiation (self ):
@@ -97,3 +133,15 @@ async def test_task():
97
133
assert store .get_game_state ().test == 10
98
134
assert state_machine .game_time == 10
99
135
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