def test_Apply(self): conf = GameConfig() conf.frames_per_sec = 120 conf.game_length = 60 conf.rounds = 2 conf.screen_width = 1024 conf.screen_height = 756 conf.paddle_width = 32 conf.paddle_height = 50 conf.ball_size = 100 conf.player_id = 2 conf.cool_down = 5 e = UDPGameEngine() conf.Apply(e) self.assertTrue(e.player_id == conf.player_id) self.assertTrue(e.key_cool_down_time == conf.cool_down) s = e.state self.assertTrue(s.frames_per_sec == conf.frames_per_sec) self.assertTrue(s.game_length == conf.game_length) self.assertTrue(s.rounds == conf.rounds) self.assertTrue(s.rotation_length == conf.frames_per_sec * 10) self.assertTrue(s.round_length == conf.frames_per_sec * 30) self.assertTrue(s.screen.half_width == conf.screen_width // 2) self.assertTrue(s.screen.half_height == conf.screen_height // 2) self.assertTrue(s.paddle_left.half_width == conf.paddle_width // 2) self.assertTrue(s.paddle_right.half_height == \ conf.paddle_height // 2) self.assertTrue(s.ball.half_width == conf.ball_size // 2)
def test_SerializeAndDeserialize(self): conf = GameConfig() conf.player_size = 5 conf.game_length = 40 conf.frames_per_sec = 130 conf.screen_width = 1024 conf.screen_height = 756 conf.buffer_region = 89 conf.ball_wall_offset_x = 9357 conf.ball_wall_offset_y = 932 conf.paddle_width = 32 conf.paddle_height = 50 conf.ball_vel = 3 conf.ball_size = 100 conf.rounds = 8 conf.buffer_delay = 1 conf.player_id = 2 # Ignore the event type bytes. b = conf.Serialize()[4:] test = GameConfig() test.Deserialize(b) self.assertTrue(conf == test, msg='{0} != {1}'.format(conf, test))
def template_Run(self, n, svr_do_sync, user_do_sync): assert svr_do_sync == user_do_sync k = MockKeyboard() k.inputs = [1]*30+[0]*100 r = NullRenderer() conf = GameConfig() conf.player_size = n conf.frames_per_sec = 32767 conf.game_length = 0.05 conf.post_game_time = 0 conf.do_sync = svr_do_sync conf.sync_timeout = 0.1 conf.sync_rate = 100 conf.buffer_size = 64 # Try to avoid clients dying at the end of handshake. test_tries = 40 status = 0 for i in range(0, test_tries): s = UDPSocket() s.Open() s.Bind(('', 0)) svraddr = s.sock.getsockname() ps = [] qs = [] clients = [] # Try only once here. In case of server failure and client success, # we must re-run the test as a whole. server_tries = 1 server_timeout = 0.5 client_tries = 1 # Send only once. To keep tests short, games run only briefly. # If the resend happens after the game is ended, the test fails. client_resend = 1 client_timeout = 0.5 user_conf = GameConfig() user_conf.do_sync = user_do_sync user_conf.sync_timeout = conf.sync_timeout * n for i in range(0, n): q = multiprocessing.Queue() c = UDPClient() p = multiprocessing.Process(target=\ UDPServerTestPickleJar_Run, args=(client_tries, client_resend, client_timeout, c, svraddr, r, k, user_conf, q)) p.start() ps.append(p) qs.append(q) svr = UDPServer() svr.buffer_time = 0 try: status = svr.Run(s, False, conf, server_tries, server_timeout) except Exception as ex: logger.exception(ex) status = 100 # Error in the server, stop the test results = [] for i in range(0, n): results.append(qs[i].get()) ps[i].join() s.Close() self.assertTrue(status != 100) if status == 0: break self.assertTrue(status == 0) for i in range(0, n): self.assertTrue(results[i])
def test_ne_1(self): conf = GameConfig() conf.game_length = 413 conf2 = GameConfig() conf2.game_length = 32 self.assertTrue(conf != conf2)
def test_eq_3(self): conf = GameConfig() conf.game_length = 51 conf2 = GameConfig() conf2.game_length = 51 self.assertTrue(conf == conf2)