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_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_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_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_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)