def test_tot_ierrors(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.get_data = mock.MagicMock(return_value='3,4,5,6') expected = 3, 3 result = prox.tot_ierrors() self.assertEqual(result, expected)
def test_get_data(self, mock_select, mock_time): mock_select.select.side_effect = [[1], [0]] mock_socket = mock.MagicMock() mock_recv = mock_socket.recv() mock_recv.decode.return_value = "" prox = ProxSocketHelper(mock_socket) ret = prox.get_data() self.assertEqual(ret, "") self.assertEqual(len(prox._pkt_dumps), 0) mock_select.select.reset_mock() mock_select.select.side_effect = chain([['a'], ['']], repeat([1], 3)) mock_recv.decode.return_value = PACKET_DUMP_1 ret = prox.get_data() self.assertEqual(mock_select.select.call_count, 2) self.assertEqual(ret, 'pktdump,3,11') self.assertEqual(len(prox._pkt_dumps), 1) mock_select.select.reset_mock() mock_select.select.side_effect = chain([[object()], [None]], repeat([1], 3)) mock_recv.decode.return_value = PACKET_DUMP_2 ret = prox.get_data() self.assertEqual(mock_select.select.call_count, 2) self.assertEqual(ret, 'jumped over') self.assertEqual(len(prox._pkt_dumps), 3)
def test_lat_stats(self, mock_time): latency_output = [ '1, 2 , 3', # has white space '4,5', # too short '7,8,9,10.5,11', # too long with float, but float is in unused portion 'twelve,13,14', # value as English word '15,16.2,17', # float in used portion ] mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.get_data = mock.MagicMock(side_effect=latency_output) expected = ( { 3: 1, 5: 7, }, { 3: 2, 5: 8, }, { 3: 3, 5: 9, }, ) result = prox.lat_stats([3, 4, 5, 6, 7], 16) self.assertEqual(mock_socket.sendall.call_count, 5) self.assertEqual(result, expected)
def test__parse_socket_data_mixed_data(self, mock_time): prox = ProxSocketHelper(mock.MagicMock()) ret = prox._parse_socket_data(PACKET_DUMP_NON_1, False) self.assertEqual(ret, 'not_a_dump,1,2') self.assertEqual(len(prox._pkt_dumps), 0) ret = prox._parse_socket_data(PACKET_DUMP_MIXED_1, False) self.assertEqual(ret, 'not_a_dump,1,2') self.assertEqual(len(prox._pkt_dumps), 1)
def test__parse_socket_data_pkt_dump_only(self, mock_time): prox = ProxSocketHelper(mock.MagicMock()) ret = prox._parse_socket_data('', True) self.assertFalse(ret) ret = prox._parse_socket_data(PACKET_DUMP_1, True) self.assertTrue(ret) ret = prox._parse_socket_data(PACKET_DUMP_2, True) self.assertTrue(ret)
def test_port_stats(self, mock_time): port_stats = [ ','.join(str(n) for n in range(3, 15)), ','.join(str(n) for n in range(8, 32, 2)), ','.join(str(n) for n in range(5, 89, 7)), ] mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.get_data = mock.MagicMock(side_effect=port_stats) expected = [16, 26, 36, 46, 56, 66, 76, 86, 96, 106, 116, 126] result = prox.port_stats([3, 4, 5]) self.assertEqual(result, expected)
def test_core_stats(self, mock_time): core_stats = [ '3,4,5,6', '7,8,9,10,NaN', '11,12,13,14,15', ] mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.get_data = mock.MagicMock(side_effect=core_stats) expected = 21, 24, 27, 14 result = prox.core_stats([3, 4, 5], 16) self.assertEqual(result, expected)
def test__parse_socket_data_bad_data(self, mock_time): prox = ProxSocketHelper(mock.MagicMock()) with self.assertRaises(ValueError): prox._parse_socket_data(PACKET_DUMP_BAD_1, False) with self.assertRaises(ValueError): prox._parse_socket_data(PACKET_DUMP_BAD_2, False) ret = prox._parse_socket_data(PACKET_DUMP_BAD_3, False) self.assertEqual(ret, 'pktdump,3')
def test_measure_tot_stats(self, mock_time): start_tot = 3, 4, 5, 6 end_tot = 7, 9, 11, 13 delta_tot = 4, 5, 6, 7 get_data_output = [ ','.join(str(n) for n in start_tot), ','.join(str(n) for n in end_tot), ] mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.get_data = mock.MagicMock(side_effect=get_data_output) expected = { 'start_tot': start_tot, 'end_tot': end_tot, 'delta': delta_tot, } with prox.measure_tot_stats() as result: pass self.assertEqual(result, expected)
def test_get_packet_dump(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox._pkt_dumps = [] self.assertIsNone(prox.get_packet_dump()) prox._pkt_dumps = [234] self.assertEqual(prox.get_packet_dump(), 234) self.assertEqual(prox._pkt_dumps, [])
def test_slope_speed(self, mock_time): core_data = [ { 'cores': [3, 4, 5], 'speed': 1000, }, { 'cores': [9, 10, 11], 'speed': '500.5', }, ] mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.set_speed = set_speed = mock.MagicMock() prox.slope_speed(core_data, 5) self.assertEqual(set_speed.call_count, 20) set_speed.reset_mock() prox.slope_speed(core_data, 5, 5) self.assertEqual(set_speed.call_count, 10)
def test_get_sock(self, mock_time): mock_sock = mock.MagicMock() prox = ProxSocketHelper(mock_sock) result = prox.get_socket() self.assertIs(result, mock_sock)
def test_set_pps(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.set_pps([3, 4, 5], 1000, 512) self.assertEqual(mock_socket.sendall.call_count, 3)
def test_reset_values(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.reset_values([3, 4, 5]) self.assertEqual(mock_socket.sendall.call_count, 3)
def test_connect(self, mock_time): mock_sock = mock.MagicMock() prox = ProxSocketHelper(mock_sock) prox.connect('10.20.30.40', 23456) self.assertEqual(mock_sock.connect.call_count, 1)
def test_dump_rx(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.dump_rx(3, 5, 8) self.assertEqual(mock_socket.sendall.call_count, 1)
def test_put_command(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.put_command("data") mock_socket.sendall.assert_called_once()
def test_start(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.start([3, 4, 5]) mock_socket.sendall.assert_called()
def test_reset_stats(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.reset_stats() mock_socket.sendall.assert_called()
def test_set_count(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.set_count(432, [3, 4, 5]) self.assertEqual(mock_socket.sendall.call_count, 3)
def test_set_value(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.set_value([3, 4, 5], 10, 20, 30) self.assertEqual(mock_socket.sendall.call_count, 3)
def test_force_quit(self, mock_time): mock_socket = mock.MagicMock() prox = ProxSocketHelper(mock_socket) prox.force_quit() mock_socket.sendall.assert_called()
def test___init__(self, mock_socket, mock_time): expected = mock_socket.socket() prox = ProxSocketHelper() result = prox._sock self.assertEqual(result, expected)