Libraries for testing openmodule services
Project description
Openmodule Test SDK
ZMQ Messages
Our zmq stack requires a broker. For testcases a mixin exists, which starts a simple broker on setUp(...) and shuts it
down on tearDown(...). Furthermore the mixin takes care of the slow joiner issue, by testing connectivity with each
topic it has subscribed to.
Context
The ZMQTextMixin has some useful functions:
zmq_context()returns the ZMQ Context of the broker. This context has to be used in order to utilize theinproc://transport.zmq_config()returns a config object withBROKER_SUBandBROKER_PUBset to the correct binds.
Subscribing
The mixin automatically subscribes to all topics listed in the topics list, and all rpc response channels for the
rpc's listed in rpc_channels.
class MyTestCase(ZMQTestMixin, TestCase):
topics = ["health"]
rpc_channels = ["controller"]
For dynamically subscribing you must use the provided subscribe function. It again takes care of the slow joiner issue by sending to the requested topic and waiting for a response.
class MyTestCase(ZMQTestMixin, TestCase):
def test_something(self):
...
self.zmq_client.subscribe("my-topic", "some-more-topics")
...
Sending
In order to send, you have to specify the topic in the topics list of the testcase. You can then send by passing a
dict to the send function, or by passing keyword arguments.
class MyTestCase(ZMQTestMixin, TestCase):
topics = ["test"]
def test_something(self):
self.zmq_client.send("test", type="test", some="data", even_more="data")
You can also pass a model instance directly and send it:
class MyModel(OpenModuleModel):
data: str
zmq_client.send("test", MyModel(some="data"))
Receiving
Next message on a specific topic:
topic, message = self.zmq_client.wait_for_message_on_topic(b"test")
Next message which matches a filter:
rpc_id = ...
topic, message = self.zmq_client.wait_for_message(
filter=lambda topic, message: message.get("rpc_id") == rpc_id
)
RPC Servers
When testing RPC servers we again have to combat the slow joiner issue. In order to avoid it, a utility function exists
which waits until a passed RPC server has booted up compeltely. The method wait_for_rpc_server(...) registers a dummy
handler in each of the rpc server's channels, and calls it repeatetly until it receives an answer.
class MyTestCase(RPCServerTestMixin, TestCase):
rpc_channels = ["controller"]
def test_my_server(self):
my_server = my_code.get_server()
self.wait_for_rpc_server(my_server)
Channels and dynamic registering of handlers
All RPC channels have to be listed in the ZMQTestMixin.rpc_channels of the testcase.
When registering RPC channels on the go, the function needs to be called again, to ensure that the rpc server is properly listening on the new channels.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file openmodule-test-0.1.7.tar.gz.
File metadata
- Download URL: openmodule-test-0.1.7.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
09a53bf0c54c4726727c1ea140b40f89dbd0c27ee5189f111800bb2c4d7ef2aa
|
|
| MD5 |
a24c9ae73b5bd983a391a1bc1d757684
|
|
| BLAKE2b-256 |
0e385b2e9426381e5faeabb0c8068b3d2fd0b3797c59ddb9d63e977c0feb9e08
|
File details
Details for the file openmodule_test-0.1.7-py2.py3-none-any.whl.
File metadata
- Download URL: openmodule_test-0.1.7-py2.py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac56b4cd7b62384b62850730dfdfb6d1733b088d0cd1b144db9b4c10715d7565
|
|
| MD5 |
e064ec01a122e287846a8e04ea26f4d1
|
|
| BLAKE2b-256 |
c2ee662aa9a63f0a700edc01e9bffb777723935fc1d84f81ac69bee3cb4d2f4f
|