test2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - fix thread id display
  16. *******************************************************************************/
  17. /**
  18. * @file
  19. * Multi-threaded tests for the Eclipse Paho MQTT C client
  20. */
  21. #include "MQTTClient.h"
  22. #include "Thread.h"
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #if !defined(_WINDOWS)
  26. #include <sys/time.h>
  27. #include <sys/socket.h>
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #define WINAPI
  31. #else
  32. #include <windows.h>
  33. #define setenv(a, b, c) _putenv_s(a, b)
  34. #endif
  35. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  36. void usage(void)
  37. {
  38. printf("help!!\n");
  39. exit(EXIT_FAILURE);
  40. }
  41. struct Options
  42. {
  43. char* connection; /**< connection to system under test. */
  44. char** haconnections;
  45. int hacount;
  46. int verbose;
  47. int test_no;
  48. int MQTTVersion;
  49. int iterations;
  50. } options =
  51. {
  52. "tcp://localhost:1883",
  53. NULL,
  54. 0,
  55. 0,
  56. 0,
  57. MQTTVERSION_DEFAULT,
  58. 1,
  59. };
  60. void getopts(int argc, char** argv)
  61. {
  62. int count = 1;
  63. while (count < argc)
  64. {
  65. if (strcmp(argv[count], "--test_no") == 0)
  66. {
  67. if (++count < argc)
  68. options.test_no = atoi(argv[count]);
  69. else
  70. usage();
  71. }
  72. else if (strcmp(argv[count], "--connection") == 0)
  73. {
  74. if (++count < argc)
  75. {
  76. options.connection = argv[count];
  77. printf("\nSetting connection to %s\n", options.connection);
  78. }
  79. else
  80. usage();
  81. }
  82. else if (strcmp(argv[count], "--haconnections") == 0)
  83. {
  84. if (++count < argc)
  85. {
  86. char* tok = strtok(argv[count], " ");
  87. options.hacount = 0;
  88. options.haconnections = malloc(sizeof(char*) * 5);
  89. while (tok)
  90. {
  91. options.haconnections[options.hacount] = malloc(strlen(tok) + 1);
  92. strcpy(options.haconnections[options.hacount], tok);
  93. options.hacount++;
  94. tok = strtok(NULL, " ");
  95. }
  96. }
  97. else
  98. usage();
  99. }
  100. else if (strcmp(argv[count], "--MQTTversion") == 0)
  101. {
  102. if (++count < argc)
  103. {
  104. options.MQTTVersion = atoi(argv[count]);
  105. printf("setting MQTT version to %d\n", options.MQTTVersion);
  106. }
  107. else
  108. usage();
  109. }
  110. else if (strcmp(argv[count], "--iterations") == 0)
  111. {
  112. if (++count < argc)
  113. options.iterations = atoi(argv[count]);
  114. else
  115. usage();
  116. }
  117. else if (strcmp(argv[count], "--verbose") == 0)
  118. {
  119. options.verbose = 1;
  120. printf("\nSetting verbose on\n");
  121. }
  122. count++;
  123. }
  124. }
  125. #define LOGA_DEBUG 0
  126. #define LOGA_INFO 1
  127. #include <stdarg.h>
  128. #include <time.h>
  129. #include <sys/timeb.h>
  130. void MyLog(int LOGA_level, char* format, ...)
  131. {
  132. static char msg_buf[256];
  133. va_list args;
  134. #if defined(_WIN32) || defined(_WINDOWS)
  135. struct timeb ts;
  136. #else
  137. struct timeval ts;
  138. #endif
  139. struct tm timeinfo;
  140. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  141. return;
  142. #if defined(_WIN32) || defined(_WINDOWS)
  143. ftime(&ts);
  144. localtime_s(&timeinfo, &ts.time);
  145. #else
  146. gettimeofday(&ts, NULL);
  147. localtime_r(&ts.tv_sec, &timeinfo);
  148. #endif
  149. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  150. #if defined(_WIN32) || defined(_WINDOWS)
  151. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  152. #else
  153. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  154. #endif
  155. va_start(args, format);
  156. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  157. va_end(args);
  158. printf("%s\n", msg_buf);
  159. fflush(stdout);
  160. }
  161. #if defined(_WIN32) || defined(_WINDOWS)
  162. #define mqsleep(A) Sleep(1000*A)
  163. #define START_TIME_TYPE DWORD
  164. static DWORD start_time = 0;
  165. START_TIME_TYPE start_clock(void)
  166. {
  167. return GetTickCount();
  168. }
  169. #elif defined(AIX)
  170. #define mqsleep sleep
  171. #define START_TIME_TYPE struct timespec
  172. START_TIME_TYPE start_clock(void)
  173. {
  174. static struct timespec start;
  175. clock_gettime(CLOCK_REALTIME, &start);
  176. return start;
  177. }
  178. #else
  179. #define mqsleep sleep
  180. #define START_TIME_TYPE struct timeval
  181. /* TODO - unused - remove? static struct timeval start_time; */
  182. START_TIME_TYPE start_clock(void)
  183. {
  184. struct timeval start_time;
  185. gettimeofday(&start_time, NULL);
  186. return start_time;
  187. }
  188. #endif
  189. #if defined(_WIN32)
  190. long elapsed(START_TIME_TYPE start_time)
  191. {
  192. return GetTickCount() - start_time;
  193. }
  194. #elif defined(AIX)
  195. #define assert(a)
  196. long elapsed(struct timespec start)
  197. {
  198. struct timespec now, res;
  199. clock_gettime(CLOCK_REALTIME, &now);
  200. ntimersub(now, start, res);
  201. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  202. }
  203. #else
  204. long elapsed(START_TIME_TYPE start_time)
  205. {
  206. struct timeval now, res;
  207. gettimeofday(&now, NULL);
  208. timersub(&now, &start_time, &res);
  209. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  210. }
  211. #endif
  212. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  213. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  214. int tests = 0;
  215. int failures = 0;
  216. FILE* xml;
  217. START_TIME_TYPE global_start_time;
  218. char output[3000];
  219. char* cur_output = output;
  220. void write_test_result(void)
  221. {
  222. long duration = elapsed(global_start_time);
  223. fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
  224. if (cur_output != output)
  225. {
  226. fprintf(xml, "%s", output);
  227. cur_output = output;
  228. }
  229. fprintf(xml, "</testcase>\n");
  230. }
  231. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  232. {
  233. ++tests;
  234. if (!value)
  235. {
  236. va_list args;
  237. ++failures;
  238. MyLog(LOGA_INFO, "Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  239. va_start(args, format);
  240. vprintf(format, args);
  241. va_end(args);
  242. cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
  243. description, filename, lineno);
  244. }
  245. else
  246. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  247. }
  248. #if defined(_WIN32) || defined(_WIN64)
  249. mutex_type deliveryCompleted_mutex = NULL;
  250. #else
  251. pthread_mutex_t deliveryCompleted_mutex_store = PTHREAD_MUTEX_INITIALIZER;
  252. mutex_type deliveryCompleted_mutex = &deliveryCompleted_mutex_store;
  253. #endif
  254. void lock_mutex(mutex_type amutex)
  255. {
  256. int rc = Thread_lock_mutex(amutex);
  257. if (rc != 0)
  258. MyLog(LOGA_INFO, "Error %s locking mutex", strerror(rc));
  259. }
  260. void unlock_mutex(mutex_type amutex)
  261. {
  262. int rc = Thread_unlock_mutex(amutex);
  263. if (rc != 0)
  264. MyLog(LOGA_INFO, "Error %s unlocking mutex", strerror(rc));
  265. }
  266. /*********************************************************************
  267. Test1: multiple threads to single client object
  268. *********************************************************************/
  269. volatile int test1_arrivedcount = 0;
  270. volatile int test1_arrivedcount_qos[3] = {0, 0, 0};
  271. volatile int test1_deliveryCompleted = 0;
  272. MQTTClient_message test1_pubmsg_check = MQTTClient_message_initializer;
  273. void test1_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
  274. {
  275. lock_mutex(deliveryCompleted_mutex);
  276. MyLog(LOGA_DEBUG, "Delivery complete for token %d", dt);
  277. ++test1_deliveryCompleted;
  278. unlock_mutex(deliveryCompleted_mutex);
  279. }
  280. int test1_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  281. {
  282. ++(test1_arrivedcount_qos[m->qos]);
  283. ++test1_arrivedcount;
  284. MyLog(LOGA_DEBUG, "messageArrived: %d message received on topic %s is %.*s.",
  285. test1_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
  286. if (test1_pubmsg_check.payloadlen != m->payloadlen ||
  287. memcmp(m->payload, test1_pubmsg_check.payload, m->payloadlen) != 0)
  288. {
  289. failures++;
  290. MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test1_pubmsg_check.payloadlen, m->payloadlen);
  291. }
  292. MQTTClient_free(topicName);
  293. MQTTClient_freeMessage(&m);
  294. return 1;
  295. }
  296. struct thread_parms
  297. {
  298. MQTTClient* c;
  299. int qos;
  300. char* test_topic;
  301. };
  302. static int iterations = 50;
  303. thread_return_type WINAPI test1_sendAndReceive(void* n)
  304. {
  305. MQTTClient_deliveryToken dt;
  306. int i = 0;
  307. int rc = 0;
  308. int wait_seconds = 30;
  309. MQTTClient_message test1_pubmsg = MQTTClient_message_initializer;
  310. int subsqos = 2;
  311. struct thread_parms *parms = n;
  312. MQTTClient* c = parms->c;
  313. int qos = parms->qos;
  314. char* test_topic = parms->test_topic;
  315. rc = MQTTClient_subscribe(c, test_topic, subsqos);
  316. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  317. MyLog(LOGA_INFO, "Thread %u, %d messages at QoS %d", Thread_getid(), iterations, qos);
  318. test1_pubmsg.payload = test1_pubmsg_check.payload;
  319. test1_pubmsg.payloadlen = test1_pubmsg_check.payloadlen;
  320. test1_pubmsg.retained = 0;
  321. test1_pubmsg.qos = qos;
  322. for (i = 1; i <= iterations; ++i)
  323. {
  324. if (i % 10 == 0)
  325. rc = MQTTClient_publish(c, test_topic, test1_pubmsg.payloadlen, test1_pubmsg.payload,
  326. qos, test1_pubmsg.retained, &dt);
  327. else
  328. rc = MQTTClient_publishMessage(c, test_topic, &test1_pubmsg, &dt);
  329. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  330. #if defined(_WIN32)
  331. Sleep(100);
  332. #else
  333. usleep(100000L);
  334. #endif
  335. wait_seconds = 30;
  336. while ((test1_arrivedcount_qos[qos] < i) && (wait_seconds-- > 0))
  337. {
  338. MyLog(LOGA_DEBUG, "Arrived %d count %d", test1_arrivedcount_qos[qos], i);
  339. #if defined(_WIN32)
  340. Sleep(1000);
  341. #else
  342. usleep(1000000L);
  343. #endif
  344. }
  345. assert("Message Arrived", wait_seconds > 0,
  346. "Timed out waiting for message %d\n", i);
  347. }
  348. #if defined(_WINDOWS)
  349. return 0;
  350. #else
  351. return NULL;
  352. #endif
  353. }
  354. int test1(struct Options options)
  355. {
  356. MQTTClient c;
  357. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  358. MQTTClient_willOptions wopts = MQTTClient_willOptions_initializer;
  359. int rc = 0;
  360. char* test_topic = "C client test1";
  361. fprintf(xml, "<testcase classname=\"test1\" name=\"multiple threads using same client object\"");
  362. global_start_time = start_clock();
  363. failures = 0;
  364. MyLog(LOGA_INFO, "Starting test 1 - multiple threads using same client object");
  365. rc = MQTTClient_create(&c, options.connection, "single_object, multiple threads",
  366. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  367. assert("good rc from create", rc == MQTTCLIENT_SUCCESS, "rc was %d\n", rc);
  368. if (rc != MQTTCLIENT_SUCCESS)
  369. {
  370. MQTTClient_destroy(&c);
  371. goto exit;
  372. }
  373. rc = MQTTClient_setCallbacks(c, NULL, NULL, test1_messageArrived, test1_deliveryComplete);
  374. assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  375. opts.keepAliveInterval = 20;
  376. opts.cleansession = 1;
  377. opts.username = "testuser";
  378. opts.password = "testpassword";
  379. opts.MQTTVersion = options.MQTTVersion;
  380. if (options.haconnections != NULL)
  381. {
  382. opts.serverURIs = options.haconnections;
  383. opts.serverURIcount = options.hacount;
  384. }
  385. opts.will = &wopts;
  386. opts.will->message = "will message";
  387. opts.will->qos = 1;
  388. opts.will->retained = 0;
  389. opts.will->topicName = "will topic";
  390. opts.will = NULL;
  391. MyLog(LOGA_DEBUG, "Connecting");
  392. rc = MQTTClient_connect(c, &opts);
  393. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  394. if (rc != MQTTCLIENT_SUCCESS)
  395. goto exit;
  396. test1_pubmsg_check.payload = "a much longer message that we can shorten to the extent that we need to";
  397. test1_pubmsg_check.payloadlen = 27;
  398. test1_deliveryCompleted = test1_arrivedcount = 0;
  399. struct thread_parms parms0 = {c, 0, test_topic};
  400. Thread_start(test1_sendAndReceive, (void*)&parms0);
  401. struct thread_parms parms1 = {c, 1, test_topic};
  402. Thread_start(test1_sendAndReceive, (void*)&parms1);
  403. struct thread_parms parms2 = {c, 2, test_topic};
  404. Thread_start(test1_sendAndReceive, (void*)&parms2);
  405. /* MQTT servers can send a message to a subscriber before the server has
  406. completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
  407. allow time for the final delivery complete callback before checking
  408. that all expected callbacks have been made */
  409. int wait_seconds = 90;
  410. while (((test1_arrivedcount < iterations*3) || (test1_deliveryCompleted < iterations*2)) && (wait_seconds-- > 0))
  411. {
  412. #if defined(_WIN32)
  413. Sleep(1000);
  414. #else
  415. usleep(1000000L);
  416. #endif
  417. }
  418. assert("Arrived count == 150", test1_arrivedcount == iterations*3, "arrivedcount was %d", test1_arrivedcount);
  419. assert("All Deliveries Complete", test1_deliveryCompleted == iterations*2,
  420. "Number of deliveryCompleted callbacks was %d\n", test1_deliveryCompleted);
  421. MyLog(LOGA_DEBUG, "Stopping\n");
  422. rc = MQTTClient_unsubscribe(c, test_topic);
  423. assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  424. rc = MQTTClient_disconnect(c, 0);
  425. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  426. /* Just to make sure we can connect again */
  427. rc = MQTTClient_connect(c, &opts);
  428. assert("Connect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  429. rc = MQTTClient_disconnect(c, 0);
  430. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  431. MQTTClient_destroy(&c);
  432. exit:
  433. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  434. (failures == 0) ? "passed" : "failed", tests, failures);
  435. write_test_result();
  436. return failures;
  437. }
  438. /*********************************************************************
  439. Test2: multiple client objects used from multiple threads
  440. *********************************************************************/
  441. volatile int test2_arrivedcount = 0;
  442. volatile int test2_deliveryCompleted = 0;
  443. MQTTClient_message test2_pubmsg = MQTTClient_message_initializer;
  444. void test2_deliveryComplete(void* context, MQTTClient_deliveryToken dt)
  445. {
  446. lock_mutex(deliveryCompleted_mutex);
  447. MyLog(LOGA_DEBUG, "Delivery complete for token %d", dt);
  448. ++test2_deliveryCompleted;
  449. unlock_mutex(deliveryCompleted_mutex);
  450. }
  451. int test2_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* m)
  452. {
  453. ++test2_arrivedcount;
  454. MyLog(LOGA_DEBUG, "Callback: %d message received on topic %s is %.*s.",
  455. test2_arrivedcount, topicName, m->payloadlen, (char*)(m->payload));
  456. if (test2_pubmsg.payloadlen != m->payloadlen ||
  457. memcmp(m->payload, test2_pubmsg.payload, m->payloadlen) != 0)
  458. {
  459. failures++;
  460. MyLog(LOGA_INFO, "Error: wrong data received lengths %d %d\n", test2_pubmsg.payloadlen, m->payloadlen);
  461. }
  462. MQTTClient_free(topicName);
  463. MQTTClient_freeMessage(&m);
  464. return 1;
  465. }
  466. void test2_sendAndReceive(MQTTClient* c, int qos, char* test_topic)
  467. {
  468. MQTTClient_deliveryToken dt;
  469. int i = 0;
  470. int iterations = 50;
  471. int rc = 0;
  472. int wait_seconds = 0;
  473. test2_deliveryCompleted = 0;
  474. MyLog(LOGA_INFO, "%d messages at QoS %d", iterations, qos);
  475. test2_pubmsg.payload = "a much longer message that we can shorten to the extent that we need to";
  476. test2_pubmsg.payloadlen = 27;
  477. test2_pubmsg.qos = qos;
  478. test2_pubmsg.retained = 0;
  479. for (i = 1; i <= iterations; ++i)
  480. {
  481. if (i % 10 == 0)
  482. rc = MQTTClient_publish(c, test_topic, test2_pubmsg.payloadlen, test2_pubmsg.payload,
  483. test2_pubmsg.qos, test2_pubmsg.retained, NULL);
  484. else
  485. rc = MQTTClient_publishMessage(c, test_topic, &test2_pubmsg, &dt);
  486. assert("Good rc from publish", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  487. #if defined(_WIN32)
  488. Sleep(100);
  489. #else
  490. usleep(100000L);
  491. #endif
  492. wait_seconds = 10;
  493. while ((test2_arrivedcount < i) && (wait_seconds-- > 0))
  494. {
  495. MyLog(LOGA_DEBUG, "Arrived %d count %d", test2_arrivedcount, i);
  496. #if defined(_WIN32)
  497. Sleep(1000);
  498. #else
  499. usleep(1000000L);
  500. #endif
  501. }
  502. assert("Message Arrived", wait_seconds > 0,
  503. "Time out waiting for message %d\n", i );
  504. }
  505. if (qos > 0)
  506. {
  507. /* MQ Telemetry can send a message to a subscriber before the server has
  508. completed the QoS 2 handshake with the publisher. For QoS 1 and 2,
  509. allow time for the final delivery complete callback before checking
  510. that all expected callbacks have been made */
  511. wait_seconds = 40;
  512. while ((test2_deliveryCompleted < iterations) && (wait_seconds-- > 0))
  513. {
  514. MyLog(LOGA_DEBUG, "Delivery Completed %d count %d", test2_deliveryCompleted, i);
  515. #if defined(_WIN32)
  516. Sleep(1000);
  517. #else
  518. usleep(1000000L);
  519. #endif
  520. }
  521. assert("All Deliveries Complete", test2_deliveryCompleted == iterations,
  522. "Number of deliveryCompleted callbacks was %d\n",
  523. test2_deliveryCompleted);
  524. }
  525. }
  526. int test2(struct Options options)
  527. {
  528. char* testname = "test2";
  529. int subsqos = 2;
  530. /* TODO - usused - remove ? MQTTClient_deliveryToken* dt = NULL; */
  531. MQTTClient c;
  532. MQTTClient_connectOptions opts = MQTTClient_connectOptions_initializer;
  533. int rc = 0;
  534. char* test_topic = "C client test2";
  535. fprintf(xml, "<testcase classname=\"test1\" name=\"multi-threaded client using callbacks\"");
  536. MyLog(LOGA_INFO, "Starting test 2 - multi-threaded client using callbacks");
  537. global_start_time = start_clock();
  538. failures = 0;
  539. MQTTClient_create(&c, options.connection, "multi_threaded_sample", MQTTCLIENT_PERSISTENCE_NONE, NULL);
  540. opts.keepAliveInterval = 20;
  541. opts.cleansession = 1;
  542. opts.MQTTVersion = options.MQTTVersion;
  543. if (options.haconnections != NULL)
  544. {
  545. opts.serverURIs = options.haconnections;
  546. opts.serverURIcount = options.hacount;
  547. }
  548. rc = MQTTClient_setCallbacks(c, NULL, NULL, test2_messageArrived, test2_deliveryComplete);
  549. assert("Good rc from setCallbacks", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  550. MyLog(LOGA_DEBUG, "Connecting");
  551. rc = MQTTClient_connect(c, &opts);
  552. assert("Good rc from connect", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  553. if (rc != MQTTCLIENT_SUCCESS)
  554. goto exit;
  555. rc = MQTTClient_subscribe(c, test_topic, subsqos);
  556. assert("Good rc from subscribe", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  557. test2_sendAndReceive(c, 0, test_topic);
  558. test2_sendAndReceive(c, 1, test_topic);
  559. test2_sendAndReceive(c, 2, test_topic);
  560. MyLog(LOGA_DEBUG, "Stopping");
  561. rc = MQTTClient_unsubscribe(c, test_topic);
  562. assert("Unsubscribe successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  563. rc = MQTTClient_disconnect(c, 0);
  564. assert("Disconnect successful", rc == MQTTCLIENT_SUCCESS, "rc was %d", rc);
  565. MQTTClient_destroy(&c);
  566. exit:
  567. MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
  568. (failures == 0) ? "passed" : "failed", testname, tests, failures);
  569. write_test_result();
  570. return failures;
  571. }
  572. int main(int argc, char** argv)
  573. {
  574. int rc = 0;
  575. int (*tests[])() = {NULL, test1};
  576. int i;
  577. #if defined(_WIN32) || defined(_WIN64)
  578. deliveryCompleted_mutex = CreateMutex(NULL, 0, NULL);
  579. #endif
  580. xml = fopen("TEST-test2.xml", "w");
  581. fprintf(xml, "<testsuite name=\"test1\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests) - 1));
  582. setenv("MQTT_C_CLIENT_TRACE", "ON", 1);
  583. setenv("MQTT_C_CLIENT_TRACE_LEVEL", "ERROR", 1);
  584. getopts(argc, argv);
  585. for (i = 0; i < options.iterations; ++i)
  586. {
  587. if (options.test_no == 0)
  588. { /* run all the tests */
  589. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  590. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  591. }
  592. else
  593. rc = tests[options.test_no](options); /* run just the selected test */
  594. }
  595. if (rc == 0)
  596. MyLog(LOGA_INFO, "verdict pass");
  597. else
  598. MyLog(LOGA_INFO, "verdict fail");
  599. fprintf(xml, "</testsuite>\n");
  600. fclose(xml);
  601. return rc;
  602. }