28 #if !defined(_WINDOWS) 30 #include <sys/socket.h> 35 #define setenv(a, b, c) _putenv_s(a, b) 38 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 63 "tcp://mqtt.eclipse.org:1883",
65 "tcp://localhost:1883",
84 if (strcmp(argv[count],
"--test_no") == 0)
91 else if (strcmp(argv[count],
"--connection") == 0)
101 else if (strcmp(argv[count],
"--haconnections") == 0)
105 char* tok = strtok(argv[count],
" ");
113 tok = strtok(NULL,
" ");
119 else if (strcmp(argv[count],
"--proxy_connection") == 0)
126 else if (strcmp(argv[count],
"--client_id") == 0)
133 else if (strcmp(argv[count],
"--username") == 0)
140 else if (strcmp(argv[count],
"--password") == 0)
147 else if (strcmp(argv[count],
"--log") == 0)
154 else if (strcmp(argv[count],
"--MQTTversion") == 0)
164 else if (strcmp(argv[count],
"--iterations") == 0)
171 else if (strcmp(argv[count],
"--reconnection_period") == 0)
178 else if (strcmp(argv[count],
"--verbose") == 0)
181 printf(
"\nSetting verbose on\n");
192 #include <sys/timeb.h> 197 #if defined(_WIN32) || defined(_WINDOWS) 207 #if defined(_WIN32) || defined(_WINDOWS) 211 gettimeofday(&ts, NULL);
212 localtime_r(&ts.tv_sec, &timeinfo);
214 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", &timeinfo);
216 #if defined(_WIN32) || defined(_WINDOWS) 217 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
219 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
222 va_start(args, format);
223 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
226 printf(
"%s\n", msg_buf);
231 #if defined(_WIN32) || defined(_WINDOWS) 232 #define mqsleep(A) Sleep(1000*A) 233 #define START_TIME_TYPE DWORD 234 static DWORD start_time = 0;
237 return GetTickCount();
240 #define mqsleep sleep 241 #define START_TIME_TYPE struct timespec 244 static struct timespec start;
245 clock_gettime(CLOCK_REALTIME, &start);
249 #define mqsleep sleep 250 #define START_TIME_TYPE struct timeval 254 struct timeval start_time;
255 gettimeofday(&start_time, NULL);
264 return GetTickCount() - start_time;
268 long elapsed(
struct timespec start)
270 struct timespec now, res;
272 clock_gettime(CLOCK_REALTIME, &now);
273 ntimersub(now, start, res);
274 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
279 struct timeval now, res;
281 gettimeofday(&now, NULL);
282 timersub(&now, &start_time, &res);
283 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
288 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 289 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 303 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
309 fprintf(
xml,
"</testcase>\n");
321 MyLog(
LOGA_INFO,
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
323 va_start(args, format);
324 vprintf(format, args);
328 description, filename, lineno);
331 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
337 Sleep(1000L*seconds);
352 printf(
"Callback: connection lost\n");
357 printf(
"Callback: publish complete for token %d\n", token);
363 printf(
"Callback: message received on topic '%s' is '%.*s'.\n", topicName, m->
payloadlen, (
char*)(m->
payload));
371 char* testname =
"test1";
376 MyLog(
LOGA_INFO,
"Starting test 1 - clean session and reconnect with session present");
377 fprintf(
xml,
"<testcase classname=\"test1\" name=\"connectionLost and will messages\"");
463 int main(
int argc,
char** argv)
470 xml = fopen(
"TEST-test1.xml",
"w");
471 fprintf(
xml,
"<testsuite name=\"test1\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests) - 1));
477 setenv(
"MQTT_C_CLIENT_TRACE",
"ON", 1);
478 setenv(
"MQTT_C_CLIENT_TRACE_LEVEL",
options.
log, 1);
500 fprintf(
xml,
"</testsuite>\n");
void mysleep(int seconds)
enum MQTTPropertyCodes value
int test1_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
#define MQTTCLIENT_SUCCESS
struct MQTTClient_connectOptions::@57 returned
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
int MQTTClient_disconnect(MQTTClient handle, int timeout)
START_TIME_TYPE start_clock(void)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
void write_test_result(void)
START_TIME_TYPE global_start_time
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
#define assert(a, b, c, d)
#define MQTTVERSION_3_1_1
void MyLog(int LOGA_level, char *format,...)
void test1_connectionLost(void *context, char *cause)
void MQTTClient_freeMessage(MQTTClient_message **message)
void MQTTClient_destroy(MQTTClient *handle)
int test1(struct Options options)
#define MQTTCLIENT_PERSISTENCE_DEFAULT
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
void MQTTClient_free(void *memory)
#define MQTTClient_connectOptions_initializer
int MQTTClient_deliveryToken
long elapsed(START_TIME_TYPE start_time)
int main(int argc, char **argv)
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
void getopts(int argc, char **argv)
#define MQTTVERSION_DEFAULT
void test1_deliveryComplete(void *context, MQTTClient_deliveryToken token)