def test_incremental_collector(self): # Start the daemon: d = Daemon() d.start() # Create a collector: self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') # Send an event: self.sendEvent('ENEMY_KILLED', [1234], 1) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1234, 1) # Send the event again: self.sendEvent('ENEMY_KILLED', [1234], 2) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1234, 2) # Now, 2 players kill an enemy: self.sendEvent('ENEMY_KILLED', [1234, 1235], 1) self.expectStat('enemies_killed', 'avatar', 1234, 1) self.expectStat('enemies_killed', 'avatar', 1235, 1) # Cleanup: d.stop() self.resetDatabase()
def test_highscore(self): # Start the daemon: d = Daemon() d.start() # Create two collectors: self.doRPC('add_highscore', name='cd_wave', event='CD_WAVE', reversed=False) self.doRPC('add_highscore', name='bp_time', event='BP_TIME', reversed=True) # Send events: self.sendEvent('CD_WAVE', [1234, 1235], 100) self.sendEvent('BP_TIME', [1234], 50) # Check the DB: self.expectHighscore('cd_wave', 1234, 100) self.expectHighscore('cd_wave', 1235, 100) self.expectHighscore('bp_time', 1234, 50) # 1234 sets a new highscore: self.sendEvent('CD_WAVE', [1234], 200) self.expectHighscore('cd_wave', 1234, 200) self.sendEvent('BP_TIME', [1234], 40) self.expectHighscore('bp_time', 1234, 40) # Restart to ensure load_highscore_entries is working d.restart() # 1235 scores 40 in cd_wave, but gets to keep 100 in the highscore: self.sendEvent('CD_WAVE', [1235], 40) self.expectHighscore('cd_wave', 1235, 100) # 1234 scores 80 in bp_time, but gets to keep 40 in the highscore: self.sendEvent('BP_TIME', [1234], 80) self.expectHighscore('bp_time', 1234, 40) # Cleanup: d.stop() self.resetDatabase()
def test_leaderboards(self): # Start the daemon: d = Daemon() d.start() # Create a collector: self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') # Assign guilds: self.sendEvent('AV_GUILD', [1000], 20000) self.sendEvent('AV_GUILD', [1001], 20000) self.sendEvent('AV_GUILD', [1002], 20001) # Send a few events: self.sendEvent('ENEMY_KILLED', [1000], 1) self.sendEvent('ENEMY_KILLED', [1001], 1) self.sendEvent('ENEMY_KILLED', [1002], 2) self.sendEvent('ENEMY_KILLED', [1002], 3) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1000, 1) self.expectStat('enemies_killed', 'avatar', 1001, 1) self.expectStat('enemies_killed', 'avatar', 1002, 5) self.expectStat('enemies_killed', 'guild', 20000, 2) self.expectStat('enemies_killed', 'guild', 20001, 5) self.expectLeaderboard('avatar:enemies_killed', 1000, 2, 1) self.expectLeaderboard('avatar:enemies_killed', 1001, 1, 1) self.expectLeaderboard('avatar:enemies_killed', 1002, 0, 5) self.expectLeaderboard('guild:enemies_killed', 20000, 1, 2) self.expectLeaderboard('guild:enemies_killed', 20001, 0, 5) # Ban 1001 and 20001: self.doRPC('ban', id=1001) self.doRPC('ban', id=20001) time.sleep(1) self.expectOverallLeaderboard('avatar:enemies_killed', 1000, 1, 1) self.expectOverallLeaderboard('avatar:enemies_killed', 1001, None, None) self.expectOverallLeaderboard('avatar:enemies_killed', 1002, 0, 5) self.expectOverallLeaderboard('guild:enemies_killed', 20000, 0, 2) self.expectOverallLeaderboard('guild:enemies_killed', 20001, None, None) # Cleanup: d.stop() self.resetDatabase()
def test_cache(self): # Start the daemon: d = Daemon() d.start() # Create a collector: self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') # 1234 comes online: self.sendEvent('AV_ONLINE', [1234]) # Give 1234 a guild: self.sendEvent('AV_GUILD', [1234], 10000) # Send an event: self.sendEvent('ENEMY_KILLED', [1234], 1) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1234, 1) self.expectStat('enemies_killed', 'guild', 10000, 1) # Oh no! The daemon died! Restart it: d.restart() # Send the event again: self.sendEvent('ENEMY_KILLED', [1234], 1) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1234, 1) self.expectStat('enemies_killed', 'guild', 10000, 1) # Cleanup: d.stop() self.resetDatabase()
def test_periodic_collector(self): # Start the daemon: d = Daemon() d.start() # Create a collector: self.doRPC('add_periodic', name='shard_pop', event='SHARD_POP', period=3) # Send an event: self.sendEvent('SHARD_POP', [30000, 30001], 1) # Wait 4 seconds: time.sleep(4) # Check the DB: self.expectPeriodicStat('shard_pop', 30000, 1) self.expectPeriodicStat('shard_pop', 30001, 1) # Send more events: self.sendEvent('SHARD_POP', [30000, 30001], 10) self.sendEvent('SHARD_POP', [30000], 20) self.sendEvent('SHARD_POP', [30001], 15) # ... ZZZ ... time.sleep(4) # Should register only the last value for each doId: self.expectPeriodicStat('shard_pop', 30000, 20) self.expectPeriodicStat('shard_pop', 30001, 15) # Cleanup: d.stop() self.resetDatabase()
def test_rpc(self): # Start the daemon: d = Daemon() d.start() # Initially, there should be no collector: result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals(result['result'], {}) # Create a collector: result = self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') self.assertTrue(result['success']) # Let's do it again! result = self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') self.assertFalse(result['success']) self.assertEquals(result['error'], 'enemies_killed already exists') # Oops, forgot a param: result = self.doRPC('add_incremental', name='ships_sunk') self.assertFalse(result['success']) self.assertEquals(result['error'], "invalid or missing 'event' param") result = self.doRPC('add_incremental', event='SHIPS_SUNK') self.assertFalse(result['success']) self.assertEquals(result['error'], "invalid or missing 'name' param") # Try sending invalid params: result = self.doRPC('add_incremental', name='ships_sunk', event=1) self.assertFalse(result['success']) self.assertEquals(result['error'], "invalid or missing 'event' param") result = self.doRPC('add_incremental', name=1, event='SHIPS_SUNK') self.assertFalse(result['success']) self.assertEquals(result['error'], "invalid or missing 'name' param") # Make sure that the list isn't broken: result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals( result['result'], { 'enemies_killed': { 'event': 'ENEMY_KILLED', 'name': 'enemies_killed', 'type': 'incremental' } }) # Remove enemies_killed: result = self.doRPC('remove', name='enemies_killed') self.assertTrue(result['success']) result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals(result['result'], {}) # Add a periodic collector: result = self.doRPC('add_periodic', name='shard_pop', event='SHARD_POP') self.assertFalse(result['success']) # Oops, forgot a param self.assertEquals(result['error'], "invalid or missing 'period' param") result = self.doRPC('add_periodic', name='shard_pop', event='SHARD_POP', period='1234 years') self.assertFalse(result['success']) # Good joke self.assertEquals(result['error'], "invalid or missing 'period' param") result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals(result['result'], {}) # Fine, let's do it right: result = self.doRPC('add_periodic', name='shard_pop', event='SHARD_POP', period=5) self.assertTrue(result['success']) result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals( result['result'], { 'shard_pop': { 'event': 'SHARD_POP', 'name': 'shard_pop', 'period': 5, 'type': 'periodic' } }) # Remove shard_pop: result = self.doRPC('remove', name='shard_pop') self.assertTrue(result['success']) result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals(result['result'], {}) # Add highscore collectors: result = self.doRPC('add_highscore', name='cd_wave', event='CD_WAVE', reversed=False) self.assertTrue(result['success']) result = self.doRPC('add_highscore', name='bp_time', event='BP_TIME', reversed=True) self.assertTrue(result['success']) result = self.doRPC('list') self.assertTrue(result['success']) self.assertEquals( result['result'], { 'cd_wave': { 'event': 'CD_WAVE', 'name': 'cd_wave', 'reversed': False, 'type': 'highscore' }, 'bp_time': { 'event': 'BP_TIME', 'name': 'bp_time', 'reversed': True, 'type': 'highscore' } }) # Cleanup: d.stop() self.resetDatabase()
def test_stat_manager(self): # Start the daemon: d = Daemon() d.start() # Create a collector: self.doRPC('add_incremental', name='enemies_killed', event='ENEMY_KILLED') # 1234 comes online: self.sendEvent('AV_ONLINE', [1234]) # Send an event: self.sendEvent('ENEMY_KILLED', [1234], 1) # Check the DB: self.expectStat('enemies_killed', 'avatar', 1234, 1) # Give 1234 a guild: self.sendEvent('AV_GUILD', [1234], 10000) # Send the event again: self.sendEvent('ENEMY_KILLED', [1234], 2) # This time, check the guild stats too: self.expectStat('enemies_killed', 'avatar', 1234, 2) self.expectStat('enemies_killed', 'guild', 10000, 2) # Change their guild and do it again: self.sendEvent('AV_GUILD', [1234], 10001) self.sendEvent('ENEMY_KILLED', [1234], 7) self.expectStat('enemies_killed', 'avatar', 1234, 7) self.expectStat('enemies_killed', 'guild', 10000, 0) # No change self.expectStat('enemies_killed', 'guild', 10001, 7) # No guild: self.sendEvent('AV_GUILD', [1234], 0) self.sendEvent('ENEMY_KILLED', [1234], 5) self.expectStat('enemies_killed', 'avatar', 1234, 5) self.expectStat('enemies_killed', 'guild', 10000, 0) # No change self.expectStat('enemies_killed', 'guild', 10001, 0) # No change # Back to 10000: self.sendEvent('AV_GUILD', [1234], 10000) self.sendEvent('ENEMY_KILLED', [1234], 2) self.expectStat('enemies_killed', 'avatar', 1234, 2) self.expectStat('enemies_killed', 'guild', 10000, 2) self.expectStat('enemies_killed', 'guild', 10001, 0) # No change # Avatar goes offline: self.sendEvent('AV_OFFLINE', [1234], 0) # I'd tell you an UDP joke, but you wouldn't get it self.sendEvent('ENEMY_KILLED', [1234], 5) self.expectStat('enemies_killed', 'avatar', 1234, 5) self.expectStat('enemies_killed', 'guild', 10000, 0) # No change self.expectStat('enemies_killed', 'guild', 10001, 0) # No change # Cleanup: d.stop() self.resetDatabase()