32 #if !defined(_WINDOWS) 34 #include <sys/socket.h> 43 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 59 "mqtt.eclipse.org:1883",
71 if (strcmp(argv[count],
"--test_no") == 0)
78 else if (strcmp(argv[count],
"--connection") == 0)
85 else if (strcmp(argv[count],
"--proxy_connection") == 0)
92 else if (strcmp(argv[count],
"--verbose") == 0)
103 #include <sys/timeb.h> 108 #if defined(_WIN32) || defined(_WINDOWS) 118 #if defined(_WIN32) || defined(_WINDOWS) 122 gettimeofday(&ts, NULL);
123 localtime_r(&ts.tv_sec, &timeinfo);
125 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", &timeinfo);
127 #if defined(_WIN32) || defined(_WINDOWS) 128 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
130 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
133 va_start(args, format);
134 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
137 printf(
"%s\n", msg_buf);
143 #if defined(_WIN32) || defined(_WIN64) 146 usleep(milliseconds*1000);
150 #if defined(_WIN32) || defined(_WINDOWS) 151 #define START_TIME_TYPE DWORD 152 static DWORD start_time = 0;
155 return GetTickCount();
158 #define START_TIME_TYPE struct timespec 161 static struct timespec start;
162 clock_gettime(CLOCK_REALTIME, &start);
166 #define START_TIME_TYPE struct timeval 170 struct timeval start_time;
171 gettimeofday(&start_time, NULL);
179 return GetTickCount() - start_time;
183 long elapsed(
struct timespec start)
185 struct timespec now, res;
187 clock_gettime(CLOCK_REALTIME, &now);
188 ntimersub(now, start, res);
189 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
194 struct timeval now, res;
196 gettimeofday(&now, NULL);
197 timersub(&now, &start_time, &res);
198 return (res.tv_sec) * 1000 + (res.tv_usec) / 1000;
202 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 203 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 219 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
225 fprintf(
xml,
"</testcase>\n");
237 MyLog(
LOGA_INFO,
"Assertion failed, file %s, line %d, description: %s", filename,
238 lineno, description);
240 va_start(args, format);
241 vprintf(format, args);
245 description, filename, lineno);
248 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s",
249 filename, lineno, description);
266 while (tokens[i] != -1)
273 while (i > 0 && ++
count < 10);
274 assert(
"Number of getPendingTokens should be 0", i == 0,
"i was %d ", i);
288 while (tokens[i] != -1)
292 assert(
"Number of getPendingTokens should be 3", i == 3,
"i was %d\n", i);
316 printf(
"%s\n", message);
358 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
366 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
375 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client d, context %p\n", context);
396 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
406 int qoss[2] = {2, 2};
409 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
425 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
432 char* testname =
"test1";
445 sprintf(clientidc,
"paho-test9-1-c-%s",
unique);
446 sprintf(clientidd,
"paho-test9-1-d-%s",
unique);
451 MyLog(
LOGA_INFO,
"Starting Offline buffering 1 - messages while disconnected");
452 fprintf(
xml,
"<testcase classname=\"test1\" name=\"%s\"", testname);
457 NULL, &createOptions);
498 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
534 for (i = 0; i < 3; ++i)
540 sprintf(buf,
"QoS %d message", i);
618 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
626 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
635 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client d, context %p\n", context);
656 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
666 int qoss[2] = {2, 2};
669 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
685 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
692 char* testname =
"test2";
706 sprintf(clientidc,
"paho-test9-2-c-%s",
unique);
707 sprintf(clientidd,
"paho-test9-2-d-%s",
unique);
712 MyLog(
LOGA_INFO,
"Starting Offline buffering 2 - messages while disconnected with serverURIs");
713 fprintf(
xml,
"<testcase classname=\"test2\" name=\"%s\"", testname);
718 NULL, &createOptions);
757 assert(
"Count should be less than 300", count < 300,
"count was %d", count);
789 assert(
"Count should be less than 300", count < 300,
"count was %d", count);
794 assert(
"Count should be less than 300", count < 300,
"count was %d", count);
800 for (i = 0; i < 3; ++i)
806 sprintf(buf,
"QoS %d message", i);
824 assert(
"Count should be less than 300", count < 300,
"count was %d", count);
830 assert(
"Count should be less than 300", count < 300,
"count was %d", count);
888 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
896 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
905 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client d, context %p\n", context);
926 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
936 int qoss[2] = {2, 2};
939 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
955 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
962 char* testname =
"test3";
975 sprintf(clientidc,
"paho-test9-3-c-%s",
unique);
976 sprintf(clientidd,
"paho-test9-3-d-%s",
unique);
981 MyLog(
LOGA_INFO,
"Starting Offline buffering 3 - messages while disconnected");
982 fprintf(
xml,
"<testcase classname=\"test3\" name=\"%s\"", testname);
987 NULL, &createOptions);
1028 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
1065 for (i = 0; i < 3; ++i)
1071 sprintf(buf,
"QoS %d message", i);
1146 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
1154 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
1163 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client d, context %p\n", context);
1184 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
1194 int qoss[2] = {2, 2};
1197 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
1213 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
1220 char* testname =
"test4";
1234 sprintf(clientidc,
"paho-test9-4-c-%s",
unique);
1235 sprintf(clientidd,
"paho-test9-4-d-%s",
unique);
1240 MyLog(
LOGA_INFO,
"Starting Offline buffering 4 - messages while disconnected with serverURIs");
1241 fprintf(
xml,
"<testcase classname=\"test4\" name=\"%s\"", testname);
1246 NULL, &createOptions);
1285 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
1324 for (i = 0; i < 3; ++i)
1330 sprintf(buf,
"QoS %d message", i);
1405 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
1413 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
1422 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client d, context %p\n", context);
1443 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
1453 int qoss[2] = {2, 2};
1456 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
1470 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
1477 char* testname =
"test5";
1490 sprintf(clientidc,
"paho-test9-5-c-%s",
unique);
1491 sprintf(clientidd,
"paho-test9-5-d-%s",
unique);
1497 fprintf(
xml,
"<testcase classname=\"test5\" name=\"%s\"", testname);
1503 NULL, &createOptions);
1544 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
1580 for (i = 0; i < 5; ++i)
1586 sprintf(buf,
"QoS %d message", i);
1632 char* testname =
"test6";
1651 sprintf(clientidc,
"paho-test9-6-c-%s",
unique);
1652 sprintf(clientidd,
"paho-test9-6-d-%s",
unique);
1657 MyLog(
LOGA_INFO,
"Starting Offline buffering 6 - max buffered with binary will");
1658 fprintf(
xml,
"<testcase classname=\"test6\" name=\"%s\"", testname);
1664 NULL, &createOptions);
1705 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
1742 for (i = 0; i < 5; ++i)
1748 sprintf(buf,
"QoS %d message", i);
1826 MyLog(
LOGA_DEBUG,
"In connected callback for client c, context %p\n", context);
1832 MyLog(
LOGA_DEBUG,
"In c connect onFailure callback, context %p", context);
1843 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
1856 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
1866 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
1875 int qoss[2] = {2, 2};
1878 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
1892 char* testname =
"test7";
1911 sprintf(clientidc,
"paho-test9-7-c-%s",
unique);
1912 sprintf(clientidd,
"paho-test9-7-d-%s",
unique);
1917 MyLog(
LOGA_INFO,
"Starting Offline buffering 7 - fill TCP buffer");
1918 fprintf(
xml,
"<testcase classname=\"test7\" name=\"%s\"", testname);
1964 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
1996 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2002 MyLog(
LOGA_DEBUG,
"Now we can send some messages to be buffered by TCP");
2007 for (i = 0; i < 50000; ++i)
2012 sprintf(buf,
"QoS %d message", pubmsg.
qos);
2092 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
2102 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
2117 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
2128 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
2135 char* testname =
"test8";
2148 sprintf(clientidc,
"paho-test9-8-c-%s",
unique);
2149 sprintf(clientidd,
"paho-test9-8-d-%s",
unique);
2154 MyLog(
LOGA_INFO,
"Starting Offline buffering 8 - send messages before successful connect");
2155 fprintf(
xml,
"<testcase classname=\"test8\" name=\"%s\"", testname);
2162 NULL, &createOptions);
2171 for (i = 0; i < 5; ++i)
2177 sprintf(buf,
"QoS %d message", i);
2200 NULL, &createOptions);
2229 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2232 for (i = 0; i < 5; ++i)
2238 sprintf(buf,
"QoS %d message", i);
2269 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2306 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
2316 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
2323 char* testname =
"test9";
2334 int no_buffered_messages = 50000;
2337 sprintf(clientidc,
"paho-test9-9-c-%s",
unique);
2342 MyLog(
LOGA_INFO,
"Starting Offline buffering - large nos messages on create");
2343 fprintf(
xml,
"<testcase classname=\"test\" name=\"%s\"", testname);
2350 NULL, &createOptions);
2359 for (i = 0; i < no_buffered_messages; ++i)
2365 sprintf(buf,
"QoS %d message", i);
2377 MyLog(
LOGA_INFO,
"Create starting with %d messages", no_buffered_messages);
2380 NULL, &createOptions);
2404 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2437 int sequence_no = atoi(message->
payload);
2455 MyLog(
LOGA_DEBUG,
"In subscribe onSuccess callback for client d, %p granted qos %d", c, response->
alt.
qos);
2465 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
2480 MyLog(
LOGA_DEBUG,
"In connect onFailure callback, context %p", context);
2491 MyLog(
LOGA_DEBUG,
"In connect onSuccess callback for client c, context %p\n", context);
2498 char* testname =
"test10";
2511 sprintf(clientidc,
"paho-test9-10-c-%s",
unique);
2512 sprintf(clientidd,
"paho-test9-10-d-%s",
unique);
2517 MyLog(
LOGA_INFO,
"Starting Offline buffering 10 - delete oldest buffered messages first");
2518 fprintf(
xml,
"<testcase classname=\"test9\" name=\"%s\"", testname);
2534 NULL, &createOptions);
2563 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2573 sprintf(buf,
"%d message no, QoS %d", i, pubmsg.
qos);
2600 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2622 sprintf(buf,
"%d message no, QoS %d", i, pubmsg.
qos);
2637 NULL, &createOptions);
2654 assert(
"Count should be less than 10000", count < 10000,
"count was %d", count);
2681 int* numtests = &
tests;
2683 int (*
tests[])() = { NULL,
test1,
test2,
test3,
test4,
test5,
test6,
test7,
test8,
test9,
test10};
2686 srand((
unsigned)
time(&randtime));
2687 sprintf(
unique,
"%u", rand());
2690 xml = fopen(
"TEST-test9.xml",
"w");
2691 fprintf(
xml,
"<testsuite name=\"test9\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests) - 1));
2717 fprintf(
xml,
"</testsuite>\n");
int test3(struct Options options)
MQTTAsync_onFailure * onFailure
void test4cConnected(void *context, char *cause)
void test8donSubscribe(void *context, MQTTAsync_successData *response)
int test10_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
int test9(struct Options options)
void handleTrace(enum MQTTASYNC_TRACE_LEVELS level, char *message)
void MyLog(int LOGA_level, char *format,...)
void test2donSubscribe(void *context, MQTTAsync_successData *response)
enum MQTTPropertyCodes value
int MQTTAsync_createWithOptions(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context, MQTTAsync_createOptions *options)
void test3cOnFailure(void *context, MQTTAsync_failureData *response)
int test2_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
int test7_messages_received
void test1dOnConnect(void *context, MQTTAsync_successData *response)
void test2dOnConnect(void *context, MQTTAsync_successData *response)
union MQTTAsync_successData::@46 alt
int test5_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
int sendWhileDisconnected
void test4dOnFailure(void *context, MQTTAsync_failureData *response)
#define MQTTAsync_responseOptions_initializer
void test1cOnConnect(void *context, MQTTAsync_successData *response)
void test5dOnConnect(void *context, MQTTAsync_successData *response)
int test8(struct Options options)
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
void test7donSubscribe(void *context, MQTTAsync_successData *response)
int test4_messages_received
void test7cOnConnectSuccess(void *context, MQTTAsync_successData *response)
void MySleep(long milliseconds)
int test4_will_message_received
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
void MQTTAsync_free(void *memory)
void MQTTAsync_freeMessage(MQTTAsync_message **message)
#define MQTTASYNC_DISCONNECTED
int MQTTAsync_setConnected(MQTTAsync handle, void *context, MQTTAsync_connected *connected)
int test7_will_message_received
void test9cOnConnect(void *context, MQTTAsync_successData *response)
void MQTTAsync_setTraceCallback(MQTTAsync_traceCallback *callback)
int test1_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
void test7cOnConnectFailure(void *context, MQTTAsync_failureData *response)
int test10(struct Options options)
void test5cOnFailure(void *context, MQTTAsync_failureData *response)
void write_test_result(void)
#define assert(a, b, c, d)
START_TIME_TYPE global_start_time
int test4_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
void test3dOnConnect(void *context, MQTTAsync_successData *response)
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
int test1_will_message_received
void test5cOnConnect(void *context, MQTTAsync_successData *response)
void test2cOnConnect(void *context, MQTTAsync_successData *response)
#define MQTTAsync_willOptions_initializer
int test1(struct Options options)
int test5(struct Options options)
void test2dOnFailure(void *context, MQTTAsync_failureData *response)
int test7(struct Options options)
int test7_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
void test4dOnConnect(void *context, MQTTAsync_successData *response)
int MQTTAsync_getPendingTokens(MQTTAsync handle, MQTTAsync_token **tokens)
void test4cOnFailure(void *context, MQTTAsync_failureData *response)
int test8_messages_received
void test7cConnected(void *context, char *cause)
int test10OnFailureCalled
void test8dOnConnect(void *context, MQTTAsync_successData *response)
int test6(struct Options options)
#define MQTTVERSION_3_1_1
int test8_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
struct MQTTAsync_willOptions::@54 payload
int test3_will_message_received
void test1cConnected(void *context, char *cause)
#define MQTTAsync_createOptions_initializer
int MQTTAsync_isConnected(MQTTAsync handle)
void test3dOnFailure(void *context, MQTTAsync_failureData *response)
int MQTTAsync_subscribeMany(MQTTAsync handle, int count, char *const *topic, int *qos, MQTTAsync_responseOptions *response)
START_TIME_TYPE start_clock(void)
void test8cOnConnect(void *context, MQTTAsync_successData *response)
void test5dOnFailure(void *context, MQTTAsync_failureData *response)
#define MQTTASYNC_MAX_BUFFERED_MESSAGES
#define MQTTAsync_connectOptions_initializer
MQTTAsync_onSuccess * onSuccess
int test10_messages_received
MQTTAsync_willOptions * will
void test10OnFailure(void *context, MQTTAsync_failureData *response)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
void test3cOnConnect(void *context, MQTTAsync_successData *response)
void test4cOnConnect(void *context, MQTTAsync_successData *response)
void test3cConnected(void *context, char *cause)
int test1_messages_received
void MQTTAsync_destroy(MQTTAsync *handle)
int test2(struct Options options)
void test7dOnConnectSuccess(void *context, MQTTAsync_successData *response)
void test2cOnFailure(void *context, MQTTAsync_failureData *response)
#define MQTTASYNC_SUCCESS
void MQTTAsync_setTraceLevel(enum MQTTASYNC_TRACE_LEVELS level)
int MQTTAsync_sendMessage(MQTTAsync handle, const char *destinationName, const MQTTAsync_message *message, MQTTAsync_responseOptions *response)
void test5cConnected(void *context, char *cause)
void test9OnFailure(void *context, MQTTAsync_failureData *response)
void test7dOnConnectFailure(void *context, MQTTAsync_failureData *response)
int main(int argc, char **argv)
#define MQTTCLIENT_PERSISTENCE_NONE
void test1dOnFailure(void *context, MQTTAsync_failureData *response)
void assert3PendingTokens(MQTTAsync c)
int test5_messages_received
int test4(struct Options options)
void test4donSubscribe(void *context, MQTTAsync_successData *response)
int MQTTAsync_reconnect(MQTTAsync handle)
int test2_messages_received
void getopts(int argc, char **argv)
void test2cConnected(void *context, char *cause)
void waitForNoPendingTokens(MQTTAsync c)
int allowDisconnectedSendAtAnyTime
void test1donSubscribe(void *context, MQTTAsync_successData *response)
int test9_messages_received
#define MQTTAsync_message_initializer
long elapsed(START_TIME_TYPE start_time)
int test5_will_message_received
MQTTAsync_onSuccess * onSuccess
int test3_messages_received
void test10cOnConnect(void *context, MQTTAsync_successData *response)
void test1cOnFailure(void *context, MQTTAsync_failureData *response)
int test3_messageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
void test10dOnConnect(void *context, MQTTAsync_successData *response)
void test5donSubscribe(void *context, MQTTAsync_successData *response)
int test2_will_message_received
void test8OnFailure(void *context, MQTTAsync_failureData *response)
void test10donSubscribe(void *context, MQTTAsync_successData *response)
void test3donSubscribe(void *context, MQTTAsync_successData *response)