From 6c14bbee444740fbf49d0aa8be1ef650aa335f49 Mon Sep 17 00:00:00 2001 From: Guilherme Maciel Ferreira Date: Wed, 25 Jan 2012 21:11:58 -0200 Subject: [PATCH] Renamed HostStatusAnalyzer to HostStatus. - This is a stronger noun name; - See "The Gravitational Pull of Functional Decomposition" at http://www.eetimes.com/design/embedded/4006522/Using-object-oriented-methods-to-achieve-C--s-promise-Part-3 --- src/CMakeLists.txt | 2 +- src/host/hoststatus.cpp | 170 +++++++++++++++++++++++++++ src/host/hoststatus.h | 79 +++++++++++++ src/host/hoststatusanalyzer.cpp | 170 --------------------------- src/host/hoststatusanalyzer.h | 79 ------------- src/host/pingscheduler.h | 4 +- test/CMakeLists.test_hoststatus.txt | 15 +++ test/CMakeLists.test_hoststatusanalyzer.txt | 15 --- test/CMakeLists.txt | 4 +- test/test_hoststatus.cpp | 155 ++++++++++++++++++++++++ test/test_hoststatusanalyzer.cpp | 155 ------------------------ 11 files changed, 424 insertions(+), 424 deletions(-) create mode 100644 src/host/hoststatus.cpp create mode 100644 src/host/hoststatus.h delete mode 100644 src/host/hoststatusanalyzer.cpp delete mode 100644 src/host/hoststatusanalyzer.h create mode 100644 test/CMakeLists.test_hoststatus.txt delete mode 100644 test/CMakeLists.test_hoststatusanalyzer.txt create mode 100644 test/test_hoststatus.cpp delete mode 100644 test/test_hoststatusanalyzer.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 706ed0d..b0fd7ab 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,7 +57,7 @@ set(SOURCES dns/dnsresolver.cpp dns/hostaddress.cpp dns/timetolive.cpp - host/hoststatusanalyzer.cpp + host/hoststatus.cpp host/messagepayload.cpp host/networkinterfacelist.cpp host/pinger.cpp diff --git a/src/host/hoststatus.cpp b/src/host/hoststatus.cpp new file mode 100644 index 0000000..dbe9e96 --- /dev/null +++ b/src/host/hoststatus.cpp @@ -0,0 +1,170 @@ +/* +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ +#include "host/hoststatus.h" + +#include + +#include + +using namespace std; +using boost::shared_ptr; + +//----------------------------------------------------------------------------- +// HostStatus +//----------------------------------------------------------------------------- + +/** + * @param host_address The address of the host it has to analyze. + * @param ping_fail_percentage_limit The percentage threshold of pings that can + * fail. + * @param link_analyzer The object used to notify the status of the host. + */ +HostStatus::HostStatus( + const string &host_address, + const int ping_fail_limit_percentage, + const shared_ptr link_analyzer +) : + HostAddress( host_address ), + LinkAnalyzer( link_analyzer ), + PingFailLimitPercentage( ping_fail_limit_percentage ), + ResolvedIpCount( 0 ), + PingsPerformedCount( 0 ), + PingsFailedCount( 0 ), + ExceededPingFailedLimit( false ) +{ + BOOST_ASSERT( !HostAddress.empty() ); + BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) ); +} + +HostStatus::~HostStatus() +{ +} + +/** + * @param resolved_ip_count The number of IPs resolved for the host. + */ +void HostStatus::set_resolved_ip_count( const int resolved_ip_count ) +{ + BOOST_ASSERT( 1 <= resolved_ip_count ); + + ResolvedIpCount = resolved_ip_count; +} + +/** + * @return true if the amount of failed pings given to the host exceeded the + * limit. + */ +bool HostStatus::exceeded_ping_failed_limit() const +{ + return ExceededPingFailedLimit; +} + +/** + * Adds a ping status (success or failure). + * @param ping_success + */ +void HostStatus::update_ping_statistics( bool ping_success ) +{ + BOOST_ASSERT( 1 <= ResolvedIpCount ); + BOOST_ASSERT( 0 <= PingsPerformedCount ); + BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); + + increase_ping_performed_count(); + + if ( !ping_success ) + { + increase_ping_failed_count(); + } + + analyze_ping_failed_count(); + + // after we tried all IPs resolved for this host, we can analyze how many + // failed + if ( tried_all_resolved_ip() ) + { + analyze_ping_statistics(); + + reset_ping_counters(); + } + + BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); +} + +bool HostStatus::tried_all_resolved_ip() const +{ + BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); + + return ( PingsPerformedCount == ResolvedIpCount ); +} + +void HostStatus::analyze_ping_statistics() +{ + BOOST_ASSERT( !HostAddress.empty() ); + BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount ); + + // notify if the amount of pings that failed exceed the limit + if ( exceeded_ping_failed_limit() ) + { + LinkAnalyzer->notify_host_down( HostAddress ); + } + else + { + LinkAnalyzer->notify_host_up( HostAddress ); + } + +} + +void HostStatus::reset_ping_counters() +{ + PingsPerformedCount = 0; + PingsFailedCount = 0; +} + +void HostStatus::increase_ping_performed_count() +{ + ++PingsPerformedCount; + + BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); +} + +void HostStatus::increase_ping_failed_count() +{ + ++PingsFailedCount; + + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); +} + +void HostStatus::analyze_ping_failed_count() +{ + BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) ); + BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); + + int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100; + + // keep a boolean variable because the PingsFailedCount can be reseted + if ( PingsFailedCount > ping_fail_limit_count ) + { + ExceededPingFailedLimit = true; + } + else + { + ExceededPingFailedLimit = false; + } +} diff --git a/src/host/hoststatus.h b/src/host/hoststatus.h new file mode 100644 index 0000000..491fc39 --- /dev/null +++ b/src/host/hoststatus.h @@ -0,0 +1,79 @@ +/* +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ +#ifndef HOST_STATUS_H +#define HOST_STATUS_H + +#include + +#include + +#include "link/linkstatusanalyzer.h" + +//----------------------------------------------------------------------------- +// HostStatus +//----------------------------------------------------------------------------- + +/** + * @brief This class analyze the host status (up or down) based on the pings + * responses (success or failure). And notifies its status. + * Scope: one object per host. + */ +class HostStatus +{ +public: + HostStatus( + const std::string &host_address, + const int ping_fail_limit_percentage, + const boost::shared_ptr link_analyzer + ); + ~HostStatus(); + + void set_resolved_ip_count( const int resolved_ip_count ); + bool exceeded_ping_failed_limit() const; + void update_ping_statistics( bool ping_success ); + +private: + bool tried_all_resolved_ip() const; + void analyze_ping_statistics(); + void reset_ping_counters(); + void increase_ping_performed_count(); + void increase_ping_failed_count(); + void analyze_ping_failed_count(); + +private: + /// the DNS address of the host to analyze + std::string HostAddress; + /// the object responsible to analyze the link + const boost::shared_ptr LinkAnalyzer; + /// the maximum amount of pings that can fail without warning + int PingFailLimitPercentage; + /// the amount of IPs that are aliases to the host DNS + int ResolvedIpCount; + /// the amount of pings sent until now + int PingsPerformedCount; + /// the amount of pings sent that failed until now + int PingsFailedCount; + /// boolean flag that indicate if the last set of failed pings exceed the + /// limit + bool ExceededPingFailedLimit; + +}; + +#endif // HOST_STATUS_H diff --git a/src/host/hoststatusanalyzer.cpp b/src/host/hoststatusanalyzer.cpp deleted file mode 100644 index 93a6229..0000000 --- a/src/host/hoststatusanalyzer.cpp +++ /dev/null @@ -1,170 +0,0 @@ -/* -The software in this package is distributed under the GNU General -Public License version 2 (with a special exception described below). - -A copy of GNU General Public License (GPL) is included in this distribution, -in the file COPYING.GPL. - -As a special exception, if other files instantiate templates or use macros -or inline functions from this file, or you compile this file and link it -with other works to produce a work based on this file, this file -does not by itself cause the resulting work to be covered -by the GNU General Public License. - -However the source code for this file must still be made available -in accordance with section (3) of the GNU General Public License. - -This exception does not invalidate any other reasons why a work based -on this file might be covered by the GNU General Public License. -*/ -#include "host/hoststatusanalyzer.h" - -#include - -#include - -using namespace std; -using boost::shared_ptr; - -//----------------------------------------------------------------------------- -// HostStatusAnalyzer -//----------------------------------------------------------------------------- - -/** - * @param host_address the address of the host it has to analyze. - * @param ping_fail_percentage_limit the percentage threshold of pings that can - * fail. - * @param link_analyzer the object used to notify the status of the host. - */ -HostStatusAnalyzer::HostStatusAnalyzer( - const string &host_address, - const int ping_fail_limit_percentage, - const shared_ptr link_analyzer -) : - HostAddress( host_address ), - LinkAnalyzer( link_analyzer ), - PingFailLimitPercentage( ping_fail_limit_percentage ), - ResolvedIpCount( 0 ), - PingsPerformedCount( 0 ), - PingsFailedCount( 0 ), - ExceededPingFailedLimit( false ) -{ - BOOST_ASSERT( !HostAddress.empty() ); - BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) ); -} - -HostStatusAnalyzer::~HostStatusAnalyzer() -{ -} - -/** - * @param resolved_ip_count the number of IPs resolved for the host. - */ -void HostStatusAnalyzer::set_resolved_ip_count( const int resolved_ip_count ) -{ - BOOST_ASSERT( 1 <= resolved_ip_count ); - - ResolvedIpCount = resolved_ip_count; -} - -/** - * @return true if the amount of failed pings given to the host exceeded the - * limit. - */ -bool HostStatusAnalyzer::exceeded_ping_failed_limit() const -{ - return ExceededPingFailedLimit; -} - -/** - * Adds a ping status (success or failure). - * @param ping_success - */ -void HostStatusAnalyzer::update_ping_statistics( bool ping_success ) -{ - BOOST_ASSERT( 1 <= ResolvedIpCount ); - BOOST_ASSERT( 0 <= PingsPerformedCount ); - BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); - - increase_ping_performed_count(); - - if ( !ping_success ) - { - increase_ping_failed_count(); - } - - analyze_ping_failed_count(); - - // after we tried all IPs resolved for this host, we can analyze how many - // failed - if ( tried_all_resolved_ip() ) - { - analyze_ping_statistics(); - - reset_ping_counters(); - } - - BOOST_ASSERT( PingsFailedCount <= PingsPerformedCount ); -} - -bool HostStatusAnalyzer::tried_all_resolved_ip() const -{ - BOOST_ASSERT( ( 0 < PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); - - return ( PingsPerformedCount == ResolvedIpCount ); -} - -void HostStatusAnalyzer::analyze_ping_statistics() -{ - BOOST_ASSERT( !HostAddress.empty() ); - BOOST_ASSERT( PingsPerformedCount == ResolvedIpCount ); - - // notify if the amount of pings that failed exceed the limit - if ( exceeded_ping_failed_limit() ) - { - LinkAnalyzer->notify_host_down( HostAddress ); - } - else - { - LinkAnalyzer->notify_host_up( HostAddress ); - } - -} - -void HostStatusAnalyzer::reset_ping_counters() -{ - PingsPerformedCount = 0; - PingsFailedCount = 0; -} - -void HostStatusAnalyzer::increase_ping_performed_count() -{ - ++PingsPerformedCount; - - BOOST_ASSERT( ( 0 <= PingsPerformedCount ) && ( PingsPerformedCount <= ResolvedIpCount ) ); -} - -void HostStatusAnalyzer::increase_ping_failed_count() -{ - ++PingsFailedCount; - - BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); -} - -void HostStatusAnalyzer::analyze_ping_failed_count() -{ - BOOST_ASSERT( ( 0 <= PingFailLimitPercentage ) && ( PingFailLimitPercentage <= 100 ) ); - BOOST_ASSERT( ( 0 <= PingsFailedCount ) && ( PingsFailedCount <= PingsPerformedCount ) ); - - int ping_fail_limit_count = ( ResolvedIpCount * PingFailLimitPercentage ) / 100; - - // keep a boolean variable because the PingsFailedCount can be reseted - if ( PingsFailedCount > ping_fail_limit_count ) - { - ExceededPingFailedLimit = true; - } - else - { - ExceededPingFailedLimit = false; - } -} diff --git a/src/host/hoststatusanalyzer.h b/src/host/hoststatusanalyzer.h deleted file mode 100644 index be115ca..0000000 --- a/src/host/hoststatusanalyzer.h +++ /dev/null @@ -1,79 +0,0 @@ -/* -The software in this package is distributed under the GNU General -Public License version 2 (with a special exception described below). - -A copy of GNU General Public License (GPL) is included in this distribution, -in the file COPYING.GPL. - -As a special exception, if other files instantiate templates or use macros -or inline functions from this file, or you compile this file and link it -with other works to produce a work based on this file, this file -does not by itself cause the resulting work to be covered -by the GNU General Public License. - -However the source code for this file must still be made available -in accordance with section (3) of the GNU General Public License. - -This exception does not invalidate any other reasons why a work based -on this file might be covered by the GNU General Public License. -*/ -#ifndef HOST_STATUS_ANALYZER_H -#define HOST_STATUS_ANALYZER_H - -#include - -#include - -#include "link/linkstatusanalyzer.h" - -//----------------------------------------------------------------------------- -// HostStatusAnalyzer -//----------------------------------------------------------------------------- - -/** - * @brief This class analyze the host status (up or down) based on the pings - * responses (success or failure). And notifies its status. - * Scope: one object per host. - */ -class HostStatusAnalyzer -{ -public: - HostStatusAnalyzer( - const std::string &host_address, - const int ping_fail_limit_percentage, - const boost::shared_ptr link_analyzer - ); - ~HostStatusAnalyzer(); - - void set_resolved_ip_count( const int resolved_ip_count ); - bool exceeded_ping_failed_limit() const; - void update_ping_statistics( bool ping_success ); - -private: - bool tried_all_resolved_ip() const; - void analyze_ping_statistics(); - void reset_ping_counters(); - void increase_ping_performed_count(); - void increase_ping_failed_count(); - void analyze_ping_failed_count(); - -private: - /// the DNS address of the host to analyze - std::string HostAddress; - /// the object responsible to analyze the link - const boost::shared_ptr LinkAnalyzer; - /// the maximum amount of pings that can fail without warning - int PingFailLimitPercentage; - /// the amount of IPs that are aliases to the host DNS - int ResolvedIpCount; - /// the amount of pings sent until now - int PingsPerformedCount; - /// the amount of pings sent that failed until now - int PingsFailedCount; - /// boolean flag that indicate if the last set of failed pings exceed the - /// limit - bool ExceededPingFailedLimit; - -}; - -#endif // HOST_STATUS_ANALYZER_H diff --git a/src/host/pingscheduler.h b/src/host/pingscheduler.h index fdc43ce..3f7f038 100644 --- a/src/host/pingscheduler.h +++ b/src/host/pingscheduler.h @@ -29,7 +29,7 @@ on this file might be covered by the GNU General Public License. #include "dns/dnsresolver.h" #include "link/linkstatusanalyzer.h" -#include "host/hoststatusanalyzer.h" +#include "host/hoststatus.h" #include "host/pinger.h" #include "host/pingerfactory.h" #include "host/pinginterval.h" @@ -98,7 +98,7 @@ private: /// The port to ping at destination host int DestinationPort; /// Object responsible to evaluate the status of the host - HostStatusAnalyzer HostAnalyzer; + HostStatus HostAnalyzer; /// Internal boost pinger object boost::shared_ptr Ping; /// Thread object diff --git a/test/CMakeLists.test_hoststatus.txt b/test/CMakeLists.test_hoststatus.txt new file mode 100644 index 0000000..2261a76 --- /dev/null +++ b/test/CMakeLists.test_hoststatus.txt @@ -0,0 +1,15 @@ +# compiler: creates the binaries +add_executable(test_hoststatus + test_hoststatus.cpp + ${CMAKE_SOURCE_DIR}/src/host/hoststatus.cpp +) + +# linker: link the program against the libraries +target_link_libraries( + test_hoststatus + ${I2NCOMMON_LIBRARIES} + ${Boost_LIBRARIES} +) + +# cmake: invocation via "make test" +add_test(test_hoststatus test_hoststatus) diff --git a/test/CMakeLists.test_hoststatusanalyzer.txt b/test/CMakeLists.test_hoststatusanalyzer.txt deleted file mode 100644 index 901dead..0000000 --- a/test/CMakeLists.test_hoststatusanalyzer.txt +++ /dev/null @@ -1,15 +0,0 @@ -# compiler: creates the binaries -add_executable(test_hoststatusanalyzer - test_hoststatusanalyzer.cpp - ${CMAKE_SOURCE_DIR}/src/host/hoststatusanalyzer.cpp -) - -# linker: link the program against the libraries -target_link_libraries( - test_hoststatusanalyzer - ${I2NCOMMON_LIBRARIES} - ${Boost_LIBRARIES} -) - -# cmake: invocation via "make test" -add_test(test_hoststatusanalyzer test_hoststatusanalyzer) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 131855c..975be0c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -25,7 +25,7 @@ enable_testing() # cmake: inclusion of each test case cmake file include(CMakeLists.test_messagepayload.txt) -include(CMakeLists.test_hoststatusanalyzer.txt) +include(CMakeLists.test_hoststatus.txt) include(CMakeLists.test_ipv4header.txt) include(CMakeLists.test_ipv6header.txt) include(CMakeLists.test_icmpv4header.txt) @@ -34,7 +34,7 @@ include(CMakeLists.test_icmpv6header.txt) # cmake: add a custom "make check" target which automatically builds the binary add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND} DEPENDS test_messagepayload - test_hoststatusanalyzer + test_hoststatus test_ipv4header test_ipv6header test_icmpv4header diff --git a/test/test_hoststatus.cpp b/test/test_hoststatus.cpp new file mode 100644 index 0000000..920c2dc --- /dev/null +++ b/test/test_hoststatus.cpp @@ -0,0 +1,155 @@ +/* +The software in this package is distributed under the GNU General +Public License version 2 (with a special exception described below). + +A copy of GNU General Public License (GPL) is included in this distribution, +in the file COPYING.GPL. + +As a special exception, if other files instantiate templates or use macros +or inline functions from this file, or you compile this file and link it +with other works to produce a work based on this file, this file +does not by itself cause the resulting work to be covered +by the GNU General Public License. + +However the source code for this file must still be made available +in accordance with section (3) of the GNU General Public License. + +This exception does not invalidate any other reasons why a work based +on this file might be covered by the GNU General Public License. +*/ + +#define BOOST_TEST_MAIN +#define BOOST_TEST_DYN_LINK + +#include + +#include +#include + +#include "mock_linkstatusanalyzer.h" + +#include "host/hoststatus.h" + +BOOST_AUTO_TEST_SUITE( TestHostStatus ) + +BOOST_AUTO_TEST_CASE( fail_percentage_10 ) +{ + int ping_fail_percentage_limit = 10; + int resolved_ip_count = 10; + + LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); + host_status.set_resolved_ip_count( resolved_ip_count ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); +} + +BOOST_AUTO_TEST_CASE( fail_percentage_50 ) +{ + int ping_fail_percentage_limit = 50; + int resolved_ip_count = 10; + + LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); + host_status.set_resolved_ip_count( resolved_ip_count ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); +} + +BOOST_AUTO_TEST_CASE( fail_percentage_80 ) +{ + int ping_fail_percentage_limit = 80; + int resolved_ip_count = 10; + + LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); + HostStatus host_status( "localhost", ping_fail_percentage_limit, link_status ); + host_status.set_resolved_ip_count( resolved_ip_count ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); + + host_status.update_ping_statistics( false ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); + + host_status.update_ping_statistics( true ); + BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); +} + +BOOST_AUTO_TEST_SUITE_END() diff --git a/test/test_hoststatusanalyzer.cpp b/test/test_hoststatusanalyzer.cpp deleted file mode 100644 index 2e2d788..0000000 --- a/test/test_hoststatusanalyzer.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* -The software in this package is distributed under the GNU General -Public License version 2 (with a special exception described below). - -A copy of GNU General Public License (GPL) is included in this distribution, -in the file COPYING.GPL. - -As a special exception, if other files instantiate templates or use macros -or inline functions from this file, or you compile this file and link it -with other works to produce a work based on this file, this file -does not by itself cause the resulting work to be covered -by the GNU General Public License. - -However the source code for this file must still be made available -in accordance with section (3) of the GNU General Public License. - -This exception does not invalidate any other reasons why a work based -on this file might be covered by the GNU General Public License. -*/ - -#define BOOST_TEST_MAIN -#define BOOST_TEST_DYN_LINK - -#include - -#include -#include - -#include "mock_linkstatusanalyzer.h" - -#include "host/hoststatusanalyzer.h" - -BOOST_AUTO_TEST_SUITE( TestHostStatusAnalyzer ) - -BOOST_AUTO_TEST_CASE( fail_percentage_10 ) -{ - int ping_fail_percentage_limit = 10; - int resolved_ip_count = 10; - - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); - HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status ); - host_status.set_resolved_ip_count( resolved_ip_count ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); -} - -BOOST_AUTO_TEST_CASE( fail_percentage_50 ) -{ - int ping_fail_percentage_limit = 50; - int resolved_ip_count = 10; - - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); - HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status ); - host_status.set_resolved_ip_count( resolved_ip_count ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); -} - -BOOST_AUTO_TEST_CASE( fail_percentage_80 ) -{ - int ping_fail_percentage_limit = 80; - int resolved_ip_count = 10; - - LinkStatusAnalyzerItem link_status( new LinkStatusAnalyzer ); - HostStatusAnalyzer host_status( "localhost", ping_fail_percentage_limit, link_status ); - host_status.set_resolved_ip_count( resolved_ip_count ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); - - host_status.update_ping_statistics( false ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); - - host_status.update_ping_statistics( true ); - BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); -} - -BOOST_AUTO_TEST_SUITE_END() -- 1.7.1