]> git.ipfire.org Git - thirdparty/kea.git/commitdiff
Changed isReady and improve tests
authorFrancis Dupont <fdupont@isc.org>
Sat, 7 Jul 2018 09:52:25 +0000 (11:52 +0200)
committerFrancis Dupont <fdupont@isc.org>
Sat, 7 Jul 2018 09:52:25 +0000 (11:52 +0200)
src/lib/util/tests/watch_socket_unittests.cc
src/lib/util/watch_socket.cc

index 0dd8dea16931292a90a2fcec5d196b6522ae6736..b503844aefc1488788f299387817c322a7517745 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -139,10 +139,17 @@ TEST(WatchSocketTest, closedWhileReady) {
     /// Verify that the socket can be marked ready.
     ASSERT_NO_THROW(watch->markReady());
     EXPECT_EQ(1, selectCheck(select_fd));
+    EXPECT_TRUE(watch->isReady());
 
     // Interfere by closing the fd.
     ASSERT_EQ(0, close(select_fd));
 
+    // Verify that isReady() does not throw.
+    ASSERT_NO_THROW(watch->isReady());
+
+    // and return false.
+    EXPECT_FALSE(watch->isReady());
+
     // Verify that trying to clear it does not throw.
     ASSERT_NO_THROW(watch->clearReady());
 
@@ -168,6 +175,7 @@ TEST(WatchSocketTest, emptyReadySelectFd) {
 
     /// Verify that the socket can be marked ready.
     ASSERT_NO_THROW(watch->markReady());
+    EXPECT_TRUE(watch->isReady());
     EXPECT_EQ(1, selectCheck(select_fd));
 
     // Interfere by reading the fd. This should empty the read pipe.
@@ -179,7 +187,8 @@ TEST(WatchSocketTest, emptyReadySelectFd) {
     // make sure we aren't in a weird state.
     ASSERT_NO_THROW(watch->clearReady());
 
-    // Verify the select_fd fails as socket is invalid/closed.
+    // Verify the select_fd does not fail.
+    EXPECT_FALSE(watch->isReady());
     EXPECT_EQ(0, selectCheck(select_fd));
 
     // Verify that getSelectFd() returns is still good.
@@ -201,6 +210,7 @@ TEST(WatchSocketTest, badReadOnClear) {
 
     /// Verify that the socket can be marked ready.
     ASSERT_NO_THROW(watch->markReady());
+    EXPECT_TRUE(watch->isReady());
     EXPECT_EQ(1, selectCheck(select_fd));
 
     // Interfere by reading the fd. This should empty the read pipe.
@@ -214,6 +224,7 @@ TEST(WatchSocketTest, badReadOnClear) {
     ASSERT_THROW(watch->clearReady(), WatchSocketError);
 
     // Verify the select_fd does not evaluate to ready.
+    EXPECT_FALSE(watch->isReady());
     EXPECT_NE(1, selectCheck(select_fd));
 
     // Verify that getSelectFd() returns INVALID.
@@ -244,6 +255,9 @@ TEST(WatchSocketTest, explicitClose) {
     EXPECT_EQ(WatchSocket::SOCKET_NOT_VALID, watch->getSelectFd());
     // No errors should be reported.
     EXPECT_TRUE(error_string.empty());
+    // Not ready too.
+    ASSERT_NO_THROW(watch->isReady());
+    EXPECT_FALSE(watch->isReady());
 }
 
 } // end of anonymous namespace
index b78fdf6312c5c3fd4f735628431202c97fbd7282..dd0c564dbdb7c17dae46ce48b7135faa7dc8a8f5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2014-2015,2017 Internet Systems Consortium, Inc. ("ISC")
+// Copyright (C) 2014-2018 Internet Systems Consortium, Inc. ("ISC")
 //
 // This Source Code Form is subject to the terms of the Mozilla Public
 // License, v. 2.0. If a copy of the MPL was not distributed with this
@@ -15,7 +15,7 @@
 #include <errno.h>
 #include <sstream>
 #include <string.h>
-#include <sys/select.h>
+#include <sys/ioctl.h>
 #include <unistd.h>
 
 namespace isc {
@@ -92,19 +92,11 @@ WatchSocket::isReady() {
         return (false);
     }
 
-    fd_set read_fds;
-    FD_ZERO(&read_fds);
-
-    // Add select_fd socket to listening set
-    FD_SET(sink_,  &read_fds);
-
-    // Set zero timeout (non-blocking).
-    struct timeval select_timeout;
-    select_timeout.tv_sec = 0;
-    select_timeout.tv_usec = 0;
-
+    // Use ioctl FIONREAD vs polling select as it is faster.
+    int len;
+    int result = ioctl(sink_, FIONREAD, &len);
     // Return true only if read ready, treat error same as not ready.
-    return (select(sink_ + 1, &read_fds, NULL, NULL, &select_timeout) > 0);
+    return ((result == 0) && (len > 0));
 }
 
 void