unit-tests: Add condvar broadcast test
authorMartin Willi <martin@revosec.ch>
Mon, 21 Oct 2013 15:27:49 +0000 (17:27 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 6 Nov 2013 09:31:04 +0000 (10:31 +0100)
src/libstrongswan/tests/suites/test_threading.c

index 97e6e33..108c4fe 100644 (file)
@@ -221,6 +221,48 @@ START_TEST(test_condvar)
 }
 END_TEST
 
+static void *condvar_run_broad(void *data)
+{
+       mutex->lock(mutex);
+       while (sigcount < 0)
+       {
+               condvar->wait(condvar, mutex);
+       }
+       mutex->unlock(mutex);
+       return NULL;
+}
+
+START_TEST(test_condvar_broad)
+{
+       thread_t *threads[THREADS];
+       int i;
+
+       mutex = mutex_create(MUTEX_TYPE_DEFAULT);
+       condvar = condvar_create(CONDVAR_TYPE_DEFAULT);
+       sigcount = 0;
+
+       for (i = 0; i < THREADS; i++)
+       {
+               threads[i] = thread_create(condvar_run_broad, NULL);
+       }
+
+       sched_yield();
+
+       mutex->lock(mutex);
+       sigcount = 1;
+       condvar->broadcast(condvar);
+       mutex->unlock(mutex);
+
+       for (i = 0; i < THREADS; i++)
+       {
+               threads[i]->join(threads[i]);
+       }
+
+       mutex->destroy(mutex);
+       condvar->destroy(condvar);
+}
+END_TEST
+
 static void *join_run(void *data)
 {
        /* force some context switches */
@@ -720,6 +762,7 @@ Suite *threading_suite_create()
 
        tc = tcase_create("condvar");
        tcase_add_test(tc, test_condvar);
+       tcase_add_test(tc, test_condvar_broad);
        suite_add_tcase(s, tc);
 
        tc = tcase_create("thread joining");