mosquitto/test/broker/03-pattern-matching.py
2019-02-13 12:05:43 +00:00

91 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python
import subprocess
import time
import inspect, os, sys
# From http://stackoverflow.com/questions/279237/python-import-a-module-from-a-folder
cmd_subfolder = os.path.realpath(os.path.abspath(os.path.join(os.path.split(inspect.getfile( inspect.currentframe() ))[0],"..")))
if cmd_subfolder not in sys.path:
sys.path.insert(0, cmd_subfolder)
import mosq_test
def pattern_test(sub_topic, pub_topic):
rc = 1
keepalive = 60
connect_packet = mosq_test.gen_connect("pattern-sub-test", keepalive=keepalive)
connack_packet = mosq_test.gen_connack(rc=0)
publish_packet = mosq_test.gen_publish(pub_topic, qos=0, payload="message")
publish_retained_packet = mosq_test.gen_publish(pub_topic, qos=0, retain=True, payload="message")
mid = 312
subscribe_packet = mosq_test.gen_subscribe(mid, sub_topic, 0)
suback_packet = mosq_test.gen_suback(mid, 0)
mid = 234;
unsubscribe_packet = mosq_test.gen_unsubscribe(mid, sub_topic)
unsuback_packet = mosq_test.gen_unsuback(mid)
port = mosq_test.get_port()
broker = subprocess.Popen(['../../src/mosquitto', '-p', str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
time.sleep(0.5)
sock = mosq_test.do_client_connect(connect_packet, connack_packet, timeout=20, port=port)
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
pub = subprocess.Popen(['./03-pattern-matching-helper.py', pub_topic, str(port)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pub.wait()
(stdo, stde) = pub.communicate()
if mosq_test.expect_packet(sock, "publish", publish_packet):
mosq_test.do_send_receive(sock, unsubscribe_packet, unsuback_packet, "unsuback")
mosq_test.do_send_receive(sock, subscribe_packet, suback_packet, "suback")
if mosq_test.expect_packet(sock, "publish retained", publish_retained_packet):
rc = 0
sock.close()
finally:
broker.terminate()
broker.wait()
(stdo, stde) = broker.communicate()
if rc:
print(stde)
print(stdo)
raise
return rc
pattern_test("#", "test/topic")
pattern_test("#", "/test/topic")
pattern_test("foo/#", "foo/bar/baz")
pattern_test("foo/+/baz", "foo/bar/baz")
pattern_test("foo/+/baz/#", "foo/bar/baz")
pattern_test("foo/+/baz/#", "foo/bar/baz/bar")
pattern_test("foo/foo/baz/#", "foo/foo/baz/bar")
pattern_test("foo/#", "foo")
pattern_test("foo/#", "foo/")
pattern_test("/#", "/foo")
pattern_test("test/topic/", "test/topic/")
pattern_test("test/topic/+", "test/topic/")
pattern_test("+/+/+/+/+/+/+/+/+/+/test", "one/two/three/four/five/six/seven/eight/nine/ten/test")
pattern_test("#", "test////a//topic")
pattern_test("#", "/test////a//topic")
pattern_test("foo/#", "foo//bar///baz")
pattern_test("foo/+/baz", "foo//baz")
pattern_test("foo/+/baz//", "foo//baz//")
pattern_test("foo/+/baz/#", "foo//baz")
pattern_test("foo/+/baz/#", "foo//baz/bar")
pattern_test("foo//baz/#", "foo//baz/bar")
pattern_test("foo/foo/baz/#", "foo/foo/baz/bar")
pattern_test("/#", "////foo///bar")
exit(0)