Make test_connection more deterministic.

This commit is contained in:
joo committed 2016-08-22 17:46:53 +01:00
1 parent e3aad3d623
commit 9af39f186a
1 file changed
+21 -22
+21 -22
View File
@@ -15,7 +15,6 @@ import socket
import json import json
import sys import sys
TEST_TIMEOUT_S = None
VERSIONS = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda i: i[1]) VERSIONS = sorted(SUPPORTED_MINECRAFT_VERSIONS.items(), key=lambda i: i[1])
@@ -27,28 +26,28 @@ class _ConnectTest(unittest.TestCase):
addr, port, username='User', initial_version=client_version) addr, port, username='User', initial_version=client_version)
cond = threading.Condition() cond = threading.Condition()
with cond: try:
cond.exc_info = Ellipsis with cond:
server_thread = threading.Thread(
name='test_connection server',
target=self._test_connect_server,
args=(server, cond))
server_thread.start()
server_thread = threading.Thread( client_thread = threading.Thread(
name='test_connection server', name='test_connection client',
target=self._test_connect_server, target=self._test_connect_client,
args=(server, cond)) args=(client, cond))
server_thread.daemon = True client_thread.start()
server_thread.start()
client_thread = threading.Thread( cond.wait()
name='test_connection client', if cond.exc_info is not None:
target=self._test_connect_client, raise_(*cond.exc_info)
args=(client, cond)) finally:
client_thread.daemon = True # Wait for all threads to exit.
client_thread.start() for thread in server_thread, client_thread:
if thread.is_alive():
cond.wait(TEST_TIMEOUT_S) thread.join()
if cond.exc_info is Ellipsis:
self.fail('Timed out.')
elif cond.exc_info is not None:
raise_(*cond.exc_info)
def _test_connect_client(self, client, cond): def _test_connect_client(self, client, cond):
def handle_packet(packet): def handle_packet(packet):
@@ -166,7 +165,7 @@ class FakeServer(threading.Thread):
max_players=255, level_type='default', reduced_debug_info=False) max_players=255, level_type='default', reduced_debug_info=False)
self.write_packet(packet, client_socket) self.write_packet(packet, client_socket)
keep_alive_id = random.randrange(2**31) keep_alive_id = 1076048782
packet = packets.KeepAlivePacketClientbound( packet = packets.KeepAlivePacketClientbound(
self.context, keep_alive_id=keep_alive_id) self.context, keep_alive_id=keep_alive_id)
self.write_packet(packet, client_socket) self.write_packet(packet, client_socket)