Handle socket failures while testing latency

This commit is contained in:
Matt Martz 2018-03-23 09:37:51 -05:00
parent ced2890261
commit 5d0c62faec
1 changed files with 29 additions and 13 deletions

View File

@ -1517,21 +1517,33 @@ class Speedtest(object):
results = {} results = {}
for server in servers: for server in servers:
cum = [] cum = []
try:
sock = connection_factory( sock = connection_factory(
server['host'], server['host'],
timeout=self._timeout, timeout=self._timeout,
source_address=source_address source_address=source_address
) )
sock.sendall('HI\n'.encode()) sock.sendall('HI\n'.encode())
sock.recv(1024) sock.recv(1024)
except socket.error:
e = get_exception()
printer('ERROR: %r' % e, debug=True)
cum.append(3600 * 3)
continue
for _ in range(0, 3): for _ in range(0, 3):
start = timeit.default_timer() start = timeit.default_timer()
try:
sock.sendall( sock.sendall(
('PING %d\n' % (int(timeit.time.time()) * 1000,)).encode() ('PING %d\n' %
(int(timeit.time.time()) * 1000,)).encode()
) )
resp = sock.recv(1024) resp = sock.recv(1024)
except socket.errror:
e = get_exception()
printer('ERROR: %r' % e, debug=True)
cum.append(3600)
continue
total = (timeit.default_timer() - start) total = (timeit.default_timer() - start)
if resp.startswith('PONG '.encode()): if resp.startswith('PONG '.encode()):
cum.append(total) cum.append(total)
@ -1560,8 +1572,12 @@ class Speedtest(object):
try: try:
fastest = sorted(results.keys())[0] fastest = sorted(results.keys())[0]
except IndexError: except IndexError:
if self._use_socket:
extra = ' Try the HTTP based tests by removing --socket.'
else:
extra = ''
raise SpeedtestBestServerFailure('Unable to connect to servers to ' raise SpeedtestBestServerFailure('Unable to connect to servers to '
'test latency.') 'test latency.%s' % extra)
best = results[fastest] best = results[fastest]
best['latency'] = fastest best['latency'] = fastest