Commit 77d7dae4 authored by Ian Craggs's avatar Ian Craggs

Improve tests - will messages and ha connect options

parent 3ed9a7b3
...@@ -994,23 +994,18 @@ typedef struct ...@@ -994,23 +994,18 @@ typedef struct
int test6(struct Options options) int test6(struct Options options)
{ {
char* testname = "test6"; char* testname = "test6";
/* TODO - unused -remove? char summaryname[50]; */
/* TODO - unused -remove? FILE *outfile = NULL; */
MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer; MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer; MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
/* TODO - unused -remove? MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer; */ MQTTClient_connectOptions opts2 = MQTTClient_connectOptions_initializer;
int rc; int rc, count;
/* TODO - unused -remove? MQTTClient_message pubmsg = MQTTClient_message_initializer; */ char* mqttsas_topic = "MQTTSAS topic";
/* TODO - unused -remove? MQTTClient_message* m = NULL; */
/* TODO - unused -remove? int count = 0; */
/* TODO - unused -remove? char* mqttsas_topic = "MQTTSAS topic"; */
failures = 0; failures = 0;
MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages"); MyLog(LOGA_INFO, "Starting test 6 - connectionLost and will messages");
fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\""); fprintf(xml, "<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
global_start_time = start_clock(); global_start_time = start_clock();
opts.keepAliveInterval = 10; opts.keepAliveInterval = 2;
opts.cleansession = 1; opts.cleansession = 1;
opts.will = &wopts; opts.will = &wopts;
opts.will->message = test6_will_message; opts.will->message = test6_will_message;
...@@ -1024,7 +1019,6 @@ int test6(struct Options options) ...@@ -1024,7 +1019,6 @@ int test6(struct Options options)
} }
/* Client-1 with Will options */ /* Client-1 with Will options */
/* connect to 1884 which is the protocol tracer and which allows the connection to be broken on command */
rc = MQTTClient_create(&test6_c1, options.connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); rc = MQTTClient_create(&test6_c1, options.connection, "Client_1", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc); assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
if (rc != MQTTCLIENT_SUCCESS) if (rc != MQTTCLIENT_SUCCESS)
...@@ -1041,12 +1035,8 @@ int test6(struct Options options) ...@@ -1041,12 +1035,8 @@ int test6(struct Options options)
if (rc != MQTTCLIENT_SUCCESS) if (rc != MQTTCLIENT_SUCCESS)
goto exit; goto exit;
#if 1
rc = MQTTClient_disconnect(test6_c1, 100L);
assert("Good rc from disconnect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
#else
/* Client - 2 (multi-threaded) */ /* Client - 2 (multi-threaded) */
rc = MQTTClient_create(&test6_c2, "127.0.0.1:1883", "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); rc = MQTTClient_create(&test6_c2, options.connection, "Client_2", MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc); assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
/* Set the callback functions for the client */ /* Set the callback functions for the client */
...@@ -1064,9 +1054,9 @@ int test6(struct Options options) ...@@ -1064,9 +1054,9 @@ int test6(struct Options options)
assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc); assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
/* now send the command which will break the connection and cause the will message to be sent */ /* now send the command which will break the connection and cause the will message to be sent */
rc = MQTTClient_publish(test6_c1, mqttsas_topic, strlen("TERMINATE"), "TERMINATE", 0, 0, NULL); /*rc = MQTTClient_publish(test6_c1, mqttsas_topic, strlen("TERMINATE"), "TERMINATE", 0, 0, NULL);
assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc); assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);*/
/* test6_socket_close(((MQTTClients*)test6_c1)->c->socket); */ test6_socket_close(((MQTTClients*)test6_c1)->c->socket);
MyLog(LOGA_INFO, "Waiting to receive the will message"); MyLog(LOGA_INFO, "Waiting to receive the will message");
count = 0; count = 0;
...@@ -1084,9 +1074,8 @@ int test6(struct Options options) ...@@ -1084,9 +1074,8 @@ int test6(struct Options options)
rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic); rc = MQTTClient_unsubscribe(test6_c2, test6_will_topic);
assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc); assert("Good rc from unsubscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
rc = MQTTClient_isConnected(test6_c1); rc = MQTTClient_isConnected(test6_c2);
assert("Client-2 still connected", rc == 1, "isconnected is %d", rc); assert("Client-2 still connected", rc == 1, "isconnected is %d", rc);
#endif
rc = MQTTClient_isConnected(test6_c1); rc = MQTTClient_isConnected(test6_c1);
assert("Client-1 not connected", rc == 0, "isconnected is %d", rc); assert("Client-1 not connected", rc == 0, "isconnected is %d", rc);
......
...@@ -976,15 +976,22 @@ exit: ...@@ -976,15 +976,22 @@ exit:
} }
typedef struct
{
MQTTAsync c;
int should_fail;
} test6_client_info;
void test6_onConnectFailure(void* context, MQTTAsync_failureData* response) void test6_onConnectFailure(void* context, MQTTAsync_failureData* response)
{ {
MQTTAsync c = (MQTTAsync)context; test6_client_info cinfo = *(test6_client_info*)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc;
MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context); MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
MyLog(LOGA_INFO, "Connack rc is %d", response ? response->code : -999); if (response)
MyLog(LOGA_INFO, "Connack rc is %d", response->code);
assert("Should fail to connect", cinfo.should_fail, "should_fail was %d", cinfo.should_fail);
test_finished = 1; test_finished = 1;
} }
...@@ -992,11 +999,11 @@ void test6_onConnectFailure(void* context, MQTTAsync_failureData* response) ...@@ -992,11 +999,11 @@ void test6_onConnectFailure(void* context, MQTTAsync_failureData* response)
void test6_onConnect(void* context, MQTTAsync_successData* response) void test6_onConnect(void* context, MQTTAsync_successData* response)
{ {
MQTTAsync c = (MQTTAsync)context; test6_client_info cinfo = *(test6_client_info*)context;
MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
int rc;
MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context); MyLog(LOGA_DEBUG, "In connect success callback, context %p", context);
assert("Should connect correctly", !cinfo.should_fail, "should_fail was %d", cinfo.should_fail);
test_finished = 1; test_finished = 1;
} }
...@@ -1010,38 +1017,72 @@ Test6: HA connections ...@@ -1010,38 +1017,72 @@ Test6: HA connections
int test6(struct Options options) int test6(struct Options options)
{ {
int subsqos = 2; int subsqos = 2;
MQTTAsync c; test6_client_info cinfo;
MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer; MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer; MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
int rc = 0; int rc = 0;
char* test_topic = "C client test1"; char* test_topic = "C client test1";
char* uris[2] = {"tcp://localhost:1884", "tcp://localhost:1883"}; char* uris[2] = {options.connection, options.connection};
test_finished = failures = 0; failures = 0;
MyLog(LOGA_INFO, "Starting test 7 - HA connections"); MyLog(LOGA_INFO, "Starting test 7 - HA connections");
fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\""); fprintf(xml, "<testcase classname=\"test4\" name=\"HA connections\"");
global_start_time = start_clock(); global_start_time = start_clock();
rc = MQTTAsync_create(&c, options.connection, "a clientid that is too long to be accepted", test_finished = 0;
cinfo.should_fail = 1; /* fail to connect */
rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection test",
MQTTCLIENT_PERSISTENCE_DEFAULT, NULL); MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc); assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
if (rc != MQTTASYNC_SUCCESS) if (rc != MQTTASYNC_SUCCESS)
{ {
MQTTAsync_destroy(&c); MQTTAsync_destroy(&cinfo.c);
goto exit; goto exit;
} }
rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL); rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL);
assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
opts.onSuccess = test6_onConnect; opts.onSuccess = test6_onConnect;
opts.onFailure = test6_onConnectFailure; opts.onFailure = test6_onConnectFailure;
opts.context = c; opts.context = &cinfo;
MyLog(LOGA_DEBUG, "Connecting");
rc = MQTTAsync_connect(cinfo.c, &opts);
rc = 0;
assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
if (rc != MQTTASYNC_SUCCESS)
goto exit;
while (!test_finished)
#if defined(WIN32)
Sleep(100);
#else
usleep(10000L);
#endif
test_finished = 0;
cinfo.should_fail = 0; /* should connect */
rc = MQTTAsync_create(&cinfo.c, "tcp://rubbish:1883", "async ha connection test",
MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
if (rc != MQTTASYNC_SUCCESS)
{
MQTTAsync_destroy(&cinfo.c);
goto exit;
}
rc = MQTTAsync_setCallbacks(cinfo.c, cinfo.c, NULL, test1_messageArrived, NULL);
assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
opts.onSuccess = test6_onConnect;
opts.onFailure = test6_onConnectFailure;
opts.context = &cinfo;
opts.serverURIs = uris; opts.serverURIs = uris;
opts.serverURIcount = 2; opts.serverURIcount = 2;
MyLog(LOGA_DEBUG, "Connecting"); MyLog(LOGA_DEBUG, "Connecting");
rc = MQTTAsync_connect(c, &opts); rc = MQTTAsync_connect(cinfo.c, &opts);
rc = 0; rc = 0;
assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc); assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
if (rc != MQTTASYNC_SUCCESS) if (rc != MQTTASYNC_SUCCESS)
...@@ -1054,7 +1095,8 @@ int test6(struct Options options) ...@@ -1054,7 +1095,8 @@ int test6(struct Options options)
usleep(10000L); usleep(10000L);
#endif #endif
MQTTAsync_destroy(&c);
MQTTAsync_destroy(&cinfo.c);
exit: exit:
MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.", MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment