diff options
author | Michael Adam <obnox@samba.org> | 2016-09-22 03:53:27 +0200 |
---|---|---|
committer | Andreas Schneider <asn@samba.org> | 2018-05-02 16:31:42 +0200 |
commit | a5c08518e985bd3f0cb274c274c034be37fa5629 (patch) | |
tree | 7bd9c69acba66db9b9686bc3d66cf8a251659e80 /tests | |
parent | b6909fc91d5d16ced108700cbd5cd8a13481a6c4 (diff) | |
download | socket_wrapper-a5c08518e985bd3f0cb274c274c034be37fa5629.tar.gz socket_wrapper-a5c08518e985bd3f0cb274c274c034be37fa5629.tar.xz socket_wrapper-a5c08518e985bd3f0cb274c274c034be37fa5629.zip |
tests: Add new test to check mutex lock contention
Signed-off-by: Michael Adam <obnox@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 3 | ||||
-rw-r--r-- | tests/test_thread_sockets.c | 72 |
2 files changed, 74 insertions, 1 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 836a5e2..0887a01 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -37,7 +37,8 @@ set(SWRAP_TESTS test_swrap_unit test_max_sockets test_close_failure - test_fork_thread_deadlock) + test_fork_thread_deadlock + test_thread_sockets) if (HAVE_STRUCT_MSGHDR_MSG_CONTROL) set(SWRAP_TESTS ${SWRAP_TESTS} test_sendmsg_recvmsg_fd) diff --git a/tests/test_thread_sockets.c b/tests/test_thread_sockets.c new file mode 100644 index 0000000..364a001 --- /dev/null +++ b/tests/test_thread_sockets.c @@ -0,0 +1,72 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <cmocka.h> + +#include <pthread.h> + +#include <stdlib.h> +#include <errno.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <arpa/inet.h> +#include <unistd.h> +#include <grp.h> + +#define NUM_THREADS 10 + +static void *thread_worker(void *arg) +{ + int i; + + (void) arg; /* unused */ + + for (i = 0; i < 1000; i++) { + int s; + + s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + assert_return_code(s, errno); + + close(s); + } + + return NULL; +} + +static void test_threads_socket(void **state) +{ + pthread_attr_t pthread_custom_attr; + pthread_t threads[NUM_THREADS]; + int i; + + (void) state; /* unused */ + + pthread_attr_init(&pthread_custom_attr); + + for (i = 0; i < NUM_THREADS; i++) { + pthread_create(&threads[i], + &pthread_custom_attr, + thread_worker, + NULL); + } + + for (i = 0; i < NUM_THREADS; i++) { + pthread_join(threads[i], NULL); + } + + pthread_attr_destroy(&pthread_custom_attr); +} + +int main(void) { + int rc; + + const struct CMUnitTest thread_tests[] = { + cmocka_unit_test(test_threads_socket), + }; + + rc = cmocka_run_group_tests(thread_tests, NULL, NULL); + + return rc; +} |