Add several tests for the Connection class.

This commit is contained in:
joo committed 2018-05-27 07:40:13 +01:00
1 parent ab9ca6dfee
commit 92f2eff681
3 files changed
+94 -17

No files matched your search

+10 -1
View File
@@ -395,6 +395,10 @@ class _FakeServerTest(unittest.TestCase):
public_key_bytes = None
# The serialised RSA public key used by the server: see 'FakeServer'.
ignore_extra_exceptions = False
# If True, any occurrence of the 'FakeServerTestSuccess' exception is
# considered a success, even if other exceptions are raised.
def _start_client(self, client):
game_joined = [False]
@@ -479,6 +483,8 @@ class _FakeServerTest(unittest.TestCase):
if thread is not None and thread.is_alive():
errors.append({
'msg': 'Thread "%s" timed out.' % thread.name})
if client_exc_info[0] is None:
client_exc_info[0] = client.exc_info
except Exception:
errors.insert(0, {
'msg': 'Exception in main thread',
@@ -492,11 +498,14 @@ class _FakeServerTest(unittest.TestCase):
with lock:
if exc_info is None:
continue
timeout = False
if not issubclass(exc_info[0], FakeServerTestSuccess):
errors.insert(0, {
'msg': 'Exception in %s:' % thread_name,
'exc_info': exc_info})
timeout = False
elif self.ignore_extra_exceptions:
del errors[:]
break
if timeout:
errors.insert(0, {'msg': 'Test timed out.'})
+55
View File
@@ -1,9 +1,14 @@
from minecraft import SUPPORTED_MINECRAFT_VERSIONS
from minecraft.networking.packets import clientbound, serverbound
from minecraft.networking.connection import IgnorePacket
from minecraft.compat import unicode
from . import fake_server
import sys
import re
import io
class ConnectTest(fake_server._FakeServerTest):
def test_connect(self):
@@ -31,6 +36,37 @@ class PingTest(ConnectTest):
client.status(handle_status=False, handle_ping=handle_ping)
class StatusTest(ConnectTest):
def _start_client(self, client):
def handle_status(status_dict):
assert status_dict['description'] == {'text': 'FakeServer'}
raise fake_server.FakeServerTestSuccess
client.status(handle_status=handle_status, handle_ping=False)
class DefaultStatusTest(ConnectTest):
def setUp(self):
class FakeStdOut(io.BytesIO):
def write(self, data):
if isinstance(data, unicode):
data = data.encode('utf8')
super(FakeStdOut, self).write(data)
sys.stdout, self.old_stdout = FakeStdOut(), sys.stdout
def tearDown(self):
sys.stdout, self.old_stdout = self.old_stdout, None
def _start_client(self, client):
def handle_exit():
output = sys.stdout.getvalue()
assert re.match(b'{.*}\\nPing: \\d+ ms\\n$', output), \
'Invalid stdout contents: %r.' % output
raise fake_server.FakeServerTestSuccess
client.handle_exit = handle_exit
client.status(handle_status=None, handle_ping=None)
class ConnectCompressionLowTest(ConnectTest):
compression_threshold = 0
@@ -189,3 +225,22 @@ class IgnorePacketTest(ConnectTest):
'Returned keep-alive IDs %r != %r' % \
(self._keep_alive_ids_returned, [3])
raise fake_server.FakeServerTestSuccess
class HandleExceptionTest(ConnectTest):
ignore_extra_exceptions = True
def _start_client(self, client):
message = 'Min skoldpadda ar inte snabb, men den ar en skoldpadda.'
def handle_login_success(_packet):
raise Exception(message)
client.register_packet_listener(
handle_login_success, clientbound.login.LoginSuccessPacket)
def handle_exception(exc, exc_info):
assert isinstance(exc, Exception) and exc.args == (message,)
raise fake_server.FakeServerTestSuccess
client.handle_exception = handle_exception
client.connect()