Skip to content

Commit

Permalink
Add another test for searches being cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Oct 7, 2020
1 parent fc828d1 commit 61dbfc8
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 5 deletions.
24 changes: 19 additions & 5 deletions tests/integration_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,17 +170,31 @@ async def perform_login(
})


async def read_until(
proto: Protocol, pred: Callable[[Dict[str, Any]], bool]
async def _read_until(
proto: Protocol,
pred: Callable[[Dict[str, Any]], bool]
) -> Dict[str, Any]:
while True:
msg = await proto.read_message()
try:
if pred(msg):
return msg
except (KeyError, ValueError):
logging.getLogger().info("read_until predicate raised during message: {}".format(msg))
except KeyError:
pass
except Exception:
logging.getLogger().warning(
"read_until predicate raised during message: %s",
msg,
exc_info=True
)


async def read_until(
proto: Protocol,
pred: Callable[[Dict[str, Any]], bool],
timeout: float = 60
) -> Dict[str, Any]:
return await asyncio.wait_for(_read_until(proto, pred), timeout=timeout)


async def read_until_command(
Expand All @@ -189,7 +203,7 @@ async def read_until_command(
timeout: float = 60
) -> Dict[str, Any]:
return await asyncio.wait_for(
read_until(proto, lambda msg: msg.get("command") == command),
_read_until(proto, lambda msg: msg.get("command") == command),
timeout=timeout
)

Expand Down
64 changes: 64 additions & 0 deletions tests/integration_tests/test_teammatchmaker.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,70 @@ async def test_game_matchmaking_multiqueue(lobby_server):
assert msg["faction"] == 2


@fast_forward(60)
async def test_game_matchmaking_multiqueue_multimatch(lobby_server):
"""
Scenario where both queues could possibly generate a match.
Queues:
ladder1v1 - 2 players join
tmm2v2 - 4 players join
Result:
Either one of the queues generates a match, but not both.
"""
protos, _ = await connect_players(lobby_server)

await asyncio.gather(*[
read_until_command(proto, "game_info") for proto in protos
])

ladder1v1_tasks = [
proto.send_message({
"command": "game_matchmaking",
"state": "start",
"faction": "uef",
"queue_name": "ladder1v1"
})
for proto in protos[:2]
]
await asyncio.gather(*[
proto.send_message({
"command": "game_matchmaking",
"state": "start",
"faction": "aeon",
"queue_name": "tmm2v2"
})
for proto in protos
] + ladder1v1_tasks)
msg1 = await read_until_command(protos[0], "match_found")
msg2 = await read_until_command(protos[1], "match_found")

matched_queue = msg1["queue"]
if matched_queue == "ladder1v1":
with pytest.raises(asyncio.TimeoutError):
await read_until_command(protos[2], "match_found", timeout=3)
with pytest.raises(asyncio.TimeoutError):
await read_until_command(protos[3], "match_found", timeout=3)
with pytest.raises(asyncio.TimeoutError):
await read_until_command(protos[2], "search_info", timeout=3)
with pytest.raises(asyncio.TimeoutError):
await read_until_command(protos[3], "search_info", timeout=3)
else:
await read_until_command(protos[2], "match_found", timeout=3)
await read_until_command(protos[3], "match_found", timeout=3)

assert msg1 == msg2

def other_cancelled(msg):
return (
msg["command"] == "search_info"
and msg["queue_name"] != matched_queue
)
msg1 = await read_until(protos[0], other_cancelled, timeout=3)
msg2 = await read_until(protos[1], other_cancelled, timeout=3)
assert msg1 == msg2
assert msg1["state"] == "stop"


@fast_forward(60)
async def test_game_matchmaking_timeout(lobby_server):
protos, _ = await queue_players_for_matchmaking(lobby_server)
Expand Down

0 comments on commit 61dbfc8

Please sign in to comment.