| 1 | /* |
| 2 | The software in this package is distributed under the GNU General |
| 3 | Public License version 2 (with a special exception described below). |
| 4 | |
| 5 | A copy of GNU General Public License (GPL) is included in this distribution, |
| 6 | in the file COPYING.GPL. |
| 7 | |
| 8 | As a special exception, if other files instantiate templates or use macros |
| 9 | or inline functions from this file, or you compile this file and link it |
| 10 | with other works to produce a work based on this file, this file |
| 11 | does not by itself cause the resulting work to be covered |
| 12 | by the GNU General Public License. |
| 13 | |
| 14 | However the source code for this file must still be made available |
| 15 | in accordance with section (3) of the GNU General Public License. |
| 16 | |
| 17 | This exception does not invalidate any other reasons why a work based |
| 18 | on this file might be covered by the GNU General Public License. |
| 19 | */ |
| 20 | |
| 21 | #define BOOST_TEST_MAIN |
| 22 | #define BOOST_TEST_DYN_LINK |
| 23 | |
| 24 | #include <streambuf> |
| 25 | |
| 26 | #include <boost/asio.hpp> |
| 27 | #include <boost/test/unit_test.hpp> |
| 28 | |
| 29 | #include <logfunc.hpp> |
| 30 | |
| 31 | #include "mock_linkstatus.h" |
| 32 | |
| 33 | #include "host/hoststatus.h" |
| 34 | #include "host/pingstatus.h" |
| 35 | |
| 36 | BOOST_AUTO_TEST_SUITE( TestHostStatus ) |
| 37 | |
| 38 | BOOST_AUTO_TEST_CASE( fail_percentage_10 ) |
| 39 | { |
| 40 | I2n::Logger::enable_stderr_log( true ); |
| 41 | I2n::Logger::set_log_level( I2n::Logger::LogLevel::Debug ); |
| 42 | I2n::Logger::GlobalLogger.info() << "Logging enabled for testing"; |
| 43 | |
| 44 | int ping_fail_percentage_limit = 10; |
| 45 | int ping_congestion_percentage_limit = 75; |
| 46 | int congest_caused_by_fail_percentage_limit = 90; |
| 47 | int ping_congestion_duration_thresh = 5; |
| 48 | int n_parallel_pings = 1; |
| 49 | int resolved_ip_count = 10; |
| 50 | long duration = 1000; // microseconds |
| 51 | |
| 52 | LinkStatusItem link_status( new LinkStatus ); |
| 53 | HostStatus host_status( "localhost", |
| 54 | ping_fail_percentage_limit, |
| 55 | ping_congestion_percentage_limit, |
| 56 | congest_caused_by_fail_percentage_limit, |
| 57 | ping_congestion_duration_thresh, |
| 58 | n_parallel_pings, |
| 59 | link_status ); |
| 60 | host_status.set_resolved_ip_count( resolved_ip_count ); |
| 61 | |
| 62 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 63 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); |
| 64 | |
| 65 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 66 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 67 | |
| 68 | host_status.update_ping_statistics( PingStatus_FailureOther, duration); |
| 69 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 70 | |
| 71 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 72 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 73 | |
| 74 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 75 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 76 | |
| 77 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 78 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 79 | |
| 80 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 81 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 82 | |
| 83 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 84 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 85 | |
| 86 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 87 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 88 | |
| 89 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 90 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE( fail_percentage_50 ) |
| 94 | { |
| 95 | int ping_fail_percentage_limit = 50; |
| 96 | int ping_congestion_percentage_limit = 75; |
| 97 | int congest_caused_by_fail_percentage_limit = 90; |
| 98 | int ping_congestion_duration_thresh = 5; |
| 99 | int n_parallel_pings = 1; |
| 100 | int resolved_ip_count = 10; |
| 101 | long duration = 1000; // microseconds |
| 102 | |
| 103 | LinkStatusItem link_status( new LinkStatus ); |
| 104 | HostStatus host_status( "localhost", |
| 105 | ping_fail_percentage_limit, |
| 106 | ping_congestion_percentage_limit, |
| 107 | congest_caused_by_fail_percentage_limit, |
| 108 | ping_congestion_duration_thresh, |
| 109 | n_parallel_pings, |
| 110 | link_status ); |
| 111 | host_status.set_resolved_ip_count( resolved_ip_count ); |
| 112 | |
| 113 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 114 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); |
| 115 | |
| 116 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 117 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), false ); |
| 118 | |
| 119 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 120 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 121 | |
| 122 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 123 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 124 | |
| 125 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 126 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 127 | |
| 128 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 129 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 130 | |
| 131 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 132 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 133 | |
| 134 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 135 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 136 | |
| 137 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 138 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 139 | |
| 140 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 141 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 142 | } |
| 143 | |
| 144 | BOOST_AUTO_TEST_CASE( fail_percentage_80 ) |
| 145 | { |
| 146 | int ping_fail_percentage_limit = 80; |
| 147 | int ping_congestion_percentage_limit = 75; |
| 148 | int congest_caused_by_fail_percentage_limit = 90; |
| 149 | int ping_congestion_duration_thresh = 5; |
| 150 | int n_parallel_pings = 1; |
| 151 | int resolved_ip_count = 10; |
| 152 | long duration = 1000; // microseconds |
| 153 | |
| 154 | LinkStatusItem link_status( new LinkStatus ); |
| 155 | HostStatus host_status( "localhost", |
| 156 | ping_fail_percentage_limit, |
| 157 | ping_congestion_percentage_limit, |
| 158 | congest_caused_by_fail_percentage_limit, |
| 159 | ping_congestion_duration_thresh, |
| 160 | n_parallel_pings, |
| 161 | link_status ); |
| 162 | host_status.set_resolved_ip_count( resolved_ip_count ); |
| 163 | |
| 164 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 165 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 166 | |
| 167 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 168 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 169 | |
| 170 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 171 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 172 | |
| 173 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 174 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 175 | |
| 176 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 177 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 178 | |
| 179 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 180 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 181 | |
| 182 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 183 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 184 | |
| 185 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 186 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 187 | |
| 188 | host_status.update_ping_statistics( PingStatus_FailureOther, duration ); |
| 189 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 190 | |
| 191 | host_status.update_ping_statistics( PingStatus_SuccessReply, duration ); |
| 192 | BOOST_CHECK_EQUAL( host_status.exceeded_ping_failed_limit(), true ); |
| 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_SUITE_END() |