]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: add a stream test case checking concurrency limit
authorMartin Willi <martin@revosec.ch>
Fri, 28 Jun 2013 14:33:18 +0000 (16:33 +0200)
committerMartin Willi <martin@revosec.ch>
Wed, 17 Jul 2013 15:11:00 +0000 (17:11 +0200)
src/libstrongswan/tests/test_stream.c

index f0d225c4ec35d2b74cfde85612c28461bd5393d8..6d924e799b4e0f7064cfc3989435594b33c44063 100644 (file)
@@ -91,7 +91,6 @@ static bool on_read(void *data, stream_t *stream)
        return FALSE;
 }
 
-
 START_TEST(test_async)
 {
        stream_service_t *service;
@@ -118,6 +117,53 @@ START_TEST(test_async)
 }
 END_TEST
 
+static void concurrency(void *data, stream_t *stream)
+{
+       static refcount_t refs = 0;
+       u_int current;
+       ssize_t len;
+
+       current = ref_get(&refs);
+       ck_assert(current <= 3);
+       len = stream->write(stream, "x", 1, TRUE);
+       ck_assert_int_eq(len, 1);
+       usleep(1000);
+       ignore_result(ref_put(&refs));
+}
+
+START_TEST(test_concurrency)
+{
+       stream_service_t *service;
+       stream_t *streams[10];
+       ssize_t len;
+       char x;
+       int i;
+
+       lib->processor->set_threads(lib->processor, 8);
+
+       service = lib->streams->create_service(lib->streams, services[_i], 10);
+       ck_assert(service != NULL);
+       service->on_accept(service, concurrency, NULL, JOB_PRIO_HIGH, 3);
+
+       for (i = 0; i < countof(streams); i++)
+       {
+               streams[i] = lib->streams->connect(lib->streams, services[_i]);
+               ck_assert(streams[i] != NULL);
+       }
+       for (i = 0; i < countof(streams); i++)
+       {
+               len = streams[i]->read(streams[i], &x, 1, TRUE);
+               ck_assert_int_eq(len, 1);
+               ck_assert_int_eq(x, 'x');
+       }
+       for (i = 0; i < countof(streams); i++)
+       {
+               streams[i]->destroy(streams[i]);
+       }
+       service->destroy(service);
+}
+END_TEST
+
 Suite *stream_suite_create()
 {
        Suite *s;
@@ -133,5 +179,9 @@ Suite *stream_suite_create()
        tcase_add_loop_test(tc, test_async, 0, countof(services));
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("concurrency");
+       tcase_add_loop_test(tc, test_concurrency, 0, countof(services));
+       suite_add_tcase(s, tc);
+
        return s;
 }