Skip to content

Commit

Permalink
Fixed bug with unsaved changes after searching the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Relrin committed Aug 1, 2018
1 parent 5b46dd6 commit 5f9b9fd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
2 changes: 0 additions & 2 deletions game-servers-pool/app/game_servers/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class RegisterGameServerSchema(Schema):
]
)
available_slots = fields.Integer(
attribute="available-slots",
load_from="available-slots",
allow_none=False,
required=True,
Expand All @@ -65,7 +64,6 @@ class RegisterGameServerSchema(Schema):
]
)
game_mode = fields.String(
attribute="game-mode",
load_from="game-mode",
required=True,
allow_none=False,
Expand Down
21 changes: 16 additions & 5 deletions game-servers-pool/app/workers/get_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,28 @@ async def get_game_server(self, raw_data):
{"game_mode": data['game-mode']}
]
}},
{'$sample': {'size': 1}}
{'$sample': {'size': 1}},
{'$addFields': {
"available_slots": {
"$subtract": ["$available_slots", data['required-slots']]
}
}}
]
result = await self.game_server_document.collection.aggregate(pipeline).to_list(1)

if result:
instance = result[0]
document_updated_data = result[0]
document_id = document_updated_data.pop('_id')
await self.game_server_document.collection.update_one(
{'_id': document_id},
{'$set': document_updated_data}
)

serializer = self.schema()
serialized_instance = serializer.dump(instance).data
document = serializer.dump(document_updated_data).data
else:
serialized_instance = None
return Response.with_content(serialized_instance)
document = None
return Response.with_content(document)

async def process_request(self, channel, body, envelope, properties):
response = await self.get_game_server(body)
Expand Down
56 changes: 54 additions & 2 deletions game-servers-pool/tests/test_get_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def test_worker_returns_a_random_server_from_a_list(sanic_server):
{
'host': '127.0.0.1',
'port': 9001,
'available_slots': 50,
'available_slots': 100,
'credentials': {
'token': 'super_secret_token2'
},
Expand All @@ -88,7 +88,7 @@ async def test_worker_returns_a_random_server_from_a_list(sanic_server):
{
'host': '127.0.0.1',
'port': 9002,
'available_slots': 10,
'available_slots': 100,
'credentials': {
'token': 'super_secret_token3'
},
Expand Down Expand Up @@ -122,6 +122,9 @@ async def test_worker_returns_a_random_server_from_a_list(sanic_server):
assert content['port'] == extracted_server.port
assert content['credentials'] == extracted_server.credentials

updated_servers = await GameServer.collection.count_documents({"available_slots": 90})
assert updated_servers == 1

await GameServer.collection.delete_many({})


Expand Down Expand Up @@ -207,3 +210,52 @@ async def test_worker_returns_a_validation_error_for_missing_fields(sanic_server
assert servers_count == 0

await GameServer.collection.delete_many({})


@pytest.mark.asyncio
async def test_worker_returns_a_validation_error_for_missing_fields(sanic_server):
await GameServer.collection.delete_many({})

objects = await create_game_servers([
{
'host': '127.0.0.1',
'port': 9000,
'available_slots': 100,
'credentials': {
'token': 'super_secret_token'
},
'game_mode': 'team-deathmatch'
}
])
game_server = objects[0]

client = RpcAmqpClient(
sanic_server.app,
routing_key=REQUEST_QUEUE,
request_exchange=REQUEST_EXCHANGE,
response_queue='',
response_exchange=RESPONSE_EXCHANGE
)
response = await client.send(payload={
'required-slots': 10,
'game-mode': 'team-deathmatch'
})

assert Response.EVENT_FIELD_NAME in response.keys()
assert Response.CONTENT_FIELD_NAME in response.keys()
content = response[Response.CONTENT_FIELD_NAME]

assert len(list(content.keys())) == 3
assert set(content.keys()) == {'host', 'port', 'credentials'}

filter_func = lambda obj: obj.host == content['host'] and obj.port == content['port'] # NOQA
extracted_server = list(filter(filter_func, objects))[0]

assert content['host'] == extracted_server.host
assert content['port'] == extracted_server.port
assert content['credentials'] == extracted_server.credentials

game_server = await GameServer.find_one({"_id": game_server["id"]})
assert game_server.available_slots == 90

await GameServer.collection.delete_many({})

0 comments on commit 5f9b9fd

Please sign in to comment.