class ParsingOfIndividualMessages(unittest.TestCase): def setUp(self): self.s = TCP_server(HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=TIMEOUT, test_mode=True) def tearDown(self): self.s.stop_server() self.s = None def test_17_good_messages(self): F,U,B,P,S = '988|F|46|67\n', '692|U|46|68\n', '295|B\n', '992|P|46|68\n', '604|S|46\n' # F: add follower and attempt to send message self.s.EP.process_event(F) self.assertEqual(self.s.clients[0].ID, 67) self.assertEqual(self.s.clients[0].followers[0], 46) # U: remove follower self.s.add_client(68,None,None) self.s.EP.process_event(U) self.assertFalse(46 in self.s.clients[1].followers) # B: Broadcast to all. Just make sure no exceptions thrown. self.s.EP.process_event(B) # P: Private message. Just make sure no exceptions thrown. self.s.EP.process_event(P) # S: Status update. Just make sure no exceptions thrown. self.s.clients[0].followers = range(2) self.s.EP.process_event(S) def test_18_bad_messages(self): F,U,B,P,S = '988|F|46\n', '692|U|46\n', '295|B|xxx\n', '992|P|46\n', '604|S\n' # F: to_user_id missing. Check no extra clients are created self.s.EP.process_event(F) self.assertEqual(len(self.s.clients), 0) # U: to_user_id missing. Check no exceptions are thrown self.s.EP.process_event(U) # B: Extra data in message. Check no exceptions are thrown for i in range(2): self.s.add_client(68,None,None) self.s.EP.process_event(B) # P: to_user_id missing. Check no exceptions are thrown self.s.EP.process_event(P) # S: from_user_id missing. Check no exceptions are thrown self.s.EP.process_event(S) def test_19_unknown_message_type(self): bad_msgs = ['0|X|0|0\n', 'xxx|F|yyy|zzz\n'] for msg in bad_msgs: self.s.EP.process_event(msg)
class ServerFunctions(unittest.TestCase): def setUp(self): self.s = TCP_server(HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=TIMEOUT, test_mode=True) def tearDown(self): self.s.stop_server() sleep(2) self.s = None def test_8_numerical_arguments(self): self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=-1) self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, maxqueue=0) self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, 0) def test_9_thread_start_point(self): # Starting server should introduce 4 new threads num_after_init = threading.active_count() self.s.start_server() sleep(5) self.assertEqual(threading.active_count()-num_after_init, 4) def test_10_stop_server(self): # Check no exceptions are raised regardless of connection state try: self.s.start_server() self.s.stop_server() self.s.stop_server() sleep(2) # wait for exceptions result = True except: result = False self.assertTrue(result) def test_11_add_client(self): self.s.add_client(0, None, None) self.assertEqual(len(self.s.clients), 1) self.s.add_client(0, None, None) self.assertEqual(len(self.s.clients), 1) def test_12_reset_timer(self): self.s.TO.start() sleep(1) self.s.reset_timer() after_reset = time() self.assertAlmostEqual(self.s.TO.get_last_reset(), after_reset, places=3)
class Clients(unittest.TestCase): def setUp(self): self.s = TCP_server(HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=TIMEOUT, test_mode=True) def tearDown(self): self.s = None def test_23_sending_fail(self): # Add client, pack with fake data and self.s.add_client(0, None, None) c = self.s.clients[0] c.conn = 'connection' c.addr = 'address' c.send('message') self.assertIsNone(c.conn) self.assertIsNone(c.addr)
class ServerFunctions(unittest.TestCase): def setUp(self): self.s = TCP_server(HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=TIMEOUT, test_mode=True) def tearDown(self): self.s.stop_server() sleep(2) self.s = None def test_8_numerical_arguments(self): self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=-1) self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, maxqueue=0) self.assertRaises(ValueError, TCP_server, HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, 0) def test_9_thread_start_point(self): # Starting server should introduce 4 new threads num_after_init = threading.active_count() self.s.start_server() sleep(5) self.assertEqual(threading.active_count() - num_after_init, 4) def test_10_stop_server(self): # Check no exceptions are raised regardless of connection state try: self.s.start_server() self.s.stop_server() self.s.stop_server() sleep(2) # wait for exceptions result = True except: result = False self.assertTrue(result) def test_11_add_client(self): self.s.add_client(0, None, None) self.assertEqual(len(self.s.clients), 1) self.s.add_client(0, None, None) self.assertEqual(len(self.s.clients), 1) def test_12_reset_timer(self): self.s.TO.start() sleep(1) self.s.reset_timer() after_reset = time() self.assertAlmostEqual(self.s.TO.get_last_reset(), after_reset, places=3)
class ParsingOfIndividualMessages(unittest.TestCase): def setUp(self): self.s = TCP_server(HOST, EVENT_SOURCE_PORT, USER_CLIENT_PORT, BUFFER_SIZE, timeout=TIMEOUT, test_mode=True) def tearDown(self): self.s.stop_server() self.s = None def test_17_good_messages(self): F, U, B, P, S = '988|F|46|67\n', '692|U|46|68\n', '295|B\n', '992|P|46|68\n', '604|S|46\n' # F: add follower and attempt to send message self.s.EP.process_event(F) self.assertEqual(self.s.clients[0].ID, 67) self.assertEqual(self.s.clients[0].followers[0], 46) # U: remove follower self.s.add_client(68, None, None) self.s.EP.process_event(U) self.assertFalse(46 in self.s.clients[1].followers) # B: Broadcast to all. Just make sure no exceptions thrown. self.s.EP.process_event(B) # P: Private message. Just make sure no exceptions thrown. self.s.EP.process_event(P) # S: Status update. Just make sure no exceptions thrown. self.s.clients[0].followers = range(2) self.s.EP.process_event(S) def test_18_bad_messages(self): F, U, B, P, S = '988|F|46\n', '692|U|46\n', '295|B|xxx\n', '992|P|46\n', '604|S\n' # F: to_user_id missing. Check no extra clients are created self.s.EP.process_event(F) self.assertEqual(len(self.s.clients), 0) # U: to_user_id missing. Check no exceptions are thrown self.s.EP.process_event(U) # B: Extra data in message. Check no exceptions are thrown for i in range(2): self.s.add_client(68, None, None) self.s.EP.process_event(B) # P: to_user_id missing. Check no exceptions are thrown self.s.EP.process_event(P) # S: from_user_id missing. Check no exceptions are thrown self.s.EP.process_event(S) def test_19_unknown_message_type(self): bad_msgs = ['0|X|0|0\n', 'xxx|F|yyy|zzz\n'] for msg in bad_msgs: self.s.EP.process_event(msg)