Commit 1c0c729f authored by Francis Dupont's avatar Francis Dupont
Browse files

Changed isReady and improve tests

parent af2652d1
// 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
// 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
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment