Browse Source

[1704] Add a comment about use of select() in the test

This commit also:

 * Moves the parent's code to read the lock state into a helper
   function.

 * Describes the 5 second timeout as an arbitrary value.
Mukund Sivaraman 13 years ago
parent
commit
d575fbaa40
1 changed files with 31 additions and 32 deletions
  1. 31 32
      src/lib/util/tests/interprocess_sync_file_unittest.cc

+ 31 - 32
src/lib/util/tests/interprocess_sync_file_unittest.cc

@@ -20,6 +20,35 @@ using namespace std;
 namespace isc {
 namespace util {
 
+static unsigned char
+parent_read_locked_state (int fd) {
+  unsigned char locked = 0xff;
+
+  fd_set rfds;
+  FD_ZERO(&rfds);
+  FD_SET(fd, &rfds);
+
+  // We use select() here to wait for new data on the input end of
+  // the pipe. We wait for 5 seconds (an arbitrary value) for input
+  // data, and continue if no data is available. This is done so
+  // that read() is not blocked due to some issue in the child
+  // process (and the tests continue running).
+
+  struct timeval tv;
+  tv.tv_sec = 5;
+  tv.tv_usec = 0;
+
+  const int nfds = select(fd + 1, &rfds, NULL, NULL, &tv);
+  EXPECT_EQ(1, nfds);
+
+  if (nfds == 1) {
+      // Read status
+      read(fd, &locked, sizeof(locked));
+  }
+
+  return locked;
+}
+
 TEST(InterprocessSyncFileTest, TestLock) {
   InterprocessSyncFile sync("test");
   InterprocessSyncLocker locker(sync);
@@ -53,25 +82,10 @@ TEST(InterprocessSyncFileTest, TestLock) {
       close(fds[1]);
       exit(0);
   } else {
-      unsigned char locked = 0;
       // Parent reads from pipe
       close(fds[1]);
 
-      fd_set rfds;
-      FD_ZERO(&rfds);
-      FD_SET(fds[0], &rfds);
-
-      struct timeval tv;
-      tv.tv_sec = 5;
-      tv.tv_usec = 0;
-
-      const int nfds = select(fds[0] + 1, &rfds, NULL, NULL, &tv);
-      EXPECT_EQ(1, nfds);
-
-      if (nfds == 1) {
-          // Read status
-          read(fds[0], &locked, sizeof(locked));
-      }
+      const unsigned char locked = parent_read_locked_state(fds[0]);
 
       close(fds[0]);
 
@@ -121,25 +135,10 @@ TEST(InterprocessSyncFileTest, TestMultipleFilesForked) {
       close(fds[1]);
       exit(0);
   } else {
-      unsigned char locked = 0xff;
       // Parent reads from pipe
       close(fds[1]);
 
-      fd_set rfds;
-      FD_ZERO(&rfds);
-      FD_SET(fds[0], &rfds);
-
-      struct timeval tv;
-      tv.tv_sec = 5;
-      tv.tv_usec = 0;
-
-      const int nfds = select(fds[0] + 1, &rfds, NULL, NULL, &tv);
-      EXPECT_EQ(1, nfds);
-
-      if (nfds == 1) {
-          // Read status
-          read(fds[0], &locked, sizeof(locked));
-      }
+      const unsigned char locked = parent_read_locked_state(fds[0]);
 
       close(fds[0]);