zombie_host_detector_unittest.cc 5.5 KB

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