11import asyncio
22from datetime import datetime , timezone
33
4+ import pytest
5+
46from tests .utils import fast_forward
57
68from .conftest import connect_and_sign_in , read_until_command
7- from .test_game import open_fa , queue_players_for_matchmaking , start_search
9+ from .test_game import (
10+ client_response ,
11+ open_fa ,
12+ queue_players_for_matchmaking ,
13+ send_player_options ,
14+ start_search
15+ )
816from .test_parties import accept_party_invite , invite_to_party
17+ from .test_teammatchmaker import \
18+ queue_players_for_matchmaking as queue_players_for_matchmaking_2v2
919
1020
1121@fast_forward (360 )
@@ -18,8 +28,7 @@ async def test_violation_for_guest_timeout(mocker, lobby_server):
1828
1929 # The player that queued last will be the host
2030 async def launch_game_and_timeout_guest ():
21- await read_until_command (host , "game_launch" )
22- await open_fa (host )
31+ await client_response (host , timeout = 60 )
2332 await read_until_command (host , "game_info" )
2433
2534 await read_until_command (guest , "game_launch" )
@@ -110,6 +119,126 @@ async def launch_game_and_timeout_guest():
110119 }
111120
112121
122+ @fast_forward (360 )
123+ async def test_violation_established_peer (mocker , lobby_server ):
124+ mocker .patch (
125+ "server.ladder_service.violation_service.datetime_now" ,
126+ return_value = datetime (2022 , 2 , 5 , tzinfo = timezone .utc )
127+ )
128+ protos , ids = await queue_players_for_matchmaking_2v2 (lobby_server )
129+ host , guest1 , guest2 , guest3 = protos
130+ host_id , guest1_id , guest2_id , guest3_id = ids
131+
132+ # Connect all players to the host
133+ await asyncio .gather (* [
134+ client_response (proto , timeout = 60 )
135+ for proto in protos
136+ ])
137+ await send_player_options (
138+ host ,
139+ [host_id , "Color" , 1 ],
140+ [guest1_id , "Color" , 2 ],
141+ [guest2_id , "Color" , 3 ],
142+ [guest3_id , "Color" , 4 ],
143+ )
144+
145+ # Set up connection matrix
146+ for id in (guest1_id , guest2_id , guest3_id ):
147+ await host .send_message ({
148+ "target" : "game" ,
149+ "command" : "EstablishedPeer" ,
150+ "args" : [id ],
151+ })
152+ for id in (host_id , guest2_id ):
153+ await guest1 .send_message ({
154+ "target" : "game" ,
155+ "command" : "EstablishedPeer" ,
156+ "args" : [id ],
157+ })
158+ for id in (host_id , guest1_id ):
159+ await guest2 .send_message ({
160+ "target" : "game" ,
161+ "command" : "EstablishedPeer" ,
162+ "args" : [id ],
163+ })
164+ # Guest3 only connects to the host
165+ await guest3 .send_message ({
166+ "target" : "game" ,
167+ "command" : "EstablishedPeer" ,
168+ "args" : [host_id ],
169+ })
170+
171+ await read_until_command (host , "match_cancelled" , timeout = 120 )
172+ msg = await read_until_command (guest3 , "search_violation" , timeout = 10 )
173+ assert msg == {
174+ "command" : "search_violation" ,
175+ "count" : 1 ,
176+ "time" : "2022-02-05T00:00:00+00:00" ,
177+ }
178+ for proto in (host , guest1 , guest2 ):
179+ with pytest .raises (asyncio .TimeoutError ):
180+ await read_until_command (proto , "search_violation" , timeout = 10 )
181+
182+
183+ @fast_forward (360 )
184+ async def test_violation_established_peer_multiple (mocker , lobby_server ):
185+ mocker .patch (
186+ "server.ladder_service.violation_service.datetime_now" ,
187+ return_value = datetime (2022 , 2 , 5 , tzinfo = timezone .utc )
188+ )
189+ protos , ids = await queue_players_for_matchmaking_2v2 (lobby_server )
190+ host , guest1 , guest2 , guest3 = protos
191+ host_id , guest1_id , guest2_id , guest3_id = ids
192+
193+ # Connect all players to the host
194+ await asyncio .gather (* [
195+ client_response (proto , timeout = 60 )
196+ for proto in protos
197+ ])
198+ await send_player_options (
199+ host ,
200+ [host_id , "Color" , 1 ],
201+ [guest1_id , "Color" , 2 ],
202+ [guest2_id , "Color" , 3 ],
203+ [guest3_id , "Color" , 4 ],
204+ )
205+
206+ # Set up connection matrix
207+ for id in (guest1_id , guest2_id , guest3_id ):
208+ await host .send_message ({
209+ "target" : "game" ,
210+ "command" : "EstablishedPeer" ,
211+ "args" : [id ],
212+ })
213+ # Guests only connect to the host
214+ await guest1 .send_message ({
215+ "target" : "game" ,
216+ "command" : "EstablishedPeer" ,
217+ "args" : [host_id ],
218+ })
219+ await guest2 .send_message ({
220+ "target" : "game" ,
221+ "command" : "EstablishedPeer" ,
222+ "args" : [host_id ],
223+ })
224+ await guest3 .send_message ({
225+ "target" : "game" ,
226+ "command" : "EstablishedPeer" ,
227+ "args" : [host_id ],
228+ })
229+
230+ await read_until_command (host , "match_cancelled" , timeout = 120 )
231+ for proto in (guest1 , guest2 , guest3 ):
232+ msg = await read_until_command (proto , "search_violation" , timeout = 10 )
233+ assert msg == {
234+ "command" : "search_violation" ,
235+ "count" : 1 ,
236+ "time" : "2022-02-05T00:00:00+00:00" ,
237+ }
238+ with pytest .raises (asyncio .TimeoutError ):
239+ await read_until_command (host , "search_violation" , timeout = 10 )
240+
241+
113242@fast_forward (360 )
114243async def test_violation_persisted_across_logins (mocker , lobby_server ):
115244 mocker .patch (
0 commit comments