def test_update_to_string_without_time(self): statistic = TcpStatistic(self.host, self.port, list()) self.assertEqual((self.host, self.port), statistic.statistic_by()) self.assertEqual( statistic.update_to_string(PingRequest(self.host, self.port, -1)), f'Probing {self.host}:{self.port}' f'/tcping - No response')
def test_update_to_string(self): statistic = TcpStatistic(self.host, self.port, list()) self.assertEqual((self.host, self.port), statistic.statistic_by()) self.assertEqual( statistic.update_to_string( PingRequest(self.host, self.port, self.time)), f'Probing {self.host}:{self.port}/tcping - ' f'Port is open - time={self.time*1000}ms')
def test_update_to_string_with_jitter(self): statistic = TcpStatistic(self.host, self.port, list()) self.assertEqual((self.host, self.port), statistic.statistic_by()) self.assertEqual( statistic.update_to_string( PingRequest(self.host, self.port, self.time), self.jitter), f'Probing {self.host}:{self.port}' f'/tcping - Port is open - ' f'time={self.time*1000}ms ' f'jitter={self.jitter}ms')
def test_get_empty_statistic(self): statistic = TcpStatistic(self.host, self.port, [ PingRequest(self.host, self.port, -1), PingRequest(self.host, self.port, -1) ]) self.assertEqual( statistic.get_statistic(), f'Probing {self.host}:{self.port}/tcping - ' f'No response\n' 'Probing localhost:5001/tcping - No response\n' f'\nPing statistics for {self.host}:{self.port}\n' '\t2 probes sent.\n' '\t0 successful, 2 failed.\n' 'Was unable to connect, cannot provide trip statistics.')
def test_get_statistic(self): statistic = TcpStatistic(self.host, self.port, [ PingRequest(self.host, self.port, 0.001), PingRequest(self.host, self.port, 0.002) ]) self.assertEqual( statistic.get_statistic(), f'Probing {self.host}:{self.port}/tcping - ' f'Port is open - time=1.0ms\n' 'Probing localhost:5001/tcping - ' 'Port is open - time=2.0ms\n' f'\nPing statistics for {self.host}:{self.port}\n' '\t2 probes sent.\n' '\t2 successful, 0 failed.\n' 'Approximate trip times in milli-seconds:\n' '\tMinimum = 1.0ms, Maximum = 2.0ms, ' 'Average = 1.500ms')
def main(args): try: args.host = socket.gethostbyname(args.host) except socket.gaierror: raise TCPConnectionError(args.host, args.port) tcping = TCPing(host=args.host, port=args.port, pause=args.sleep_time, wait=args.wait, ipv6=args.ipv6) args.count = args.count if not args.cycle else None tcp_result = tcping.ping_with_count(args.count) statistic = TcpStatistic(host=args.host, port=args.port, pings=tcp_result, jitter=args.jitter, date=args.date) stat_result = statistic.get_statistic() if args.path: with open(args.path, 'a') as file: print(stat_result, file=file) else: print(stat_result)
def __init__(self, host: str, port: int): self.host = socket.gethostbyname(host) self.statistic = TcpStatistic(self.host, port) self.port = port
def test_exception(self): statistic = TcpStatistic(self.host, self.port, [1, 2, 3]) with self.assertRaises(TypeError): statistic.get_statistic()
def test_update_time(self): self.assertEqual(TcpStatistic()._update_time(0.001), 1.0) self.assertEqual(TcpStatistic()._update_time(0.00132), 1.32) self.assertEqual(TcpStatistic()._update_time(0.0132), 13.2) self.assertEqual(TcpStatistic()._update_time(13.2, kf=1), 13.2)