unit-tests: Add a rwlock condvar absolute timed wait test
authorMartin Willi <martin@revosec.ch>
Tue, 22 Oct 2013 15:41:37 +0000 (17:41 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:05 +0000 (10:31 +0100)
src/libstrongswan/tests/suites/test_threading.c

index a8de5c3..02be9ce 100644 (file)
@@ -634,6 +634,48 @@ START_TEST(test_rwlock_condvar_timed)
 }
 END_TEST
 
+START_TEST(test_rwlock_condvar_timed_abs)
+{
+       thread_t *thread;
+       timeval_t start, end, abso, diff = { .tv_usec = 50000 };
+
+       rwlock = rwlock_create(RWLOCK_TYPE_DEFAULT);
+       rwcond = rwlock_condvar_create();
+       sigcount = 0;
+
+       rwlock->write_lock(rwlock);
+       while (TRUE)
+       {
+               time_monotonic(&start);
+               timeradd(&start, &diff, &abso);
+               if (rwcond->timed_wait_abs(rwcond, rwlock, abso))
+               {
+                       break;
+               }
+       }
+       rwlock->unlock(rwlock);
+       time_monotonic(&end);
+       ck_assert_msg(timercmp(&end, &abso, >), "end: %u.%u, abso: %u.%u",
+                                       end.tv_sec, end.tv_usec, abso.tv_sec, abso.tv_usec);
+
+       thread = thread_create(rwlock_condvar_run, NULL);
+
+       time_monotonic(&start);
+       diff.tv_sec = 1;
+       timeradd(&start, &diff, &abso);
+       rwlock->write_lock(rwlock);
+       while (sigcount == 0)
+       {
+               ck_assert(!rwcond->timed_wait_abs(rwcond, rwlock, abso));
+       }
+       rwlock->unlock(rwlock);
+
+       thread->join(thread);
+       rwlock->destroy(rwlock);
+       rwcond->destroy(rwcond);
+}
+END_TEST
+
 static void *join_run(void *data)
 {
        /* force some context switches */
@@ -1148,6 +1190,7 @@ Suite *threading_suite_create()
        tcase_add_test(tc, test_rwlock_condvar);
        tcase_add_test(tc, test_rwlock_condvar_broad);
        tcase_add_test(tc, test_rwlock_condvar_timed);
+       tcase_add_test(tc, test_rwlock_condvar_timed_abs);
        suite_add_tcase(s, tc);
 
        tc = tcase_create("thread joining");