aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Willi <martin@revosec.ch>2013-10-23 14:54:00 +0200
committerMartin Willi <martin@revosec.ch>2013-11-06 10:31:06 +0100
commitffab2e0c953281aa0708a5dde4b7e64ddcd47c20 (patch)
treeff0f86ced76415de89ad4b0909f307e62c86194e
parentb1bfe595601cf3806c4831d0c7607be833d6608f (diff)
downloadstrongswan-ffab2e0c953281aa0708a5dde4b7e64ddcd47c20.tar.bz2
strongswan-ffab2e0c953281aa0708a5dde4b7e64ddcd47c20.tar.xz
unit-tests: Add a simple semaphore test
-rw-r--r--src/libstrongswan/tests/suites/test_threading.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libstrongswan/tests/suites/test_threading.c b/src/libstrongswan/tests/suites/test_threading.c
index 295b647ab..dc7796d76 100644
--- a/src/libstrongswan/tests/suites/test_threading.c
+++ b/src/libstrongswan/tests/suites/test_threading.c
@@ -25,6 +25,7 @@
#include <threading/rwlock.h>
#include <threading/rwlock_condvar.h>
#include <threading/spinlock.h>
+#include <threading/semaphore.h>
#include <threading/thread_value.h>
/*******************************************************************************
@@ -777,6 +778,41 @@ START_TEST(test_rwlock_condvar_cancel)
}
END_TEST
+/**
+ * Semaphore for different tests
+ */
+static semaphore_t *semaphore;
+
+static void *semaphore_run(void *data)
+{
+ semaphore->post(semaphore);
+ return NULL;
+}
+
+START_TEST(test_semaphore)
+{
+ thread_t *threads[THREADS];
+ int i, initial = 5;
+
+ semaphore = semaphore_create(initial);
+
+ for (i = 0; i < THREADS; i++)
+ {
+ threads[i] = thread_create(semaphore_run, NULL);
+ }
+ for (i = 0; i < THREADS + initial; i++)
+ {
+ semaphore->wait(semaphore);
+ }
+ for (i = 0; i < THREADS; i++)
+ {
+ threads[i]->join(threads[i]);
+ }
+
+ semaphore->destroy(semaphore);
+}
+END_TEST
+
static void *join_run(void *data)
{
/* force some context switches */
@@ -1299,6 +1335,10 @@ Suite *threading_suite_create()
tcase_add_test(tc, test_rwlock_condvar_cancel);
suite_add_tcase(s, tc);
+ tc = tcase_create("semaphore");
+ tcase_add_test(tc, test_semaphore);
+ suite_add_tcase(s, tc);
+
tc = tcase_create("thread joining");
tcase_add_test(tc, test_join);
tcase_add_test(tc, test_join_exit);