1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "remoting/host/zombie_host_detector.h"
#include "base/functional/callback_helpers.h"
#include "base/test/bind.h"
#include "base/test/mock_callback.h"
#include "base/test/task_environment.h"
#include "net/base/mock_network_change_notifier.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
// Extra time to fast forward to make sure the task gets run.
static constexpr base::TimeDelta kFastForwardDelta = base::Seconds(1);
} // namespace
namespace remoting {
class ZombieHostDetectorTest : public testing::Test {
public:
ZombieHostDetectorTest() {
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
zombie_host_detector_.Start();
}
protected:
void FastForwardUntilZombieStateDetected(
base::TimeDelta fast_forward_interval,
const base::RepeatingClosure& on_fast_forward) {
bool zombie_state_detected = false;
EXPECT_CALL(mock_on_zombie_state_detected_, Run()).WillOnce([&]() {
zombie_state_detected = true;
});
while (!zombie_state_detected) {
task_environment_.FastForwardBy(fast_forward_interval);
on_fast_forward.Run();
}
}
base::TimeDelta GetNextDetectionDurationSinceNow() const {
return zombie_host_detector_.GetNextDetectionTime() -
base::TimeTicks::Now();
}
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
std::unique_ptr<net::test::MockNetworkChangeNotifier>
network_change_notifier_{net::test::MockNetworkChangeNotifier::Create()};
base::MockCallback<base::OnceClosure> mock_on_zombie_state_detected_;
ZombieHostDetector zombie_host_detector_{
mock_on_zombie_state_detected_.Get()};
};
TEST_F(ZombieHostDetectorTest, NoEvent_Noop) {
task_environment_.FastForwardBy(base::Hours(1));
}
TEST_F(ZombieHostDetectorTest, AllEventsAreCurrent_Noop) {
// Fast forward to 5s before detection.
task_environment_.FastForwardBy(GetNextDetectionDurationSinceNow() -
base::Seconds(5));
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
task_environment_.FastForwardBy(base::Seconds(6));
}
TEST_F(ZombieHostDetectorTest, HeartbeatNotCurrent_CallbackCalled) {
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
// Only report signaling active but not heartbeat.
FastForwardUntilZombieStateDetected(
ZombieHostDetector::kMaxSignalingActiveInterval - kFastForwardDelta,
base::BindLambdaForTesting(
[&]() { zombie_host_detector_.OnSignalingActive(); }));
}
TEST_F(ZombieHostDetectorTest, SignalingNotCurrent_CallbackCalled) {
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
// Only report heartbeat but not signaling active.
FastForwardUntilZombieStateDetected(
ZombieHostDetector::kMaxHeartbeatInterval - kFastForwardDelta,
base::BindLambdaForTesting(
[&]() { zombie_host_detector_.OnHeartbeatSent(); }));
}
TEST_F(ZombieHostDetectorTest, NeitherIsCurrent_CallbackCalled) {
EXPECT_CALL(mock_on_zombie_state_detected_, Run()).Times(1);
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
task_environment_.FastForwardBy(base::Hours(1));
}
TEST_F(ZombieHostDetectorTest, NeitherIsCurrentWhileNoConnection_Noop) {
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_NONE);
task_environment_.FastForwardBy(base::Hours(1));
}
TEST_F(ZombieHostDetectorTest, NoEventAfterComingBackOnline_Noop) {
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_NONE);
task_environment_.FastForwardBy(base::Hours(1));
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
task_environment_.FastForwardBy(GetNextDetectionDurationSinceNow() +
kFastForwardDelta);
task_environment_.FastForwardBy(base::Hours(1));
}
TEST_F(ZombieHostDetectorTest, NeitherIsCurrentWhenJustComeBackOnline_Noop) {
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_NONE);
task_environment_.FastForwardBy(base::Hours(1));
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
task_environment_.FastForwardBy(GetNextDetectionDurationSinceNow() +
kFastForwardDelta);
}
TEST_F(ZombieHostDetectorTest, NotCurrentAfterComingBackOnline_CallbackCalled) {
zombie_host_detector_.OnHeartbeatSent();
zombie_host_detector_.OnSignalingActive();
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_NONE);
task_environment_.FastForwardBy(base::Hours(1));
network_change_notifier_->SetConnectionType(
net::NetworkChangeNotifier::CONNECTION_ETHERNET);
task_environment_.FastForwardBy(GetNextDetectionDurationSinceNow() +
kFastForwardDelta);
FastForwardUntilZombieStateDetected(
ZombieHostDetector::kMaxHeartbeatInterval - kFastForwardDelta,
base::DoNothing()); // Not reporting any event.
}
} // namespace remoting
|