Beispiel #1
0
    def test09_next_owner_id_race_condition(self):
        owner = 11
        player1 = 12
        player2 = 13

        # Create the game.
        response = yield self.service.handle([], {'action': ['create'],
                                                  'owner_id': [owner]})
        game_id = response['game_id']
        self.add_players_to_game(game_id, [owner, player1, player2])
        # Complete the game.
        yield self.complete_game(game_id, owner, player1, player2)

        table = self.table_instance.game2table[game_id]

        self.test_done = False

        self.counter = 0

        def listener(args):
            if not self.test_done:
                self.counter += 1
                modified = args['modified'][0]
                table.poll({'modified': [modified]}).addCallback(listener)
            return args

        modified = table.get_modified()
        poll = table.poll({'modified': [modified]}).addCallback(listener)

        d1 = table.update_next_owner_id()
        d2 = table.update_next_owner_id()
        d3 = table.update_next_owner_id()

        d = defer.DeferredList([d1, d2, d3])

        # Wait for all three update operations to finish...
        yield d
        # ...and then wait for the next_owner change to be dispatched by the poll.
        yield poll

        self.assertEquals(self.counter, 1)

        # Break the listen loop so that the test runner doesn't complain
        # about a dirty reactor.
        self.test_done = True
        table.touch({})
        # For the same reason cancel the next_game_timer.
        table.stop_timer(table.next_game_timer)
Beispiel #2
0
    def test06_next_game_timeout(self):
        owner = 98
        player1 = 12
        player2 = 78

        # Create game
        response = yield self.service.handle([], {'action': ['create'],
                                                  'owner_id': [owner]})
        game_id = response['game_id']

        sql = "INSERT INTO tabs (player_id, game_id, created) VALUES (%d, %d, datetime('now'))"
        for player_id in [owner, player1, player2]:
            yield self.service.db.runQuery(sql % (player_id, game_id))

        # Change the next game timeout from the default to 0.2 seconds for the test.
        table = self.table_instance.game2table[game_id]
        table.NEXT_GAME_TIMEOUT = 0.2

        # Complete the game
        yield self.complete_game(game_id, owner, player1, player2)

        state = yield self.table_instance.state({'type': ['table'],
                                                 'game_id': [game_id],
                                                 'player_id': [player1]})
        self.assertEqual(state, [{'game_id': game_id,
                                  'next_game_id': None,
                                  'next_owner_id': player1},
                                 [player1]])

        # Start a poll on this table to know when the next owner will change.
        poll = self.table_instance.poll({'game_id': [game_id],
                                         'modified': [table.get_modified()]})
        result = yield poll

        state = yield self.table_instance.state({'type': ['table'],
                                                 'game_id': [game_id],
                                                 'player_id': [player1]})
        # Next owner took too long to create the game,
        # so player2 was chosen as the "new" next owner.
        self.assertEqual(state, [{'game_id': game_id,
                                  'next_game_id': None,
                                  'next_owner_id': player2},
                                 [player2]])

        # Cancel the timers, so that the test runner doesn't complain
        # about a "dirty" reactor.
        table.stop_timer(table.next_game_timer)