From: Francis Dupont Date: Sat, 7 Jul 2018 09:52:25 +0000 (+0200) Subject: Changed isReady and improve tests X-Git-Tag: trac5694_base~3^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1c0c729f5451b43239d3a862cfa5ca297f177b3c;p=thirdparty%2Fkea.git Changed isReady and improve tests --- diff --git a/src/lib/util/tests/watch_socket_unittests.cc b/src/lib/util/tests/watch_socket_unittests.cc index 0dd8dea169..b503844aef 100644 --- a/src/lib/util/tests/watch_socket_unittests.cc +++ b/src/lib/util/tests/watch_socket_unittests.cc @@ -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 diff --git a/src/lib/util/watch_socket.cc b/src/lib/util/watch_socket.cc index b78fdf6312..dd0c564dbd 100644 --- a/src/lib/util/watch_socket.cc +++ b/src/lib/util/watch_socket.cc @@ -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 #include #include -#include +#include #include 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