From: Mukund Sivaraman Date: Fri, 25 May 2012 04:16:52 +0000 (+0530) Subject: [1704] Add a comment about use of select() in the test X-Git-Tag: trac2351_base~226^2~60^2^2~47 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d575fbaa4083182e8d6602bb042934451ae06c65;p=thirdparty%2Fkea.git [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. --- diff --git a/src/lib/util/tests/interprocess_sync_file_unittest.cc b/src/lib/util/tests/interprocess_sync_file_unittest.cc index e070e38bc4..a3a50e6b08 100644 --- a/src/lib/util/tests/interprocess_sync_file_unittest.cc +++ b/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]);