Several fixes in packet_stream tests

Problem with JSON comparison (order) + awaiting connect()
This commit is contained in:
Pedro Ferreira 2017-08-01 22:32:23 +02:00
parent 36d8a13ff3
commit 8427c188ee

View File

@ -60,25 +60,25 @@ class MockSHSSocket(SHSSocket):
def disconnect(self): def disconnect(self):
self.is_connected = False self.is_connected = False
def _set_connected(self):
self.is_connected = True
for cb in self._on_connect:
ensure_future(cb(), loop=self.loop)
class MockSHSClient(MockSHSSocket): class MockSHSClient(MockSHSSocket):
connect = MockSHSSocket._set_connected async def connect(self):
self.is_connected = True
for cb in self._on_connect:
await cb()
class MockSHSServer(MockSHSSocket): class MockSHSServer(MockSHSSocket):
listen = MockSHSSocket._set_connected def listen(self):
self.is_connected = True
for cb in self._on_connect:
ensure_future(cb(), loop=self.loop)
@pytest.fixture @pytest.fixture
def ps_client(event_loop): def ps_client(event_loop):
client = PSClient('fake.local', 1000, SigningKey.generate(), b'\00' * 32, socket_class=MockSHSClient, client = PSClient('fake.local', 1000, SigningKey.generate(), b'\00' * 32, socket_class=MockSHSClient,
loop=event_loop) loop=event_loop)
client.connect()
return client return client
@ -102,6 +102,7 @@ async def test_on_connect(ps_server):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_message_decoding(ps_client): async def test_message_decoding(ps_client):
await ps_client.connect()
assert ps_client.is_connected assert ps_client.is_connected
ps_client.connection.feed([ ps_client.connection.feed([
@ -129,6 +130,7 @@ async def test_message_decoding(ps_client):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_message_encoding(ps_client): async def test_message_encoding(ps_client):
await ps_client.connect()
assert ps_client.is_connected assert ps_client.is_connected
ps_client.send({ ps_client.send({
@ -142,16 +144,23 @@ async def test_message_encoding(ps_client):
'type': 'source' 'type': 'source'
}, stream=True) }, stream=True)
body = (b'{"name": ["createHistoryStream"], "args": [{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519"' header, body = list(ps_client.connection.get_output())
b', "seq": 1, "live": false, "keys": false}], "type": "source"}')
assert list(ps_client.connection.get_output()) == [b'\x0a\x00\x00\x00\xa6\x00\x00\x00\x01', body] assert header == b'\x0a\x00\x00\x00\xa6\x00\x00\x00\x01'
assert json.loads(body.decode('utf-8')) == {
"name": ["createHistoryStream"],
"args": [
{"id": "@1+Iwm79DKvVBqYKFkhT6fWRbAVvNNVH4F2BSxwhYmx8=.ed25519", "seq": 1, "live": False, "keys": False}
],
"type": "source"
}
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_message_stream(ps_client, mocker): async def test_message_stream(ps_client, mocker):
mocker.patch.object(ps_client, 'register_handler', wraps=ps_client.register_handler) mocker.patch.object(ps_client, 'register_handler', wraps=ps_client.register_handler)
await ps_client.connect()
assert ps_client.is_connected assert ps_client.is_connected
ps_client.send({ ps_client.send({
@ -223,8 +232,9 @@ async def test_message_request(ps_server, mocker):
'args': [] 'args': []
}) })
assert (list(ps_server.connection.get_output()) == header, body = list(ps_server.connection.get_output())
[b'\x02\x00\x00\x00 \x00\x00\x00\x01', b'{"name": ["whoami"], "args": []}']) assert header == b'\x02\x00\x00\x00 \x00\x00\x00\x01'
assert json.loads(body.decode('utf-8')) == {"name": ["whoami"], "args": []}
assert ps_server.req_counter == 2 assert ps_server.req_counter == 2
assert ps_server.register_handler.call_count == 1 assert ps_server.register_handler.call_count == 1