Skip to content

Commit

Permalink
Merge pull request #2441 from cesanta/mqttst
Browse files Browse the repository at this point in the history
improve MQTT test speed
  • Loading branch information
cpq authored Oct 30, 2023
2 parents ef16a73 + f0a9278 commit d6d2d75
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions test/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,17 +521,16 @@ static void check_mqtt_message(struct mg_mqtt_opts *opts,
}
}

static void test_mqtt_ver(uint8_t mqtt_version) {
char tbuf[16], mbuf[50] = {0}, client_id[16], topic[16];
static void test_mqtt_basic(void) {
char tbuf[16], mbuf[50] = {0}, topic[16];
struct mqtt_data test_data = {tbuf, mbuf, 16, 50, 0};
struct mg_mgr mgr;
struct mg_connection *c;
struct mg_mqtt_opts opts;
struct mg_mqtt_prop properties[5];
const char *url = "mqtt://broker.hivemq.com:1883";
int i, retries;

// Connect with empty client ID, no options, ergo no MQTT != 3.1.1
// Connect with empty client ID, no options, ergo MQTT = 3.1.1
mg_mgr_init(&mgr);
c = mg_mqtt_connect(&mgr, url, NULL, mqtt_cb, &test_data);
for (i = 0; i < 300 && mbuf[0] == 0; i++) mg_mgr_poll(&mgr, 10);
Expand Down Expand Up @@ -571,13 +570,24 @@ static void test_mqtt_ver(uint8_t mqtt_version) {
memset(mbuf + 1, 0, sizeof(mbuf) - 1);
test_data.flags = 0;

// Disconnect !
// Clean Disconnect !
mg_mqtt_disconnect(c, NULL);
for (i = 0; i < 10 && mbuf[0] != 0; i++) mg_mgr_poll(&mgr, 10);
mg_mgr_free(&mgr);
ASSERT(mgr.conns == NULL);
}

static void test_mqtt_ver(uint8_t mqtt_version) {
char tbuf[16], mbuf[50] = {0}, client_id[16], topic[16];
struct mqtt_data test_data = {tbuf, mbuf, 16, 50, 0};
struct mg_mgr mgr;
struct mg_connection *c;
struct mg_mqtt_opts opts;
struct mg_mqtt_prop properties[5];
const char *url = "mqtt://broker.hivemq.com:1883";
int i, retries;

// (Re-)connect with options: version, clean session, last will, keepalive
// Connect with options: version, clean session, last will, keepalive
// time. Don't set retain, some runners are not random
test_data.flags = 0;
memset(mbuf, 0, sizeof(mbuf));
Expand Down Expand Up @@ -659,6 +669,7 @@ static void test_mqtt_ver(uint8_t mqtt_version) {

static void test_mqtt(void) {
test_mqtt_base();
test_mqtt_basic();
test_mqtt_ver(4);
test_mqtt_ver(5);
}
Expand Down

0 comments on commit d6d2d75

Please sign in to comment.