28 #if !defined(_WINDOWS) 30 #include <sys/socket.h> 36 #define setenv(a, b, c) _putenv_s(a, b) 39 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) 58 "tcp://m2m.eclipse.org:1883",
73 if (strcmp(argv[count],
"--test_no") == 0)
80 else if (strcmp(argv[count],
"--connection") == 0)
90 else if (strcmp(argv[count],
"--haconnections") == 0)
94 char* tok = strtok(argv[count],
" ");
102 tok = strtok(NULL,
" ");
108 else if (strcmp(argv[count],
"--MQTTversion") == 0)
118 else if (strcmp(argv[count],
"--iterations") == 0)
125 else if (strcmp(argv[count],
"--verbose") == 0)
128 printf(
"\nSetting verbose on\n");
139 #include <sys/timeb.h> 144 #if defined(_WIN32) || defined(_WINDOWS) 154 #if defined(_WIN32) || defined(_WINDOWS) 158 gettimeofday(&ts, NULL);
159 localtime_r(&ts.tv_sec, &timeinfo);
161 strftime(msg_buf, 80,
"%Y%m%d %H%M%S", &timeinfo);
163 #if defined(_WIN32) || defined(_WINDOWS) 164 sprintf(&msg_buf[strlen(msg_buf)],
".%.3hu ", ts.millitm);
166 sprintf(&msg_buf[strlen(msg_buf)],
".%.3lu ", ts.tv_usec / 1000);
169 va_start(args, format);
170 vsnprintf(&msg_buf[strlen(msg_buf)],
sizeof(msg_buf) - strlen(msg_buf), format, args);
173 printf(
"%s\n", msg_buf);
178 #if defined(_WIN32) || defined(_WINDOWS) 179 #define mqsleep(A) Sleep(1000*A) 180 #define START_TIME_TYPE DWORD 181 static DWORD start_time = 0;
184 return GetTickCount();
187 #define mqsleep sleep 188 #define START_TIME_TYPE struct timespec 191 static struct timespec start;
192 clock_gettime(CLOCK_REALTIME, &start);
196 #define mqsleep sleep 197 #define START_TIME_TYPE struct timeval 201 struct timeval start_time;
202 gettimeofday(&start_time, NULL);
211 return GetTickCount() - start_time;
215 long elapsed(
struct timespec start)
217 struct timespec now, res;
219 clock_gettime(CLOCK_REALTIME, &now);
220 ntimersub(now, start, res);
221 return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
226 struct timeval now, res;
228 gettimeofday(&now, NULL);
229 timersub(&now, &start_time, &res);
230 return (res.tv_sec)*1000 + (res.tv_usec)/1000;
235 #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d) 236 #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e) 250 fprintf(
xml,
" time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
256 fprintf(
xml,
"</testcase>\n");
268 MyLog(
LOGA_INFO,
"Assertion failed, file %s, line %d, description: %s\n", filename, lineno, description);
270 va_start(args, format);
271 vprintf(format, args);
275 description, filename, lineno);
278 MyLog(
LOGA_DEBUG,
"Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
282 #if defined(_WIN32) || defined(_WIN64) 328 MyLog(
LOGA_DEBUG,
"messageArrived: %d message received on topic %s is %.*s.",
356 int wait_seconds = 30;
399 assert(
"Message Arrived", wait_seconds > 0,
400 "Timed out waiting for message %d\n", i);
403 #if defined(_WINDOWS) 419 fprintf(
xml,
"<testcase classname=\"test1\" name=\"multiple threads using same client object\"");
422 MyLog(
LOGA_INFO,
"Starting test 1 - multiple threads using same client object");
460 test1_pubmsg_check.
payload =
"a much longer message that we can shorten to the extent that we need to";
478 int wait_seconds = 90;
536 MyLog(
LOGA_DEBUG,
"Callback: %d message received on topic %s is %.*s.",
556 int wait_seconds = 0;
561 test2_pubmsg.
payload =
"a much longer message that we can shorten to the extent that we need to";
591 assert(
"Message Arrived", wait_seconds > 0,
592 "Time out waiting for message %d\n", i );
611 "Number of deliveryCompleted callbacks was %d\n",
619 char* testname =
"test2";
627 fprintf(
xml,
"<testcase classname=\"test1\" name=\"multi-threaded client using callbacks\"");
628 MyLog(
LOGA_INFO,
"Starting test 2 - multi-threaded client using callbacks");
676 int main(
int argc,
char** argv)
682 #if defined(_WIN32) || defined(_WIN64) 686 xml = fopen(
"TEST-test2.xml",
"w");
687 fprintf(
xml,
"<testsuite name=\"test1\" tests=\"%d\">\n", (
int)(
ARRAY_SIZE(
tests) - 1));
689 setenv(
"MQTT_C_CLIENT_TRACE",
"ON", 1);
690 setenv(
"MQTT_C_CLIENT_TRACE_LEVEL",
"ERROR", 1);
710 fprintf(
xml,
"</testsuite>\n");
int main(int argc, char **argv)
enum MQTTPropertyCodes value
volatile int test2_deliveryCompleted
pthread_mutex_t deliveryCompleted_mutex_store
MQTTClient_message test2_pubmsg
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
#define MQTTCLIENT_SUCCESS
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *deliveryToken)
int MQTTClient_disconnect(MQTTClient handle, int timeout)
void getopts(int argc, char **argv)
int Thread_lock_mutex(mutex_type mutex)
size_t strftime(char *str, size_t count, const char *format, const std::tm *time)
void lock_mutex(mutex_type amutex)
thread_return_type WINAPI test1_sendAndReceive(void *n)
long elapsed(START_TIME_TYPE start_time)
#define MQTTClient_message_initializer
volatile int test1_arrivedcount_qos[3]
int test2_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
void test2_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
#define MQTTClient_willOptions_initializer
void myassert(char *filename, int lineno, char *description, int value, char *format,...)
void test1_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
void test2_sendAndReceive(MQTTClient *c, int qos, char *test_topic)
void MyLog(int LOGA_level, char *format,...)
int MQTTClient_publishMessage(MQTTClient handle, const char *topicName, MQTTClient_message *message, MQTTClient_deliveryToken *deliveryToken)
void unlock_mutex(mutex_type amutex)
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
int Thread_unlock_mutex(mutex_type mutex)
mutex_type deliveryCompleted_mutex
void MQTTClient_freeMessage(MQTTClient_message **message)
void write_test_result(void)
#define assert(a, b, c, d)
START_TIME_TYPE global_start_time
void MQTTClient_destroy(MQTTClient *handle)
volatile int test2_arrivedcount
int test1(struct Options options)
volatile int test1_arrivedcount
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
MQTTClient_message test1_pubmsg_check
#define thread_return_type
thread_id_type Thread_getid(void)
void MQTTClient_free(void *memory)
#define MQTTCLIENT_PERSISTENCE_NONE
#define MQTTClient_connectOptions_initializer
int MQTTClient_deliveryToken
START_TIME_TYPE start_clock(void)
thread_type Thread_start(thread_fn fn, void *parameter)
int test1_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *m)
volatile int test1_deliveryCompleted
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
int test2(struct Options options)
#define MQTTVERSION_DEFAULT
MQTTClient_willOptions * will