kw['loop'] = self.loop
self.addr = url
self.ws = await websockets.connect(url, **kw)
+ self.loop.create_task(self.receiver())
log.info("Driver connected to juju %s", url)
return self
client = cls(endpoint, uuid, username, password, cacert, macaroons,
loop)
await client.open()
- client.loop.create_task(client.receiver)
redirect_info = await client.redirect_info()
if not redirect_info:
await self.model.disconnect()
await self.controller.destroy_model(self.model.info.uuid)
await self.controller.disconnect()
+
+
+class AsyncMock(mock.MagicMock):
+ async def __call__(self, *args, **kwargs):
+ return super().__call__(*args, **kwargs)
--- /dev/null
+import json
+import mock
+import pytest
+from collections import deque
+
+from .. import base
+from juju.client.connection import Connection
+
+
+class WebsocketMock:
+ def __init__(self, responses):
+ super().__init__()
+ self.responses = deque(responses)
+ self.open = True
+
+ async def send(self, message):
+ pass
+
+ async def recv(self):
+ return json.dumps(self.responses.popleft())
+
+ async def close(self):
+ self.open = False
+
+
+@pytest.mark.asyncio
+async def test_out_of_order(event_loop):
+ con = Connection(*[None]*4)
+ ws = WebsocketMock([
+ {'request-id': 1},
+ {'request-id': 3},
+ {'request-id': 2},
+ ])
+ expected_responses = [
+ {'request-id': 1},
+ {'request-id': 2},
+ {'request-id': 3},
+ ]
+ con._get_sll = mock.MagicMock()
+ with mock.patch('websockets.connect', base.AsyncMock(return_value=ws)):
+ await con.open()
+ actual_responses = []
+ for i in range(3):
+ actual_responses.append(await con.rpc({'version': 1}))
+ assert actual_responses == expected_responses