From: Sebastian Harl Date: Wed, 18 Dec 2013 20:34:43 +0000 (+0100) Subject: frontend/sock_test: Try to connect to the server socket. X-Git-Tag: sysdb-0.1.0~307 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=6204a78be4e8853c5deb17390b54a6b1f4c5f6ca frontend/sock_test: Try to connect to the server socket. This verifies that the socket is actually usable. Also, it fixes a race condition in the test. The UNIX socket file is already created when adding the listener. Thus, the previous synchronization between the main thread and the socket handler thread did not work in most cases. --- diff --git a/t/frontend/sock_test.c b/t/frontend/sock_test.c index 36e7445..55f1a0c 100644 --- a/t/frontend/sock_test.c +++ b/t/frontend/sock_test.c @@ -30,6 +30,8 @@ #include +#include + #include #include @@ -37,6 +39,10 @@ #include +#include +#include +#include + /* * private variables */ @@ -107,6 +113,9 @@ START_TEST(test_listen_and_serve) pthread_t thr; + int sock_fd; + struct sockaddr_un sa; + check = sdb_fe_sock_listen_and_serve(sock, &loop); fail_unless(check < 0, "sdb_fe_sock_listen_and_serve() = %i; " @@ -119,12 +128,27 @@ START_TEST(test_listen_and_serve) fail_unless(check == 0, "INTERNAL ERROR: pthread_create() = %i; expected: 0", check); - /* wait for the socket to appear */ - while (access(tmp_file, F_OK)) { - struct timespec ts = { 0, 100000000 }; - nanosleep(&ts, NULL); + sock_fd = socket(AF_UNIX, SOCK_STREAM, 0); + fail_unless(sock_fd >= 0, + "INTERNAL ERROR: socket() = %d; expected: >= 0", sock_fd); + + sa.sun_family = AF_UNIX; + strncpy(sa.sun_path, tmp_file, sizeof(sa.sun_path)); + + /* wait for socket to become available */ + errno = ECONNREFUSED; + while (errno == ECONNREFUSED) { + check = connect(sock_fd, (struct sockaddr *)&sa, sizeof(sa)); + if (! check) + break; + + fail_unless(errno == ECONNREFUSED, + "INTERNAL ERROR: connect() = %d [errno=%d]; expected: 0", + check, errno); } + close(sock_fd); + loop.do_loop = 0; pthread_join(thr, NULL);