Code

frontend/sock_test: Try to connect to the server socket.
authorSebastian Harl <sh@tokkee.org>
Wed, 18 Dec 2013 20:34:43 +0000 (21:34 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 18 Dec 2013 20:34:43 +0000 (21:34 +0100)
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.

t/frontend/sock_test.c

index 36e744524da97cd76b66f1fd0daaad576e95331c..55f1a0c47789229457ad0febd84610eda5d1fa6c 100644 (file)
@@ -30,6 +30,8 @@
 
 #include <check.h>
 
+#include <errno.h>
+
 #include <stdio.h>
 #include <stdlib.h>
 
 
 #include <pthread.h>
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
 /*
  * 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);