Piotr Kliczewski has uploaded a new change for review.
Change subject: tests: JsonRpcClient tests suite
......................................................................
tests: JsonRpcClient tests suite
Signed-off-by: pkliczewski <piotr.kliczewski(a)gmail.com>
Change-Id: I48c209915f4047958fd9b7e5dd6a202ca14a50cb
---
M tests/Makefile.am
A tests/jsonrpcClientTests.py
M tests/stompAdapterTests.py
3 files changed, 159 insertions(+), 0 deletions(-)
git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/82/43582/1
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9a7f81f..a010bb3 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -57,6 +57,7 @@
iproute2Tests.py \
ipwrapperTests.py \
iscsiTests.py \
+ jsonrpcClientTests.py \
jsonrpcServerTests.py \
libvirtconnectionTests.py \
lvmTests.py \
diff --git a/tests/jsonrpcClientTests.py b/tests/jsonrpcClientTests.py
new file mode 100644
index 0000000..5d4304f
--- /dev/null
+++ b/tests/jsonrpcClientTests.py
@@ -0,0 +1,148 @@
+#
+# Copyright 2015 Red Hat, Inc.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+#
+# Refer to the README and COPYING files for full details of the license
+#
+from Queue import Queue
+from threading import Event
+
+from testlib import VdsmTestCase as TestCaseBase
+from yajsonrpc.stompreactor import ClientRpcTransportAdapter
+from yajsonrpc import JsonRpcClient, JsonRpcRequest, JsonRpcResponse
+from yajsonrpc.stomp import Frame, Headers
+from vdsm.compat import json
+
+from stompAdapterTests import TestSubscription
+
+
+ID = 'e8a936a6-d886-4cfa-97b9-2d54209053ff'
+
+
+class TestClient(object):
+
+ def __init__(self):
+ self._queue = Queue()
+ self.closed = False
+
+ def send(self, data, destination, headers):
+ self._queue.put_nowait((data, destination, headers))
+
+ def pop_message(self):
+ return self._queue.get(True, 3)
+
+ @property
+ def has_outgoing_messages(self):
+ return (self._queue.qsize() > 0)
+
+ def close(self):
+ self.closed = True
+
+
+class JsonrpcClientTest(TestCaseBase):
+
+ def test_call(self):
+ subscription = TestSubscription('jms.topic.vdsm_responses',
+ 'ad052acb-a934-4e10-8ec3-00c7417ef8d1')
+ client = TestClient()
+ subscription.set_client(client)
+
+ json_client = JsonRpcClient(
+ ClientRpcTransportAdapter(subscription,
+ 'jms.topic.vdsm_requests',
+ client)
+ )
+
+ request = JsonRpcRequest('my_method', (), ID)
+ json_client.call(request, timeout=0)
+
+ self.assertTrue(client.has_outgoing_messages)
+ data, destination, headers = client.pop_message()
+
+ req = JsonRpcRequest.fromRawObject(json.loads(data)[0])
+ self.assertEquals(req.method, request.method)
+ self.assertEquals(req.id, request.id)
+
+ self.assertEquals('jms.topic.vdsm_requests', destination)
+
+ self.assertEquals(headers[Headers.CONTENT_TYPE], 'application/json')
+ self.assertEquals(headers[Headers.REPLY_TO],
+ 'jms.topic.vdsm_responses')
+
+ def test_async_call_with_response(self):
+ subscription = TestSubscription('jms.topic.vdsm_responses',
+ 'ad052acb-a934-4e10-8ec3-00c7417ef8d1')
+ client = TestClient()
+ subscription.set_client(client)
+
+ json_client = JsonRpcClient(
+ ClientRpcTransportAdapter(subscription,
+ 'jms.topic.vdsm_requests',
+ client)
+ )
+
+ request = JsonRpcRequest('my_method', (), ID)
+ call = json_client.call_async(request)
+
+ self.assertTrue(client.has_outgoing_messages)
+
+ response = JsonRpcResponse(result='my_response', reqId=ID)
+ subscription.process(Frame(body=response.encode()))
+
+ self.assertTrue(call.isSet())
+ self.assertEquals('my_response', call.responses[0].result)
+
+ def test_receive_event(self):
+ subscription = TestSubscription('jms.topic.vdsm_responses',
+ 'ad052acb-a934-4e10-8ec3-00c7417ef8d1')
+ client = TestClient()
+ subscription.set_client(client)
+ ev = Event()
+
+ json_client = JsonRpcClient(
+ ClientRpcTransportAdapter(subscription,
+ 'jms.topic.vdsm_requests',
+ client)
+ )
+
+ def callback(client, event, params):
+ self.assertEquals(event, 'localhost|component|operation|id')
+ self.assertEquals(params['my_property'], 'my_value')
+ ev.set()
+
+ json_client.registerEventCallback(callback)
+
+ event = JsonRpcRequest('localhost|component|operation|id',
+ {'my_property': 'my_value'})
+
+ subscription.process(Frame(body=event.encode()))
+ ev.wait(timeout=3)
+ self.assertTrue(ev.is_set())
+
+ def test_close(self):
+ subscription = TestSubscription('jms.topic.vdsm_responses',
+ 'ad052acb-a934-4e10-8ec3-00c7417ef8d1')
+ client = TestClient()
+
+ json_client = JsonRpcClient(
+ ClientRpcTransportAdapter(subscription,
+ 'jms.topic.vdsm_requests',
+ client)
+ )
+
+ json_client.close()
+ self.assertTrue(client.closed)
+ self.assertTrue(subscription.unsubscribed)
diff --git a/tests/stompAdapterTests.py b/tests/stompAdapterTests.py
index a0be47f..68fae77 100644
--- a/tests/stompAdapterTests.py
+++ b/tests/stompAdapterTests.py
@@ -60,6 +60,7 @@
def __init__(self, destination, id):
self._destination = destination
self._id = id
+ self.unsubscribed = False
def set_client(self, client):
self._client = TestConnection(client)
@@ -76,6 +77,15 @@
def client(self):
return self._client
+ def set_message_handler(self, message_handler):
+ self.handler = message_handler
+
+ def process(self, data):
+ self.handler(None, data)
+
+ def unsubscribe(self):
+ self.unsubscribed = True
+
class ConnectFrameTest(TestCaseBase):
--
To view, visit
https://gerrit.ovirt.org/43582
To unsubscribe, visit
https://gerrit.ovirt.org/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I48c209915f4047958fd9b7e5dd6a202ca14a50cb
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Piotr Kliczewski <piotr.kliczewski(a)gmail.com>