subscribe_simple helper test
parent
529cd609ba
commit
b6de597127
@ -0,0 +1,60 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
# Test whether a client sends a correct SUBSCRIBE to a topic with QoS 2.
|
||||||
|
|
||||||
|
# The client should connect to port 1888 with keepalive=60, clean session set,
|
||||||
|
# and client id subscribe-qos2-test
|
||||||
|
# The test will send a CONNACK message to the client with rc=0. Upon receiving
|
||||||
|
# the CONNACK and verifying that rc=0, the client should send a SUBSCRIBE
|
||||||
|
# message to subscribe to topic "qos2/test" with QoS=2. If rc!=0, the client
|
||||||
|
# should exit with an error.
|
||||||
|
# Upon receiving the correct SUBSCRIBE message, the test will reply with a
|
||||||
|
# SUBACK message with the accepted QoS set to 2. On receiving the SUBACK
|
||||||
|
# message, the client should send a DISCONNECT message.
|
||||||
|
|
||||||
|
from mosq_test_helper import *
|
||||||
|
|
||||||
|
port = mosq_test.get_lib_port()
|
||||||
|
|
||||||
|
rc = 1
|
||||||
|
keepalive = 60
|
||||||
|
connect_packet = mosq_test.gen_connect("subscribe-qos2-test", keepalive=keepalive)
|
||||||
|
connack_packet = mosq_test.gen_connack(rc=0)
|
||||||
|
|
||||||
|
disconnect_packet = mosq_test.gen_disconnect()
|
||||||
|
|
||||||
|
mid = 1
|
||||||
|
subscribe_packet = mosq_test.gen_subscribe(mid, "qos2/test", 2)
|
||||||
|
suback_packet = mosq_test.gen_suback(mid, 2)
|
||||||
|
|
||||||
|
publish_packet = mosq_test.gen_publish("qos2/test", 0, "message")
|
||||||
|
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.settimeout(10)
|
||||||
|
sock.bind(('', port))
|
||||||
|
sock.listen(5)
|
||||||
|
|
||||||
|
client_args = sys.argv[1:]
|
||||||
|
client = mosq_test.start_client(filename=sys.argv[1].replace('/', '-'), cmd=client_args, port=port)
|
||||||
|
|
||||||
|
try:
|
||||||
|
(conn, address) = sock.accept()
|
||||||
|
conn.settimeout(10)
|
||||||
|
|
||||||
|
mosq_test.do_receive_send(conn, connect_packet, connack_packet, "connect")
|
||||||
|
mosq_test.do_receive_send(conn, subscribe_packet, suback_packet, "subscribe")
|
||||||
|
conn.send(publish_packet)
|
||||||
|
mosq_test.expect_packet(conn, "disconnect", disconnect_packet)
|
||||||
|
rc = 0
|
||||||
|
|
||||||
|
conn.close()
|
||||||
|
except mosq_test.TestError:
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
if mosq_test.wait_for_subprocess(client):
|
||||||
|
print("test client not finished")
|
||||||
|
rc=1
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
exit(rc)
|
@ -0,0 +1,28 @@
|
|||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <mosquitto.h>
|
||||||
|
|
||||||
|
#define QOS 2
|
||||||
|
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
int port;
|
||||||
|
struct mosquitto_message *messages;
|
||||||
|
|
||||||
|
if(argc < 2){
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
port = atoi(argv[1]);
|
||||||
|
|
||||||
|
mosquitto_lib_init();
|
||||||
|
|
||||||
|
mosquitto_subscribe_simple(&messages, 1,
|
||||||
|
true, "qos2/test", QOS, "localhost", port,
|
||||||
|
"subscribe-qos2-test", 60, true, NULL, NULL, NULL, NULL);
|
||||||
|
|
||||||
|
mosquitto_message_free(&messages);
|
||||||
|
|
||||||
|
mosquitto_lib_cleanup();
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue