Difference between revisions of "Vertx-eventbus-python"
(→Java) |
|||
(32 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | __NOTOC__ | ||
+ | https://vertx.io/assets/logo-sm.png | ||
+ | |||
+ | {{:Vertx-eventbus-python/Links}} | ||
+ | |||
=OsProject= | =OsProject= | ||
Line 10: | Line 15: | ||
|storemode=property | |storemode=property | ||
}} | }} | ||
− | {{: | + | = Content = |
+ | __TOC__ | ||
+ | |||
+ | = Usage = | ||
+ | * [https://vertx-eventbus-python.readthedocs.io/en/latest/index.html Python API documentation] | ||
+ | * [https://vertx.io/docs/vertx-tcp-eventbus-bridge/java/ vert.x tcp-eventbus bridge specification] | ||
+ | |||
+ | |||
+ | The message type can be the following for messages sent by the TCP client: | ||
+ | # send to send a message to an address, | ||
+ | # publish to publish a message to an address, | ||
+ | # register to subscribe to the messages sent or published to an address, | ||
+ | # unregister to unsubscribe to the messages sent or published to an address, | ||
+ | # ping to send a ping request to the bridge. | ||
+ | = Example = | ||
+ | For the Unit tests and as an Example Vertx-eventbus-python ships with some Java code that provides an environment to try out the python API. | ||
+ | == Usage == | ||
+ | <source lang='bash'> | ||
+ | java/run -h | ||
+ | -h (--help) : show this usage (default: true) | ||
+ | -i (--inbound) VAL : inbound permission regex to be used (default: echo.*) | ||
+ | -o (--outbound) VAL : outbound permission regex to be used (default: echo.*) | ||
+ | -p (--port) N : port to listen for eventbus messages (default: 7001) | ||
+ | </source> | ||
+ | == EchoVerticle == | ||
+ | To test things out a Java "EchoVerticle" is deployed. This verticle will accept messages in Json-Format at the address "echo". By default it will accept the message and reply to it. | ||
+ | If the JsonObject represent an EchoCommand it will react as instructed by the command. | ||
+ | |||
+ | Below you'll find an explanation graphiz graph. The nodes are clickable and link to the source code. | ||
+ | The black lines show eventbus messages. The blue lines show the bridged messages e.g. a reply message or "..." for any other message type. | ||
+ | <graphviz> | ||
+ | digraph EchoServer { | ||
+ | PC [ label="Python Client" URL="https://github.com/rc-dukes/vertx-eventbus-python/blob/master/python/tests/test_eventbus.py" ] | ||
+ | BR [ label="Java vert.x tcp Eventbus Bridge" URL="https://github.com/rc-dukes/vertx-eventbus-python/blob/master/java/src/main/java/io/vertx/example/util/TcpEventBusBridgeStarter.java" ] | ||
+ | EV [ label="Java EchoVerticle" URL="https://github.com/rc-dukes/vertx-eventbus-python/blob/master/java/src/main/java/io/vertx/example/echo/EchoVerticle.java" ] | ||
+ | PC -> BR [ label="register" ] | ||
+ | PC -> BR [ label="unregister" ] | ||
+ | PC -> BR [ label="ping" ] | ||
+ | BR -> PC [ label="pong" ] | ||
+ | PC -> BR [ label="send" ] | ||
+ | PC -> BR [ label="publish" ] | ||
+ | BR -> EV [ color="blue" label="..." ] | ||
+ | EV -> BR [ color="blue" label="reply" ] | ||
+ | EV -> BR [ label="send" ] | ||
+ | EV -> BR [ label="publish" ] | ||
+ | BR -> PC [ color="blue" label="..." ] | ||
+ | } | ||
+ | </graphviz> | ||
+ | |||
+ | == EchoCommand time == | ||
+ | === Java === | ||
+ | This is a Junit test that tries the EchoVerticle with the command "time" | ||
+ | <source lang='java'> | ||
+ | @Test | ||
+ | public void testSendCmd() throws InterruptedException { | ||
+ | EchoCommand cmd=new EchoCommand("time","send","me"); | ||
+ | |||
+ | Object[] ro= {null}; | ||
+ | // listen to any message for the address me | ||
+ | eb.consumer("me", rmsg->{ | ||
+ | ro[0]=rmsg.body(); | ||
+ | }); | ||
+ | eb.send("echo", cmd.asJsonObject()); | ||
+ | Thread.sleep(500); | ||
+ | Object o=ro[0]; | ||
+ | assertNotNull(o); | ||
+ | assertTrue(o instanceof JsonObject); | ||
+ | JsonObject jo=(JsonObject)o; | ||
+ | assertTrue(jo.containsKey("received_nanotime")); | ||
+ | assertTrue(jo.containsKey("iso_time")); | ||
+ | } | ||
+ | </source> | ||
+ | Expected result like: | ||
+ | <pre> | ||
+ | EchoVerticle deployed after 4,2 secs | ||
+ | Echo Verticle received: | ||
+ | {"cmd":"time","msgType":"send","address":"me"} | ||
+ | headers: | ||
+ | |||
+ | will reply it back now with received_time timestamp 73828431136823... | ||
+ | {"cmd":"time","msgType":"send","address":"me","received_nanotime":73828431136823,"iso_time":"2020-02-02 15:36:00.345"} | ||
+ | </pre> | ||
+ | == Python == | ||
+ | This is Python-unit test that tries the EchoVerticle with the command "time" | ||
+ | <source lang='python'> | ||
+ | def test_send(self): | ||
+ | """ test sending a message""" | ||
+ | eb = Eventbus(port=7001,debug=self.debug) | ||
+ | handler=Handler(self.debug) | ||
+ | address="echoMe" | ||
+ | eb.registerHandler(address, handler.handle) | ||
+ | cmd=EchoCommand("time","send",address) | ||
+ | eb.wait(State.OPEN) | ||
+ | eb.send('echo',cmd) | ||
+ | # wait for the message to arrive | ||
+ | time.sleep(RECEIVE_WAIT) | ||
+ | eb.close() | ||
+ | assert 'received_nanotime' in handler.result | ||
+ | assert 'iso_time' in handler.result | ||
+ | </source> | ||
+ | Expected result (with debug info) like: | ||
+ | <pre> | ||
+ | starting receiving thread | ||
+ | sending 70 bytes '{"type": "register", "address": "echoMe", "headers": {}, "body": null}' | ||
+ | |||
+ | trying to receive a message in state OPEN | ||
+ | wait for OPEN successful after 0.000 secs | ||
+ | sending 115 bytes '{"type": "send", "address": "echo", "headers": {}, "body": {"cmd": "time", "msgType": "send", "address": "echoMe"}}' | ||
+ | trying to receive 192 bytes in state OPEN | ||
+ | 192 message bytes with payload {'type': 'message', 'address': 'echoMe', 'headers': {}, 'body': {'cmd': 'time', 'msgType': 'send', 'address': 'echoMe', 'received_nanotime': 78069691493750, 'iso_time': '2020-02-02 16:46:41.462'}, 'send': True} | ||
+ | handler received {'cmd': 'time', 'msgType': 'send', 'address': 'echoMe', 'received_nanotime': 78069691493750, 'iso_time': '2020-02-02 16:46:41.462'} | ||
+ | trying to receive a message in state OPEN | ||
+ | timed out | ||
+ | trying to receive a message in state OPEN | ||
+ | timed out | ||
+ | trying to receive a message in state OPEN | ||
+ | timed out | ||
+ | receiving thread finished in state CLOSING | ||
+ | wait for CLOSED successful after 0.020 secs | ||
+ | ---------------------------------------------------------------------- | ||
+ | Ran 1 test in 0.325s | ||
+ | </pre> | ||
+ | |||
= Installation = | = Installation = | ||
+ | == From PyPi == | ||
+ | * https://test.pypi.org/project/vertx-eventbus-python/3.8.5a7/ | ||
+ | |||
+ | == From Git == | ||
<source lang='bash' highlight='1-2'> | <source lang='bash' highlight='1-2'> | ||
git clone https://github.com/rc-dukes/vertx-eventbus-python | git clone https://github.com/rc-dukes/vertx-eventbus-python | ||
Line 54: | Line 185: | ||
./test --python | ./test --python | ||
... | ... | ||
− | Ran | + | Ran 14 tests in 1.663s |
OK | OK | ||
python unit tests successful! | python unit tests successful! | ||
</source> | </source> | ||
+ | |||
== Integration == | == Integration == | ||
The integration test will do the testing steps | The integration test will do the testing steps | ||
Line 124: | Line 256: | ||
=== Travis === | === Travis === | ||
The travis environment used is an Ubuntu Xenial distribution. | The travis environment used is an Ubuntu Xenial distribution. | ||
+ | = History = | ||
+ | * [https://github.com/jaymine/TCP-eventbus-client-Python 2016: initial project by Jayamine Alupotha] | ||
+ | ** [https://pypi.org/project/vertx-eventbus/ pypi] | ||
+ | * [https://github.com/vert-x3/vertx-eventbus-bridge-clients/tree/master/python 2017: subproject of vertx-eventbus-bridge-clients] |
Latest revision as of 09:23, 5 February 2020
Click here to comment see Vertx-eventbus-python
OsProject
OsProject | |
---|---|
edit | |
id | vertx-eventbus-python |
state | |
owner | rc-dukes |
title | Python vertx eventbus bridge |
url | https://github.com/rc-dukes/vertx-eventbus-python |
version | 3.8.5 |
description | |
date | 2020/01/30 |
since | |
until |
Content
Usage
The message type can be the following for messages sent by the TCP client:
- send to send a message to an address,
- publish to publish a message to an address,
- register to subscribe to the messages sent or published to an address,
- unregister to unsubscribe to the messages sent or published to an address,
- ping to send a ping request to the bridge.
Example
For the Unit tests and as an Example Vertx-eventbus-python ships with some Java code that provides an environment to try out the python API.
Usage
java/run -h
-h (--help) : show this usage (default: true)
-i (--inbound) VAL : inbound permission regex to be used (default: echo.*)
-o (--outbound) VAL : outbound permission regex to be used (default: echo.*)
-p (--port) N : port to listen for eventbus messages (default: 7001)
EchoVerticle
To test things out a Java "EchoVerticle" is deployed. This verticle will accept messages in Json-Format at the address "echo". By default it will accept the message and reply to it. If the JsonObject represent an EchoCommand it will react as instructed by the command.
Below you'll find an explanation graphiz graph. The nodes are clickable and link to the source code. The black lines show eventbus messages. The blue lines show the bridged messages e.g. a reply message or "..." for any other message type.
EchoCommand time
Java
This is a Junit test that tries the EchoVerticle with the command "time"
@Test
public void testSendCmd() throws InterruptedException {
EchoCommand cmd=new EchoCommand("time","send","me");
Object[] ro= {null};
// listen to any message for the address me
eb.consumer("me", rmsg->{
ro[0]=rmsg.body();
});
eb.send("echo", cmd.asJsonObject());
Thread.sleep(500);
Object o=ro[0];
assertNotNull(o);
assertTrue(o instanceof JsonObject);
JsonObject jo=(JsonObject)o;
assertTrue(jo.containsKey("received_nanotime"));
assertTrue(jo.containsKey("iso_time"));
}
Expected result like:
EchoVerticle deployed after 4,2 secs Echo Verticle received: {"cmd":"time","msgType":"send","address":"me"} headers: will reply it back now with received_time timestamp 73828431136823... {"cmd":"time","msgType":"send","address":"me","received_nanotime":73828431136823,"iso_time":"2020-02-02 15:36:00.345"}
Python
This is Python-unit test that tries the EchoVerticle with the command "time"
def test_send(self):
""" test sending a message"""
eb = Eventbus(port=7001,debug=self.debug)
handler=Handler(self.debug)
address="echoMe"
eb.registerHandler(address, handler.handle)
cmd=EchoCommand("time","send",address)
eb.wait(State.OPEN)
eb.send('echo',cmd)
# wait for the message to arrive
time.sleep(RECEIVE_WAIT)
eb.close()
assert 'received_nanotime' in handler.result
assert 'iso_time' in handler.result
Expected result (with debug info) like:
starting receiving thread sending 70 bytes '{"type": "register", "address": "echoMe", "headers": {}, "body": null}' trying to receive a message in state OPEN wait for OPEN successful after 0.000 secs sending 115 bytes '{"type": "send", "address": "echo", "headers": {}, "body": {"cmd": "time", "msgType": "send", "address": "echoMe"}}' trying to receive 192 bytes in state OPEN 192 message bytes with payload {'type': 'message', 'address': 'echoMe', 'headers': {}, 'body': {'cmd': 'time', 'msgType': 'send', 'address': 'echoMe', 'received_nanotime': 78069691493750, 'iso_time': '2020-02-02 16:46:41.462'}, 'send': True} handler received {'cmd': 'time', 'msgType': 'send', 'address': 'echoMe', 'received_nanotime': 78069691493750, 'iso_time': '2020-02-02 16:46:41.462'} trying to receive a message in state OPEN timed out trying to receive a message in state OPEN timed out trying to receive a message in state OPEN timed out receiving thread finished in state CLOSING wait for CLOSED successful after 0.020 secs ---------------------------------------------------------------------- Ran 1 test in 0.325s
Installation
From PyPi
From Git
git clone https://github.com/rc-dukes/vertx-eventbus-python
cd vertx-eventbus-python
Java
cd java
mvn clean install -D gpg.skip
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.125 s
...
Python
cd python
sudo -H pip install .
Test
Java
./test --java
...
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
Python
For the python tests to be successful you might want to start the echoVerticle manually first with
java/run
... Initializing cluster partition table arrangement...
and then start the python unit tests in a separate terminal window with
./test --python
...
Ran 14 tests in 1.663s
OK
python unit tests successful!
Integration
The integration test will do the testing steps
- java tests
- start echoVerticle
- python tests
automatically in sequence
./test --all
Help / Usage
./test -h
usage:
./test [a|--all|-h|--help|-j|--java|-p|--python]
-h|--help: show this help
-a|--all: run all tests
-j|--java: run java tests
-p|--python: run python tests
-pa: run python tests with automatic start of echoServer
TestEnviroment
The tests above have been run succesfully in the following environments:
MacOS
mvn -version
Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)
Maven home: /opt/local/share/java/maven3
Java version: 1.8.0_152, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home/jre
Default locale: de_DE, platform encoding: UTF-8
OS name: "mac os x", version: "10.13.6", arch: "x86_64", family: "mac"
python3 --version
Python 3.7.6
Raspbian
mvn -version
Apache Maven 3.6.0
Maven home: /usr/share/maven
Java version: 1.8.0_212, vendor: Oracle Corporation, runtime: /usr/lib/jvm/java-8-openjdk-armhf/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "4.19.75-v7l+", arch: "arm", family: "unix"
python3 --version
Python 3.7.3
Ubuntu
mvn -version
Apache Maven 3.3.3 (7994120775791599e205a5524ec3e0dfe41d4a06; 2015-04-22T13:57:37+02:00)
Maven home: /usr/local/src/apache-maven-3.3.3
Java version: 1.8.0_242, vendor: Private Build
Java home: /usr/lib/jvm/java-8-openjdk-amd64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "5.0.0-37-generic", arch: "amd64", family: "unix"
python3 --version
Python 3.7.5
Travis
The travis environment used is an Ubuntu Xenial distribution.