test8.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. /*******************************************************************************
  2. * Copyright (c) 2012, 2021 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. *******************************************************************************/
  16. /**
  17. * @file
  18. * Tests for the Paho MQTT Async C client
  19. */
  20. #include "MQTTAsync.h"
  21. #include "Thread.h"
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #if !defined(_WINDOWS)
  25. #include <sys/time.h>
  26. #include <sys/socket.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #define WINAPI
  30. #else
  31. #include <windows.h>
  32. #endif
  33. #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
  34. void usage(void)
  35. {
  36. printf("help!!\n");
  37. exit(EXIT_FAILURE);
  38. }
  39. struct Options
  40. {
  41. char* connection; /**< connection to system under test. */
  42. int verbose;
  43. int test_no;
  44. int size; /**< size of big message */
  45. } options =
  46. {
  47. "tcp://localhost:1883",
  48. 0,
  49. -1,
  50. 5000000,
  51. };
  52. void getopts(int argc, char** argv)
  53. {
  54. int count = 1;
  55. while (count < argc)
  56. {
  57. if (strcmp(argv[count], "--test_no") == 0)
  58. {
  59. if (++count < argc)
  60. options.test_no = atoi(argv[count]);
  61. else
  62. usage();
  63. }
  64. else if (strcmp(argv[count], "--size") == 0)
  65. {
  66. if (++count < argc)
  67. options.size = atoi(argv[count]);
  68. else
  69. usage();
  70. }
  71. else if (strcmp(argv[count], "--connection") == 0)
  72. {
  73. if (++count < argc)
  74. options.connection = argv[count];
  75. else
  76. usage();
  77. }
  78. else if (strcmp(argv[count], "--verbose") == 0)
  79. options.verbose = 1;
  80. count++;
  81. }
  82. }
  83. #define LOGA_DEBUG 0
  84. #define LOGA_INFO 1
  85. #include <stdarg.h>
  86. #include <time.h>
  87. #include <sys/timeb.h>
  88. void MyLog(int LOGA_level, char* format, ...)
  89. {
  90. static char msg_buf[256];
  91. va_list args;
  92. #if defined(_WIN32) || defined(_WINDOWS)
  93. struct timeb ts;
  94. #else
  95. struct timeval ts;
  96. #endif
  97. struct tm timeinfo;
  98. if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
  99. return;
  100. #if defined(_WIN32) || defined(_WINDOWS)
  101. ftime(&ts);
  102. localtime_s(&timeinfo, &ts.time);
  103. #else
  104. gettimeofday(&ts, NULL);
  105. localtime_r(&ts.tv_sec, &timeinfo);
  106. #endif
  107. strftime(msg_buf, 80, "%Y%m%d %H%M%S", &timeinfo);
  108. #if defined(_WIN32) || defined(_WINDOWS)
  109. sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
  110. #else
  111. sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
  112. #endif
  113. va_start(args, format);
  114. vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
  115. va_end(args);
  116. printf("%s\n", msg_buf);
  117. fflush(stdout);
  118. }
  119. #if defined(_WIN32) || defined(_WINDOWS)
  120. #define mqsleep(A) Sleep(1000*A)
  121. #define START_TIME_TYPE DWORD
  122. static DWORD start_time = 0;
  123. START_TIME_TYPE start_clock(void)
  124. {
  125. return GetTickCount();
  126. }
  127. #elif defined(AIX)
  128. #define mqsleep sleep
  129. #define START_TIME_TYPE struct timespec
  130. START_TIME_TYPE start_clock(void)
  131. {
  132. static struct timespec start;
  133. clock_gettime(CLOCK_REALTIME, &start);
  134. return start;
  135. }
  136. #else
  137. #define mqsleep sleep
  138. #define START_TIME_TYPE struct timeval
  139. /* TODO - unused - remove? static struct timeval start_time; */
  140. START_TIME_TYPE start_clock(void)
  141. {
  142. struct timeval start_time;
  143. gettimeofday(&start_time, NULL);
  144. return start_time;
  145. }
  146. #endif
  147. #if defined(_WIN32)
  148. long elapsed(START_TIME_TYPE start_time)
  149. {
  150. return GetTickCount() - start_time;
  151. }
  152. #elif defined(AIX)
  153. #define assert(a)
  154. long elapsed(struct timespec start)
  155. {
  156. struct timespec now, res;
  157. clock_gettime(CLOCK_REALTIME, &now);
  158. ntimersub(now, start, res);
  159. return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
  160. }
  161. #else
  162. long elapsed(START_TIME_TYPE start_time)
  163. {
  164. struct timeval now, res;
  165. gettimeofday(&now, NULL);
  166. timersub(&now, &start_time, &res);
  167. return (res.tv_sec)*1000 + (res.tv_usec)/1000;
  168. }
  169. #endif
  170. START_TIME_TYPE global_start_time;
  171. #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
  172. #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
  173. int tests = 0;
  174. int failures = 0;
  175. void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
  176. {
  177. ++tests;
  178. if (!value)
  179. {
  180. va_list args;
  181. ++failures;
  182. printf("Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
  183. va_start(args, format);
  184. vprintf(format, args);
  185. va_end(args);
  186. }
  187. else
  188. MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
  189. }
  190. volatile int test_finished = 0;
  191. char* test_topic = "async test topic";
  192. void test1_onDisconnect(void* context, MQTTAsync_successData* response)
  193. {
  194. MQTTAsync c = (MQTTAsync)context;
  195. MyLog(LOGA_DEBUG, "In onDisconnect callback %p", c);
  196. test_finished = 1;
  197. }
  198. void test1_onUnsubscribe(void* context, MQTTAsync_successData* response)
  199. {
  200. MQTTAsync c = (MQTTAsync)context;
  201. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  202. int rc;
  203. MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback %p", c);
  204. opts.onSuccess = test1_onDisconnect;
  205. opts.context = c;
  206. opts.timeout = 1000;
  207. rc = MQTTAsync_disconnect(c, &opts);
  208. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  209. }
  210. int test1_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  211. {
  212. MQTTAsync c = (MQTTAsync)context;
  213. static int message_count = 0;
  214. int rc;
  215. MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
  216. if (++message_count == 1)
  217. {
  218. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  219. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  220. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  221. pubmsg.payloadlen = 11;
  222. pubmsg.qos = 2;
  223. pubmsg.retained = 0;
  224. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  225. }
  226. else
  227. {
  228. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  229. opts.onSuccess = test1_onUnsubscribe;
  230. opts.context = c;
  231. rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
  232. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  233. }
  234. MQTTAsync_freeMessage(&message);
  235. MQTTAsync_free(topicName);
  236. return 1;
  237. }
  238. void test1_onSubscribe(void* context, MQTTAsync_successData* response)
  239. {
  240. MQTTAsync c = (MQTTAsync)context;
  241. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  242. int rc;
  243. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p granted qos %d", c, response->alt.qos);
  244. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  245. pubmsg.payloadlen = 11;
  246. pubmsg.qos = 2;
  247. pubmsg.retained = 0;
  248. rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  249. }
  250. void test1_onConnect(void* context, MQTTAsync_successData* response)
  251. {
  252. MQTTAsync c = (MQTTAsync)context;
  253. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  254. int rc;
  255. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  256. opts.onSuccess = test1_onSubscribe;
  257. opts.context = c;
  258. rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
  259. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  260. if (rc != MQTTASYNC_SUCCESS)
  261. test_finished = 1;
  262. }
  263. void test1_onConnectFailure(void* context, MQTTAsync_failureData* response)
  264. {
  265. MQTTAsync c = (MQTTAsync)context;
  266. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  267. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  268. test_finished = 1;
  269. }
  270. /*********************************************************************
  271. Test1: Basic connect, subscribe send and receive.
  272. *********************************************************************/
  273. int test1(struct Options options)
  274. {
  275. int subsqos = 2;
  276. MQTTAsync c;
  277. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  278. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  279. int rc = 0;
  280. char* test_topic = "C client test1";
  281. char* serverURIs[2] = {"tcp://localhost:1882", options.connection};
  282. failures = 0;
  283. MyLog(LOGA_INFO, "Starting test 1 - asynchronous connect");
  284. rc = MQTTAsync_create(&c, options.connection, "async_8_test1",
  285. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  286. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  287. if (rc != MQTTASYNC_SUCCESS)
  288. {
  289. MQTTAsync_destroy(&c);
  290. goto exit;
  291. }
  292. rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
  293. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  294. opts.keepAliveInterval = 20;
  295. opts.cleansession = 1;
  296. opts.username = "testuser";
  297. opts.password = "testpassword";
  298. opts.will = &wopts;
  299. opts.will->message = "will message";
  300. opts.will->qos = 1;
  301. opts.will->retained = 0;
  302. opts.will->topicName = "will topic";
  303. opts.will = NULL;
  304. opts.onSuccess = test1_onConnect;
  305. opts.onFailure = test1_onConnectFailure;
  306. opts.context = c;
  307. opts.serverURIcount = 2;
  308. opts.serverURIs = serverURIs;
  309. MyLog(LOGA_DEBUG, "Connecting");
  310. rc = MQTTAsync_connect(c, &opts);
  311. rc = 0;
  312. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  313. if (rc != MQTTASYNC_SUCCESS)
  314. goto exit;
  315. while (!test_finished)
  316. #if defined(_WIN32)
  317. Sleep(100);
  318. #else
  319. usleep(10000L);
  320. #endif
  321. MQTTAsync_destroy(&c);
  322. exit:
  323. MyLog(LOGA_INFO, "TEST1: test %s. %d tests run, %d failures.",
  324. (failures == 0) ? "passed" : "failed", tests, failures);
  325. return failures;
  326. }
  327. int test2_onFailure_called = 0;
  328. void test2_onFailure(void* context, MQTTAsync_failureData* response)
  329. {
  330. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  331. test2_onFailure_called++;
  332. test_finished = 1;
  333. }
  334. void test2_onConnect(void* context, MQTTAsync_successData* response)
  335. {
  336. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p\n", context);
  337. assert("Connect should not succeed", 0, "connect success callback was called", 0);
  338. test_finished = 1;
  339. }
  340. /*********************************************************************
  341. Test2: connect timeout
  342. *********************************************************************/
  343. int test2(struct Options options)
  344. {
  345. int subsqos = 2;
  346. MQTTAsync c;
  347. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  348. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  349. int rc = 0;
  350. char* test_topic = "C client test2";
  351. test_finished = 0;
  352. MyLog(LOGA_INFO, "Starting test 2 - connect timeout");
  353. rc = MQTTAsync_create(&c, "tcp://9.20.96.160:66", "connect timeout",
  354. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  355. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  356. if (rc != MQTTASYNC_SUCCESS)
  357. {
  358. MQTTAsync_destroy(&c);
  359. goto exit;
  360. }
  361. rc = MQTTAsync_setCallbacks(c, c, NULL, test1_messageArrived, NULL);
  362. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  363. opts.connectTimeout = 5;
  364. opts.keepAliveInterval = 20;
  365. opts.cleansession = 1;
  366. opts.username = "testuser";
  367. opts.password = "testpassword";
  368. opts.will = &wopts;
  369. opts.will->message = "will message";
  370. opts.will->qos = 1;
  371. opts.will->retained = 0;
  372. opts.will->topicName = "will topic";
  373. opts.will = NULL;
  374. opts.onSuccess = test2_onConnect;
  375. opts.onFailure = test2_onFailure;
  376. opts.context = c;
  377. MyLog(LOGA_DEBUG, "Connecting");
  378. rc = MQTTAsync_connect(c, &opts);
  379. rc = 0;
  380. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  381. if (rc != MQTTASYNC_SUCCESS)
  382. goto exit;
  383. while (!test_finished)
  384. #if defined(_WIN32)
  385. Sleep(100);
  386. #else
  387. usleep(10000L);
  388. #endif
  389. MQTTAsync_destroy(&c);
  390. exit:
  391. assert("Connect onFailure should be called once", test2_onFailure_called == 1,
  392. "connect onFailure was called %d times", test2_onFailure_called);
  393. MyLog(LOGA_INFO, "TEST2: test %s. %d tests run, %d failures.",
  394. (failures == 0) ? "passed" : "failed", tests, failures);
  395. return failures;
  396. }
  397. typedef struct
  398. {
  399. MQTTAsync c;
  400. int index;
  401. char clientid[24];
  402. char test_topic[100];
  403. int message_count;
  404. } client_data;
  405. void test3_onDisconnect(void* context, MQTTAsync_successData* response)
  406. {
  407. client_data* cd = (client_data*)context;
  408. MyLog(LOGA_DEBUG, "In onDisconnect callback for client \"%s\"", cd->clientid);
  409. test_finished++;
  410. }
  411. void test3_onPublish(void* context, MQTTAsync_successData* response)
  412. {
  413. client_data* cd = (client_data*)context;
  414. MyLog(LOGA_DEBUG, "In QoS 0 onPublish callback for client \"%s\"", cd->clientid);
  415. }
  416. void test3_onUnsubscribe(void* context, MQTTAsync_successData* response)
  417. {
  418. client_data* cd = (client_data*)context;
  419. MQTTAsync_disconnectOptions opts = MQTTAsync_disconnectOptions_initializer;
  420. int rc;
  421. MyLog(LOGA_DEBUG, "In onUnsubscribe onSuccess callback \"%s\"", cd->clientid);
  422. opts.onSuccess = test3_onDisconnect;
  423. opts.context = cd;
  424. opts.timeout = 1000;
  425. rc = MQTTAsync_disconnect(cd->c, &opts);
  426. assert("Disconnect successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  427. }
  428. int test3_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  429. {
  430. client_data* cd = (client_data*)context;
  431. int rc;
  432. MyLog(LOGA_DEBUG, "In messageArrived callback \"%s\" message count ", cd->clientid);
  433. if (++cd->message_count == 1)
  434. {
  435. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  436. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  437. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  438. pubmsg.payloadlen = 25;
  439. pubmsg.qos = 1;
  440. pubmsg.retained = 0;
  441. rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
  442. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  443. }
  444. else if (cd->message_count == 2)
  445. {
  446. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  447. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  448. pubmsg.payload = "a QoS 0 message that we can shorten to the extent that we need to payload up to 11";
  449. pubmsg.payloadlen = 29;
  450. pubmsg.qos = 0;
  451. pubmsg.retained = 0;
  452. opts.context = cd;
  453. opts.onSuccess = test3_onPublish;
  454. rc = MQTTAsync_sendMessage(cd->c, cd->test_topic, &pubmsg, &opts);
  455. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  456. }
  457. else
  458. {
  459. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  460. opts.onSuccess = test3_onUnsubscribe;
  461. opts.context = cd;
  462. rc = MQTTAsync_unsubscribe(cd->c, cd->test_topic, &opts);
  463. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  464. }
  465. MQTTAsync_freeMessage(&message);
  466. MQTTAsync_free(topicName);
  467. return 1;
  468. }
  469. void test3_onSubscribe(void* context, MQTTAsync_successData* response)
  470. {
  471. client_data* cd = (client_data*)context;
  472. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  473. int rc;
  474. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback \"%s\"", cd->clientid);
  475. pubmsg.payload = "a much longer message that we can shorten to the extent that we need to payload up to 11";
  476. pubmsg.payloadlen = 11;
  477. pubmsg.qos = 2;
  478. pubmsg.retained = 0;
  479. rc = MQTTAsync_send(cd->c, cd->test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  480. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  481. }
  482. void test3_onConnect(void* context, MQTTAsync_successData* response)
  483. {
  484. client_data* cd = (client_data*)context;
  485. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  486. int rc;
  487. MyLog(LOGA_DEBUG, "In connect onSuccess callback, \"%s\"", cd->clientid);
  488. opts.onSuccess = test3_onSubscribe;
  489. opts.context = cd;
  490. rc = MQTTAsync_subscribe(cd->c, cd->test_topic, 2, &opts);
  491. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  492. if (rc != MQTTASYNC_SUCCESS)
  493. test_finished++;
  494. }
  495. void test3_onFailure(void* context, MQTTAsync_failureData* response)
  496. {
  497. client_data* cd = (client_data*)context;
  498. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  499. assert("Should have connected", 0, "failed to connect", NULL);
  500. MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\" rc %d\n", cd->clientid, response->code);
  501. if (response->message)
  502. MyLog(LOGA_DEBUG, "In connect onFailure callback, \"%s\"\n", response->message);
  503. test_finished++;
  504. }
  505. /*********************************************************************
  506. Test3: More than one client object - simultaneous working.
  507. *********************************************************************/
  508. int test3(struct Options options)
  509. {
  510. #define TEST3_CLIENTS 10
  511. int num_clients = TEST3_CLIENTS;
  512. int subsqos = 2;
  513. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  514. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  515. int rc = 0;
  516. int i;
  517. client_data clientdata[TEST3_CLIENTS];
  518. test_finished = 0;
  519. MyLog(LOGA_INFO, "Starting test 3 - multiple connections");
  520. for (i = 0; i < num_clients; ++i)
  521. {
  522. sprintf(clientdata[i].clientid, "async_test3_num_%d", i);
  523. sprintf(clientdata[i].test_topic, "async test3 topic num %d", i);
  524. clientdata[i].index = i;
  525. clientdata[i].message_count = 0;
  526. rc = MQTTAsync_create(&(clientdata[i].c), options.connection, clientdata[i].clientid,
  527. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  528. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  529. rc = MQTTAsync_setCallbacks(clientdata[i].c, &clientdata[i], NULL, test3_messageArrived, NULL);
  530. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  531. opts.keepAliveInterval = 20;
  532. opts.cleansession = 1;
  533. opts.username = "testuser";
  534. opts.password = "testpassword";
  535. opts.will = &wopts;
  536. opts.will->message = "will message";
  537. opts.will->qos = 1;
  538. opts.will->retained = 0;
  539. opts.will->topicName = "will topic";
  540. opts.onSuccess = test3_onConnect;
  541. opts.onFailure = test3_onFailure;
  542. opts.context = &clientdata[i];
  543. MyLog(LOGA_DEBUG, "Connecting");
  544. rc = MQTTAsync_connect(clientdata[i].c, &opts);
  545. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  546. }
  547. while (test_finished < num_clients)
  548. {
  549. MyLog(LOGA_DEBUG, "num_clients %d test_finished %d\n", num_clients, test_finished);
  550. #if defined(_WIN32)
  551. Sleep(100);
  552. #else
  553. usleep(10000L);
  554. #endif
  555. }
  556. MyLog(LOGA_DEBUG, "TEST3: destroying clients");
  557. for (i = 0; i < num_clients; ++i)
  558. MQTTAsync_destroy(&clientdata[i].c);
  559. /*exit:*/
  560. MyLog(LOGA_INFO, "TEST3: test %s. %d tests run, %d failures.",
  561. (failures == 0) ? "passed" : "failed", tests, failures);
  562. return failures;
  563. }
  564. void* test4_payload = NULL;
  565. int test4_payloadlen = 0;
  566. void test4_onPublish(void* context, MQTTAsync_successData* response)
  567. {
  568. MQTTAsync c = (MQTTAsync)context;
  569. MyLog(LOGA_DEBUG, "In publish onSuccess callback, context %p", context);
  570. }
  571. int test4_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  572. {
  573. MQTTAsync c = (MQTTAsync)context;
  574. static int message_count = 0;
  575. int rc, i;
  576. MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
  577. assert("Message size correct", message->payloadlen == test4_payloadlen,
  578. "message size was %d", message->payloadlen);
  579. for (i = 0; i < options.size; ++i)
  580. {
  581. if (((char*)test4_payload)[i] != ((char*)message->payload)[i])
  582. {
  583. assert("Message contents correct", ((char*)test4_payload)[i] != ((char*)message->payload)[i],
  584. "message content was %c", ((char*)message->payload)[i]);
  585. break;
  586. }
  587. }
  588. if (++message_count == 1)
  589. {
  590. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  591. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  592. pubmsg.payload = test4_payload;
  593. pubmsg.payloadlen = test4_payloadlen;
  594. pubmsg.qos = 1;
  595. pubmsg.retained = 0;
  596. opts.onSuccess = test4_onPublish;
  597. opts.context = c;
  598. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  599. }
  600. else if (message_count == 2)
  601. {
  602. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  603. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  604. pubmsg.payload = test4_payload;
  605. pubmsg.payloadlen = test4_payloadlen;
  606. pubmsg.qos = 0;
  607. pubmsg.retained = 0;
  608. opts.onSuccess = test4_onPublish;
  609. opts.context = c;
  610. rc = MQTTAsync_sendMessage(c, test_topic, &pubmsg, &opts);
  611. }
  612. else
  613. {
  614. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  615. opts.onSuccess = test1_onUnsubscribe;
  616. opts.context = c;
  617. rc = MQTTAsync_unsubscribe(c, test_topic, &opts);
  618. assert("Unsubscribe successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  619. }
  620. MQTTAsync_freeMessage(&message);
  621. MQTTAsync_free(topicName);
  622. return 1;
  623. }
  624. void test4_onSubscribe(void* context, MQTTAsync_successData* response)
  625. {
  626. MQTTAsync c = (MQTTAsync)context;
  627. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  628. int rc, i;
  629. MyLog(LOGA_DEBUG, "In subscribe onSuccess callback %p", c);
  630. pubmsg.payload = test4_payload = malloc(options.size);
  631. pubmsg.payloadlen = test4_payloadlen = options.size;
  632. srand(33);
  633. for (i = 0; i < options.size; ++i)
  634. ((char*)pubmsg.payload)[i] = rand() % 256;
  635. pubmsg.qos = 2;
  636. pubmsg.retained = 0;
  637. rc = MQTTAsync_send(c, test_topic, pubmsg.payloadlen, pubmsg.payload, pubmsg.qos, pubmsg.retained, NULL);
  638. assert("Send successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  639. }
  640. void test4_onConnect(void* context, MQTTAsync_successData* response)
  641. {
  642. MQTTAsync c = (MQTTAsync)context;
  643. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  644. int rc;
  645. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  646. opts.onSuccess = test4_onSubscribe;
  647. opts.context = c;
  648. rc = MQTTAsync_subscribe(c, test_topic, 2, &opts);
  649. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  650. if (rc != MQTTASYNC_SUCCESS)
  651. test_finished = 1;
  652. }
  653. /*********************************************************************
  654. Test4: Send and receive big messages
  655. *********************************************************************/
  656. int test4(struct Options options)
  657. {
  658. int subsqos = 2;
  659. MQTTAsync c;
  660. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  661. MQTTAsync_willOptions wopts = MQTTAsync_willOptions_initializer;
  662. int rc = 0;
  663. char* test_topic = "C client test4";
  664. test_finished = failures = 0;
  665. MyLog(LOGA_INFO, "Starting test 4 - big messages");
  666. rc = MQTTAsync_create(&c, options.connection, "async_test_4",
  667. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  668. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  669. if (rc != MQTTASYNC_SUCCESS)
  670. {
  671. MQTTAsync_destroy(&c);
  672. goto exit;
  673. }
  674. rc = MQTTAsync_setCallbacks(c, c, NULL, test4_messageArrived, NULL);
  675. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  676. opts.keepAliveInterval = 20;
  677. opts.cleansession = 1;
  678. opts.username = "testuser";
  679. opts.password = "testpassword";
  680. opts.will = &wopts;
  681. opts.will->message = "will message";
  682. opts.will->qos = 1;
  683. opts.will->retained = 0;
  684. opts.will->topicName = "will topic";
  685. opts.will = NULL;
  686. opts.onSuccess = test4_onConnect;
  687. opts.onFailure = NULL;
  688. opts.context = c;
  689. MyLog(LOGA_DEBUG, "Connecting");
  690. rc = MQTTAsync_connect(c, &opts);
  691. rc = 0;
  692. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  693. if (rc != MQTTASYNC_SUCCESS)
  694. goto exit;
  695. while (!test_finished)
  696. #if defined(_WIN32)
  697. Sleep(100);
  698. #else
  699. usleep(1000L);
  700. #endif
  701. MQTTAsync_destroy(&c);
  702. exit:
  703. MyLog(LOGA_INFO, "TEST4: test %s. %d tests run, %d failures.",
  704. (failures == 0) ? "passed" : "failed", tests, failures);
  705. return failures;
  706. }
  707. int test5_onConnect_called = 0;
  708. int test5_onFailure_called = 0;
  709. void test5_onConnect(void* context, MQTTAsync_successData* response)
  710. {
  711. MQTTAsync c = (MQTTAsync)context;
  712. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  713. test5_onConnect_called++;
  714. test_finished = 1;
  715. }
  716. void test5_onConnectFailure(void* context, MQTTAsync_failureData* response)
  717. {
  718. MQTTAsync c = (MQTTAsync)context;
  719. MyLog(LOGA_DEBUG, "In connect onFailure callback, context %p", context);
  720. test5_onFailure_called++;
  721. test_finished = 1;
  722. }
  723. /*********************************************************************
  724. Test5a: All HA connections out of service.
  725. *********************************************************************/
  726. int test5a(struct Options options)
  727. {
  728. MQTTAsync c;
  729. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  730. int rc = 0;
  731. char* test_topic = "C client test5a";
  732. char* serverURIs[3] = {"tcp://localhost:1880", "mqtt://localhost:1881", "tcp://localhost:1882"};
  733. failures = 0;
  734. MyLog(LOGA_INFO, "Starting test 5a - All HA connections out of service");
  735. rc = MQTTAsync_create(&c, "rubbish", "all_ha_down",
  736. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  737. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  738. if (rc != MQTTASYNC_SUCCESS)
  739. {
  740. MQTTAsync_destroy(&c);
  741. goto exit;
  742. }
  743. opts.keepAliveInterval = 20;
  744. opts.cleansession = 1;
  745. opts.username = "testuser";
  746. opts.password = "testpassword";
  747. opts.onSuccess = test5_onConnect;
  748. opts.onFailure = test5_onConnectFailure;
  749. opts.context = c;
  750. opts.serverURIcount = 3;
  751. opts.serverURIs = serverURIs;
  752. MyLog(LOGA_DEBUG, "Connecting");
  753. rc = MQTTAsync_connect(c, &opts);
  754. rc = 0;
  755. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  756. if (rc != MQTTASYNC_SUCCESS)
  757. goto exit;
  758. while (!test_finished)
  759. #if defined(_WIN32)
  760. Sleep(100);
  761. #else
  762. usleep(10000L);
  763. #endif
  764. MQTTAsync_destroy(&c);
  765. exit:
  766. assert("Connect onFailure should be called once", test5_onFailure_called == 1,
  767. "connect onFailure was called %d times", test5_onFailure_called);
  768. MyLog(LOGA_INFO, "TEST5a: test %s. %d tests run, %d failures.",
  769. (failures == 0) ? "passed" : "failed", tests, failures);
  770. return failures;
  771. }
  772. /*********************************************************************
  773. Test5b: All HA connections out of service except the last one.
  774. *********************************************************************/
  775. int test5b(struct Options options)
  776. {
  777. MQTTAsync c;
  778. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  779. int rc = 0;
  780. char* test_topic = "C client test5b";
  781. char* serverURIs[3] = {"tcp://localhost:1880", "mqtt://localhost:1881", options.connection};
  782. failures = 0;
  783. MyLog(LOGA_INFO, "Starting test 5b - All HA connections out of service except the last one");
  784. rc = MQTTAsync_create(&c, "rubbish", "all_ha_down_except_last_one",
  785. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  786. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  787. if (rc != MQTTASYNC_SUCCESS)
  788. {
  789. MQTTAsync_destroy(&c);
  790. goto exit;
  791. }
  792. opts.keepAliveInterval = 20;
  793. opts.cleansession = 1;
  794. opts.username = "testuser";
  795. opts.password = "testpassword";
  796. opts.onSuccess = test5_onConnect;
  797. opts.onFailure = test5_onConnectFailure;
  798. opts.context = c;
  799. opts.serverURIcount = 3;
  800. opts.serverURIs = serverURIs;
  801. MyLog(LOGA_DEBUG, "Connecting");
  802. rc = MQTTAsync_connect(c, &opts);
  803. rc = 0;
  804. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  805. if (rc != MQTTASYNC_SUCCESS)
  806. goto exit;
  807. while (!test_finished)
  808. #if defined(_WIN32)
  809. Sleep(100);
  810. #else
  811. usleep(10000L);
  812. #endif
  813. MQTTAsync_destroy(&c);
  814. exit:
  815. assert("Connect onConnect should be called once", test5_onConnect_called == 1,
  816. "connect onConnect was called %d times", test5_onConnect_called);
  817. MyLog(LOGA_INFO, "TEST5b: test %s. %d tests run, %d failures.",
  818. (failures == 0) ? "passed" : "failed", tests, failures);
  819. return failures;
  820. }
  821. /*********************************************************************
  822. Test5c: All HA connections out of service except the first one.
  823. *********************************************************************/
  824. int test5c(struct Options options)
  825. {
  826. MQTTAsync c;
  827. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  828. int rc = 0;
  829. char* test_topic = "C client test5c";
  830. char* serverURIs[3] = {options.connection, "tcp://localhost:1881", "tcp://localhost:1882"};
  831. failures = 0;
  832. MyLog(LOGA_INFO, "Starting test 5c - All HA connections out of service except the first one");
  833. rc = MQTTAsync_create(&c, "rubbish", "all_ha_down_except_first_one",
  834. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  835. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  836. if (rc != MQTTASYNC_SUCCESS)
  837. {
  838. MQTTAsync_destroy(&c);
  839. goto exit;
  840. }
  841. opts.keepAliveInterval = 20;
  842. opts.cleansession = 1;
  843. opts.username = "testuser";
  844. opts.password = "testpassword";
  845. opts.onSuccess = test5_onConnect;
  846. opts.onFailure = test5_onConnectFailure;
  847. opts.context = c;
  848. opts.serverURIcount = 3;
  849. opts.serverURIs = serverURIs;
  850. MyLog(LOGA_DEBUG, "Connecting");
  851. rc = MQTTAsync_connect(c, &opts);
  852. rc = 0;
  853. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  854. if (rc != MQTTASYNC_SUCCESS)
  855. goto exit;
  856. while (!test_finished)
  857. #if defined(_WIN32)
  858. Sleep(100);
  859. #else
  860. usleep(10000L);
  861. #endif
  862. MQTTAsync_destroy(&c);
  863. exit:
  864. assert("Connect onConnect should be called once", test5_onConnect_called == 1,
  865. "connect onConnect was called %d times", test5_onConnect_called);
  866. MyLog(LOGA_INFO, "TEST5c: test %s. %d tests run, %d failures.",
  867. (failures == 0) ? "passed" : "failed", tests, failures);
  868. return failures;
  869. }
  870. /*********************************************************************
  871. Test6: Acks blocked due to sending messages
  872. *********************************************************************/
  873. char* test6_topic = "test6_topic";
  874. char* test6_payload = NULL;
  875. int test6_connected = 0;
  876. int test6_payloadlen = 0;
  877. int test6_message_count = 0;
  878. int test6_disconnected = 0;
  879. void test6_onDisconnect(void* context, MQTTAsync_successData* response)
  880. {
  881. MyLog(LOGA_DEBUG, "In onDisconnect callback");
  882. test6_disconnected = 1;
  883. }
  884. int test6_messageArrived(void* context, char* topicName, int topicLen, MQTTAsync_message* message)
  885. {
  886. MQTTAsync c = (MQTTAsync)context;
  887. MyLog(LOGA_DEBUG, "In messageArrived callback %p", c);
  888. assert("Message size correct", message->payloadlen == test6_payloadlen,
  889. "message size was %d", message->payloadlen);
  890. MQTTAsync_freeMessage(&message);
  891. MQTTAsync_free(topicName);
  892. return 1;
  893. }
  894. void test6_onPublishFailure(void* context, MQTTAsync_failureData* response)
  895. {
  896. MyLog(LOGA_INFO, "In publish onFailure callback, context %p", context);
  897. failures++;
  898. test_finished = 1;
  899. }
  900. void test6_onPublish(void* context, MQTTAsync_successData* response)
  901. {
  902. MQTTAsync c = (MQTTAsync)context;
  903. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  904. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  905. int rc;
  906. static int publish_count = 0;
  907. if (++publish_count > 100)
  908. {
  909. test_finished = 1;
  910. goto exit;
  911. }
  912. MyLog(LOGA_INFO, "In publish onSuccess callback, count %d", publish_count);
  913. if (test6_payload == NULL) {
  914. test6_payload = malloc(options.size);
  915. memset(test6_payload, ' ', options.size);
  916. }
  917. MyLog(LOGA_DEBUG, "In publish onSuccess callback, context %p", context);
  918. pubmsg.payload = test6_payload;
  919. pubmsg.payloadlen = options.size;
  920. pubmsg.qos = 0;
  921. pubmsg.retained = 0;
  922. opts.onSuccess = test6_onPublish;
  923. opts.onFailure = test6_onPublishFailure;
  924. opts.context = c;
  925. MyLog(LOGA_INFO, "Calling sendMessage, count %d", publish_count);
  926. rc = MQTTAsync_sendMessage(c, "test6_big_messages", &pubmsg, &opts);
  927. MyLog(LOGA_INFO, "Called sendMessage, count %d rc %d", publish_count, rc);
  928. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  929. exit:
  930. MyLog(LOGA_DEBUG, "Leaving publish onSuccess callback, count %d, context %p", publish_count, context);
  931. }
  932. void test6_onSubscribeFailure(void* context, MQTTAsync_failureData* response)
  933. {
  934. MyLog(LOGA_INFO, "In subscribe onFailure callback, context %p", context);
  935. failures++;
  936. test_finished = 1;
  937. }
  938. void test6_onSubscribe(void* context, MQTTAsync_successData* response)
  939. {
  940. MQTTAsync c = (MQTTAsync)context;
  941. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  942. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  943. int rc;
  944. if (test6_payload == NULL) {
  945. test6_payload = malloc(options.size);
  946. memset(test6_payload, ' ', options.size);
  947. }
  948. MyLog(LOGA_INFO, "In subscribe onSuccess callback, context %p", context);
  949. pubmsg.payload = test6_payload;
  950. pubmsg.payloadlen = options.size;
  951. pubmsg.qos = 0;
  952. pubmsg.retained = 0;
  953. opts.onSuccess = test6_onPublish;
  954. opts.onFailure = test6_onPublishFailure;
  955. opts.context = c;
  956. rc = MQTTAsync_sendMessage(c, "test6_big_messages", &pubmsg, &opts);
  957. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  958. }
  959. void test6_onConnectFailure(void* context, MQTTAsync_failureData* response)
  960. {
  961. MyLog(LOGA_INFO, "In connect onFailure callback, context %p", context);
  962. failures++;
  963. test_finished = 1;
  964. }
  965. void test6_onConnect(void* context, MQTTAsync_successData* response)
  966. {
  967. MQTTAsync c = (MQTTAsync)context;
  968. MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;
  969. int rc;
  970. MyLog(LOGA_INFO, "In connect onSuccess callback, context %p", context);
  971. opts.onSuccess = test6_onSubscribe;
  972. opts.onFailure = test6_onSubscribeFailure;
  973. opts.context = c;
  974. rc = MQTTAsync_subscribe(c, test6_topic, 2, &opts);
  975. assert("Good rc from subscribe", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  976. if (rc != MQTTASYNC_SUCCESS)
  977. test_finished = 1;
  978. }
  979. void test6_onConnected(void* context, MQTTAsync_successData* response)
  980. {
  981. MQTTAsync d = (MQTTAsync)context;
  982. MyLog(LOGA_DEBUG, "In connect onSuccess callback, context %p", context);
  983. test6_connected = 1;
  984. }
  985. void test6_connectionLost(void* context, char* cause) {
  986. MyLog(LOGA_INFO, "Connection lost");
  987. test_finished = 1;
  988. }
  989. int test6(struct Options options)
  990. {
  991. MQTTAsync c, d;
  992. MQTTAsync_connectOptions opts = MQTTAsync_connectOptions_initializer;
  993. MQTTAsync_disconnectOptions dopts = MQTTAsync_disconnectOptions_initializer;
  994. MQTTAsync_message pubmsg = MQTTAsync_message_initializer;
  995. int rc = 0;
  996. char* test_topic = "C client test8 - test6";
  997. int messages_sent = 0;
  998. int count = 0;
  999. failures = 0;
  1000. test_finished = 0;
  1001. MyLog(LOGA_INFO, "Starting test 6 - acks blocked due to sending messages");
  1002. global_start_time = start_clock();
  1003. rc = MQTTAsync_create(&c, options.connection, "acks blocked",
  1004. MQTTCLIENT_PERSISTENCE_DEFAULT, NULL);
  1005. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1006. if (rc != MQTTASYNC_SUCCESS)
  1007. {
  1008. MQTTAsync_destroy(&c);
  1009. goto exit;
  1010. }
  1011. rc = MQTTAsync_setCallbacks(c, c, test6_connectionLost, test6_messageArrived, NULL);
  1012. assert("Good rc from setCallbacks", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1013. opts.keepAliveInterval = 20;
  1014. opts.cleansession = 1;
  1015. opts.username = "testuser";
  1016. opts.password = "testpassword";
  1017. opts.onSuccess = test6_onConnect;
  1018. opts.onFailure = test6_onConnectFailure;
  1019. opts.context = c;
  1020. MyLog(LOGA_DEBUG, "Connecting");
  1021. rc = MQTTAsync_connect(c, &opts);
  1022. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1023. if (rc != MQTTASYNC_SUCCESS)
  1024. goto exit;
  1025. rc = MQTTAsync_create(&d, options.connection, "acks blocked - send loop",
  1026. MQTTCLIENT_PERSISTENCE_NONE, NULL);
  1027. assert("good rc from create", rc == MQTTASYNC_SUCCESS, "rc was %d\n", rc);
  1028. if (rc != MQTTASYNC_SUCCESS)
  1029. {
  1030. MQTTAsync_destroy(&d);
  1031. goto exit;
  1032. }
  1033. opts.onSuccess = test6_onConnected;
  1034. opts.context = d;
  1035. MyLog(LOGA_DEBUG, "Connecting");
  1036. rc = MQTTAsync_connect(d, &opts);
  1037. assert("Good rc from connect", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1038. if (rc != MQTTASYNC_SUCCESS)
  1039. goto exit;
  1040. while (test6_connected == 0 && test_finished == 0 && failures == 0)
  1041. {
  1042. #if defined(_WIN32)
  1043. Sleep(100);
  1044. #else
  1045. usleep(10000L);
  1046. #endif
  1047. }
  1048. pubmsg.payload = "small payload in loop";
  1049. pubmsg.payloadlen = test6_payloadlen = strlen(pubmsg.payload)+1;
  1050. pubmsg.qos = 1;
  1051. pubmsg.retained = 0;
  1052. while (test_finished == 0 && failures == 0)
  1053. {
  1054. rc = MQTTAsync_sendMessage(d, test6_topic, &pubmsg, NULL);
  1055. assert("Good rc from publish", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1056. if (rc != MQTTASYNC_SUCCESS)
  1057. break;
  1058. messages_sent++;
  1059. #if defined(_WIN32)
  1060. Sleep(1000);
  1061. #else
  1062. usleep(100000L);
  1063. #endif
  1064. }
  1065. dopts.onSuccess = test6_onDisconnect;
  1066. dopts.timeout = 1000;
  1067. dopts.context = d;
  1068. test6_disconnected = 0;
  1069. rc = MQTTAsync_disconnect(d, &dopts);
  1070. assert("Disconnect start successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1071. while (test6_disconnected == 0 && ++count < 5)
  1072. {
  1073. #if defined(_WIN32)
  1074. Sleep(1000);
  1075. #else
  1076. usleep(100000L);
  1077. #endif
  1078. }
  1079. dopts.context = c;
  1080. test6_disconnected = 0;
  1081. rc = MQTTAsync_disconnect(c, &dopts);
  1082. assert("Disconnect start successful", rc == MQTTASYNC_SUCCESS, "rc was %d", rc);
  1083. count = 0;
  1084. while (test6_disconnected == 0 && ++count < 5)
  1085. {
  1086. #if defined(_WIN32)
  1087. Sleep(1000);
  1088. #else
  1089. usleep(100000L);
  1090. #endif
  1091. }
  1092. MQTTAsync_destroy(&c);
  1093. MQTTAsync_destroy(&d);
  1094. exit:
  1095. if (test6_payload)
  1096. free(test6_payload);
  1097. MyLog(LOGA_INFO, "TEST6: test %s. %d tests run, %d failures.",
  1098. (failures == 0) ? "passed" : "failed", tests, failures);
  1099. return failures;
  1100. }
  1101. void trace_callback(enum MQTTASYNC_TRACE_LEVELS level, char* message)
  1102. {
  1103. //if ((strstr(message, "onnect") && !strstr(message, "isconnect")) || level == MQTTASYNC_TRACE_ERROR)
  1104. printf("Trace : %d, %s\n", level, message);
  1105. }
  1106. int main(int argc, char** argv)
  1107. {
  1108. int rc = 0;
  1109. int (*tests[])() = {NULL, test1, test2, test3, test4, test5a, test5b, test5c, test6}; /* indexed starting from 1 */
  1110. MQTTAsync_nameValue* info;
  1111. getopts(argc, argv);
  1112. MQTTAsync_setTraceCallback(trace_callback);
  1113. info = MQTTAsync_getVersionInfo();
  1114. while (info->name)
  1115. {
  1116. MyLog(LOGA_INFO, "%s: %s", info->name, info->value);
  1117. info++;
  1118. }
  1119. if (options.test_no == -1)
  1120. { /* run all the tests */
  1121. for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
  1122. {
  1123. failures = 0;
  1124. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1125. rc += tests[options.test_no](options); /* return number of failures. 0 = test succeeded */
  1126. }
  1127. }
  1128. else
  1129. {
  1130. MQTTAsync_setTraceLevel(MQTTASYNC_TRACE_ERROR);
  1131. rc = tests[options.test_no](options); /* run just the selected test */
  1132. }
  1133. if (failures == 0)
  1134. MyLog(LOGA_INFO, "verdict pass");
  1135. else
  1136. MyLog(LOGA_INFO, "verdict fail");
  1137. return rc;
  1138. }