123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538 |
- #include "Thread.h"
- #include <string.h>
- #include <stdlib.h>
- #if !defined(_WINDOWS)
- #include <sys/time.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <errno.h>
- #define WINAPI
- #else
- #include <windows.h>
- #endif
- #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
- void usage(void)
- {
- printf("help!!\n");
- exit(EXIT_FAILURE);
- }
- struct Options
- {
- int verbose;
- int test_no;
- int iterations;
- } options =
- {
- 0,
- -1,
- 1,
- };
- void getopts(int argc, char** argv)
- {
- int count = 1;
- while (count < argc)
- {
- if (strcmp(argv[count], "--test_no") == 0)
- {
- if (++count < argc)
- options.test_no = atoi(argv[count]);
- else
- usage();
- }
- else if (strcmp(argv[count], "--iterations") == 0)
- {
- if (++count < argc)
- options.iterations = atoi(argv[count]);
- else
- usage();
- }
- else if (strcmp(argv[count], "--verbose") == 0)
- options.verbose = 1;
- count++;
- }
- }
- #define LOGA_DEBUG 0
- #define LOGA_INFO 1
- #include <stdarg.h>
- #include <time.h>
- #include <sys/timeb.h>
- void MyLog(int LOGA_level, char* format, ...)
- {
- static char msg_buf[256];
- va_list args;
- #if defined(_WIN32) || defined(_WINDOWS)
- struct timeb ts;
- #else
- struct timeval ts;
- #endif
- struct tm *timeinfo;
- if (LOGA_level == LOGA_DEBUG && options.verbose == 0)
- return;
- #if defined(_WIN32) || defined(_WINDOWS)
- ftime(&ts);
- timeinfo = localtime(&ts.time);
- #else
- gettimeofday(&ts, NULL);
- timeinfo = localtime(&ts.tv_sec);
- #endif
- strftime(msg_buf, 80, "%Y%m%d %H%M%S", timeinfo);
- #if defined(_WIN32) || defined(_WINDOWS)
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3hu ", ts.millitm);
- #else
- sprintf(&msg_buf[strlen(msg_buf)], ".%.3lu ", ts.tv_usec / 1000L);
- #endif
- va_start(args, format);
- vsnprintf(&msg_buf[strlen(msg_buf)], sizeof(msg_buf) - strlen(msg_buf), format, args);
- va_end(args);
- printf("%s\n", msg_buf);
- fflush(stdout);
- }
- #if defined(_WIN32) || defined(_WINDOWS)
- #define mysleep(A) Sleep(1000*A)
- #define START_TIME_TYPE DWORD
- static DWORD start_time = 0;
- START_TIME_TYPE start_clock(void)
- {
- return GetTickCount();
- }
- #elif defined(AIX)
- #define mysleep sleep
- #define START_TIME_TYPE struct timespec
- START_TIME_TYPE start_clock(void)
- {
- static struct timespec start;
- clock_gettime(CLOCK_REALTIME, &start);
- return start;
- }
- #else
- #define mysleep sleep
- #define START_TIME_TYPE struct timeval
- START_TIME_TYPE start_clock(void)
- {
- struct timeval start_time;
- gettimeofday(&start_time, NULL);
- return start_time;
- }
- #endif
- #if defined(_WIN32)
- long elapsed(START_TIME_TYPE start_time)
- {
- return GetTickCount() - start_time;
- }
- #elif defined(AIX)
- #define assert(a)
- long elapsed(struct timespec start)
- {
- struct timespec now, res;
- clock_gettime(CLOCK_REALTIME, &now);
- ntimersub(now, start, res);
- return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
- }
- #else
- long elapsed(START_TIME_TYPE start_time)
- {
- struct timeval now, res;
- gettimeofday(&now, NULL);
- timersub(&now, &start_time, &res);
- return (res.tv_sec)*1000 + (res.tv_usec)/1000;
- }
- #endif
- #define assert(a, b, c, d) myassert(__FILE__, __LINE__, a, b, c, d)
- #define assert1(a, b, c, d, e) myassert(__FILE__, __LINE__, a, b, c, d, e)
- int tests = 0;
- int failures = 0;
- FILE* xml;
- START_TIME_TYPE global_start_time;
- char output[3000];
- char* cur_output = output;
- void write_test_result(void)
- {
- long duration = elapsed(global_start_time);
- fprintf(xml, " time=\"%ld.%.3ld\" >\n", duration / 1000, duration % 1000);
- if (cur_output != output)
- {
- fprintf(xml, "%s", output);
- cur_output = output;
- }
- fprintf(xml, "</testcase>\n");
- }
- void myassert(char* filename, int lineno, char* description, int value, char* format, ...)
- {
- ++tests;
- if (!value)
- {
- va_list args;
- ++failures;
- printf("Assertion failed, file %s, line %d, description: %s, ", filename, lineno, description);
- va_start(args, format);
- vprintf(format, args);
- va_end(args);
- printf("\n");
- cur_output += sprintf(cur_output, "<failure type=\"%s\">file %s, line %d </failure>\n",
- description, filename, lineno);
- }
- else
- MyLog(LOGA_DEBUG, "Assertion succeeded, file %s, line %d, description: %s", filename, lineno, description);
- }
- static thread_return_type WINAPI sem_secondary(void* n)
- {
- int rc = 0;
- sem_type sem = n;
- START_TIME_TYPE start;
- long duration;
- MyLog(LOGA_DEBUG, "Secondary semaphore pointer %p", sem);
- rc = Thread_check_sem(sem);
- assert("rc 0 from check_sem", rc == 0, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Secondary thread about to wait");
- start = start_clock();
- rc = Thread_wait_sem(sem, 99999);
- duration = elapsed(start);
- assert("rc 0 from lock mutex", rc == 0, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is 2s", duration >= 2000L, "duration was %ld", duration);
- MyLog(LOGA_DEBUG, "Secondary thread ending");
- return 0;
- }
- int test_sem(struct Options options)
- {
- char* testname = "test_sem";
- int rc = 0, i = 0;
- START_TIME_TYPE start;
- long duration;
- sem_type sem = Thread_create_sem(&rc);
- MyLog(LOGA_INFO, "Starting semaphore test");
- fprintf(xml, "<testcase classname=\"test\" name=\"%s\"", testname);
- global_start_time = start_clock();
- MyLog(LOGA_DEBUG, "Primary semaphore pointer %p\n", sem);
-
- rc = Thread_check_sem(sem);
- assert("rc 0 from check_sem", rc == 0, "rc was %d\n", rc);
- MyLog(LOGA_DEBUG, "Post semaphore so then check should be 1\n");
- rc = Thread_post_sem(sem);
- assert("rc 0 from post_sem", rc == 0, "rc was %d\n", rc);
-
- rc = Thread_check_sem(sem);
- assert("rc 1 from check_sem", rc == 1, "rc was %d", rc);
-
- rc = Thread_check_sem(sem);
- assert("rc 0 from check_sem", rc == 0, "rc was %d", rc);
-
- for (i = 0; i < 10; ++i)
- {
- rc = Thread_post_sem(sem);
- assert("rc 0 from post_sem", rc == 0, "rc was %d\n", rc);
- }
- for (i = 0; i < 10; ++i)
- {
- rc = Thread_check_sem(sem);
- assert("rc 1 from check_sem", rc == 1, "rc was %d", rc);
- }
- rc = Thread_check_sem(sem);
- assert("rc 0 from check_sem", rc == 0, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Check timeout");
- start = start_clock();
- rc = Thread_wait_sem(sem, 1500);
- duration = elapsed(start);
- assert("rc ETIMEDOUT from lock mutex", rc == ETIMEDOUT, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is 2s", duration >= 1500L, "duration was %ld", duration);
- MyLog(LOGA_DEBUG, "Starting secondary thread");
- Thread_start(sem_secondary, (void*)sem);
- mysleep(2);
- MyLog(LOGA_DEBUG, "post secondary");
- rc = Thread_post_sem(sem);
- assert("rc 1 from post_sem", rc == 1, "rc was %d", rc);
- mysleep(1);
- MyLog(LOGA_DEBUG, "Main thread ending");
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- #if !defined(_WIN32) && !defined(_WIN64)
- thread_return_type cond_secondary(void* n)
- {
- int rc = 0;
- cond_type cond = n;
- START_TIME_TYPE start;
- long duration;
- MyLog(LOGA_DEBUG, "This will time out");
- start = start_clock();
- rc = Thread_wait_cond(cond, 1);
- duration = elapsed(start);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is about 1s", duration >= 1000L && duration <= 1050L, "duration was %ld", duration);
- assert("rc non 0 from wait_cond", rc == ETIMEDOUT, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "This should hang around a few seconds");
- start = start_clock();
- rc = Thread_wait_cond(cond, 99999);
- duration = elapsed(start);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is around 1s", duration >= 990L && duration <= 1010L, "duration was %ld", duration);
- assert("rc 9 from wait_cond", rc == 0, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Secondary cond thread ending");
- return 0;
- }
- int test_cond(struct Options options)
- {
- char* testname = "test_cond";
- int rc = 0, i = 0;
- START_TIME_TYPE start;
- long duration;
- cond_type cond = Thread_create_cond(&rc);
- MyLog(LOGA_INFO, "Starting condition variable test");
- fprintf(xml, "<testcase classname=\"cond\" name=\"%s\"", testname);
- global_start_time = start_clock();
-
- rc = Thread_wait_cond(cond, 0);
- assert("rc 0 from wait_cond", rc == ETIMEDOUT, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Check timeout");
- start = start_clock();
- rc = Thread_wait_cond(cond, 2);
- duration = elapsed(start);
- assert("rc ETIMEDOUT from lock mutex", rc == ETIMEDOUT, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is 2s", duration >= 2000L, "duration was %ld", duration);
-
- for (i = 0; i < 10; ++i)
- {
- rc = Thread_signal_cond(cond);
- assert("rc 0 from signal cond", rc == 0, "rc was %d\n", rc);
- }
-
- for (i = 0; i < 10; ++i)
- {
- rc = Thread_wait_cond(cond, 0);
- assert("rc non-zero from wait_cond", rc == ETIMEDOUT, "rc was %d", rc);
- }
- rc = Thread_wait_cond(cond, 0);
- assert("rc non-zero from wait_cond", rc == ETIMEDOUT, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Post secondary but it will time out");
- rc = Thread_signal_cond(cond);
- assert("rc 0 from signal cond", rc == 0, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Starting secondary thread");
- Thread_start(cond_secondary, (void*)cond);
- MyLog(LOGA_DEBUG, "wait for secondary thread to enter second wait");
- mysleep(2);
- MyLog(LOGA_DEBUG, "post secondary");
- rc = Thread_signal_cond(cond);
- assert("rc 0 from signal cond", rc == 0, "rc was %d", rc);
- mysleep(1);
- MyLog(LOGA_DEBUG, "Main thread ending");
- exit: MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- #endif
- static thread_return_type WINAPI mutex_secondary(void* n)
- {
- int rc = 0;
- mutex_type mutex = n;
- START_TIME_TYPE start;
- long duration;
-
- start = start_clock();
- rc = Thread_lock_mutex(mutex);
- duration = elapsed(start);
- assert("rc 0 from lock mutex", rc == 0, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is 2s", duration >= 1000L, "duration was %ld", duration);
- rc = Thread_unlock_mutex(mutex);
- assert("rc 0 from unlock mutex", rc == 0, "rc was %d", rc);
- MyLog(LOGA_DEBUG, "Secondary thread ending");
- return 0;
- }
- int test_mutex(struct Options options)
- {
- char* testname = "test_mutex";
- int rc = 0;
- mutex_type mutex = Thread_create_mutex(&rc);
- START_TIME_TYPE start;
- long duration;
- MyLog(LOGA_INFO, "Starting mutex test");
- fprintf(xml, "<testcase classname=\"test\" name=\"%s\"", testname);
- global_start_time = start_clock();
-
- start = start_clock();
- rc = Thread_lock_mutex(mutex);
- duration = elapsed(start);
- assert("rc 0 from lock mutex", rc == 0, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is very low", duration < 5L, "duration was %ld", duration);
- MyLog(LOGA_DEBUG, "Starting secondary thread");
- Thread_start(mutex_secondary, (void*)mutex);
- mysleep(2);
- rc = Thread_unlock_mutex(mutex);
- assert("rc 0 from unlock mutex", rc == 0, "rc was %d", rc);
- start = start_clock();
- rc = Thread_lock_mutex(mutex);
- duration = elapsed(start);
- assert("rc 0 from lock mutex", rc == 0, "rc was %d", rc);
- MyLog(LOGA_INFO, "Lock duration was %ld", duration);
- assert("duration is very low", duration < 5L, "duration was %ld", duration);
- Thread_destroy_mutex(mutex);
- MyLog(LOGA_DEBUG, "Main thread ending");
- MyLog(LOGA_INFO, "%s: test %s. %d tests run, %d failures.",
- (failures == 0) ? "passed" : "failed", testname, tests, failures);
- write_test_result();
- return failures;
- }
- int main(int argc, char** argv)
- {
- int rc = -1;
- int (*tests[])() = {NULL,
- test_mutex,
- test_sem,
- #if !defined(_WIN32) && !defined(_WIN64)
- test_cond
- #endif
- };
- int i;
- xml = fopen("TEST-thread.xml", "w");
- fprintf(xml, "<testsuite name=\"thread\" tests=\"%d\">\n", (int)(ARRAY_SIZE(tests)) - 1);
- getopts(argc, argv);
- for (i = 0; i < options.iterations; ++i)
- {
- if (options.test_no == -1)
- {
- for (options.test_no = 1; options.test_no < ARRAY_SIZE(tests); ++options.test_no)
- {
- failures = rc = 0;
- rc += tests[options.test_no](options);
- }
- }
- else
- {
- if (options.test_no >= ARRAY_SIZE(tests))
- MyLog(LOGA_INFO, "No test number %d", options.test_no);
- else
- {
- rc = tests[options.test_no](options);
- }
- }
- }
- if (rc == 0)
- MyLog(LOGA_INFO, "verdict pass");
- else
- MyLog(LOGA_INFO, "verdict fail");
- fprintf(xml, "</testsuite>\n");
- fclose(xml);
- return rc;
- }
|