Skip to content

Commit

Permalink
fix race condition in game instantiation inside onSync
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolodavis committed Jan 22, 2019
1 parent 7c0521c commit 3334d38
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/master/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class Master {
* along with a deltalog.
*/
async onUpdate(action, stateID, gameID, playerID) {
const key = `${this.game.name}:${gameID}`;
const key = gameID;
let state = await this.storageAPI.get(key);

if (state === undefined) {
Expand Down Expand Up @@ -170,7 +170,7 @@ export class Master {
* Returns the latest game state and the entire log.
*/
async onSync(gameID, playerID, numPlayers) {
const key = `${this.game.name}:${gameID}`;
const key = gameID;

let state = await this.storageAPI.get(key);

Expand All @@ -179,6 +179,7 @@ export class Master {
if (state === undefined) {
state = InitializeGame({ game: this.game, numPlayers });
await this.storageAPI.set(key, state);
state = await this.storageAPI.get(key);
}

const filteredState = {
Expand Down
2 changes: 1 addition & 1 deletion src/master/master.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ describe('update', async () => {
});

test('invalid gameID', async () => {
await master.onUpdate(action, 1, 'unknown', '1');
await master.onUpdate(action, 1, 'default:unknown', '1');
expect(sendAll).not.toHaveBeenCalled();
expect(error).toHaveBeenCalledWith(
`game not found, gameID=[default:unknown]`
Expand Down

0 comments on commit 3334d38

Please sign in to comment.