Added test for out of order receive and fix bug creating task
authorCory Johns <johnsca@gmail.com>
Tue, 7 Mar 2017 22:04:37 +0000 (16:04 -0600)
committerCory Johns <johnsca@gmail.com>
Tue, 7 Mar 2017 22:06:57 +0000 (16:06 -0600)
juju/client/connection.py
tests/base.py
tests/unit/__init__.py [new file with mode: 0644]
tests/unit/test_connection.py [new file with mode: 0644]

index 625c609..82ec4fa 100644 (file)
@@ -76,6 +76,7 @@ class Connection:
         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
 
@@ -216,7 +217,6 @@ class Connection:
         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:
index af386ea..292d04a 100644 (file)
@@ -44,3 +44,8 @@ class CleanModel():
         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)
diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/tests/unit/test_connection.py b/tests/unit/test_connection.py
new file mode 100644 (file)
index 0000000..354b363
--- /dev/null
@@ -0,0 +1,45 @@
+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