thread_health_checker_unittest.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2017 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 "chromecast/base/thread_health_checker.h"
  5. #include "base/bind.h"
  6. #include "base/memory/ref_counted.h"
  7. #include "base/synchronization/waitable_event.h"
  8. #include "base/test/test_mock_time_task_runner.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace chromecast {
  11. namespace {
  12. const base::TimeDelta kInterval = base::Seconds(3);
  13. const base::TimeDelta kTimeout = base::Seconds(2);
  14. } // namespace
  15. class ThreadHealthCheckerTest : public ::testing::Test {
  16. protected:
  17. ThreadHealthCheckerTest()
  18. : patient_(base::MakeRefCounted<base::TestMockTimeTaskRunner>()),
  19. doctor_(base::MakeRefCounted<base::TestMockTimeTaskRunner>()),
  20. event_(base::WaitableEvent::ResetPolicy::MANUAL,
  21. base::WaitableEvent::InitialState::NOT_SIGNALED) {}
  22. ~ThreadHealthCheckerTest() override {}
  23. scoped_refptr<base::TestMockTimeTaskRunner> patient_;
  24. scoped_refptr<base::TestMockTimeTaskRunner> doctor_;
  25. base::WaitableEvent event_;
  26. };
  27. #define CREATE_THREAD_HEALTH_CHECKER(name) \
  28. ThreadHealthChecker name(patient_, doctor_, kInterval, kTimeout, \
  29. base::BindRepeating(&base::WaitableEvent::Signal, \
  30. base::Unretained(&event_)))
  31. TEST_F(ThreadHealthCheckerTest, FiresTimeoutWhenTaskRunnerDoesNotFlush) {
  32. CREATE_THREAD_HEALTH_CHECKER(thc);
  33. // Do not flush the patient, so that the health check sentinel task won't run.
  34. doctor_->FastForwardBy(base::Seconds(6));
  35. EXPECT_TRUE(event_.IsSignaled());
  36. }
  37. TEST_F(ThreadHealthCheckerTest, DoesNotFireTimeoutWhenTaskRunnerFlushesInTime) {
  38. CREATE_THREAD_HEALTH_CHECKER(thc);
  39. // Advance the doctor by enough time to post the health check, but not to time
  40. // out.
  41. doctor_->FastForwardBy(base::Seconds(4));
  42. // Advance the patient to let the sentinel task run.
  43. patient_->FastForwardBy(base::Seconds(4));
  44. // Advance the doctor by enough time such that the sentinel not running would
  45. // cause the failure callback to run.
  46. doctor_->FastForwardBy(base::Seconds(2));
  47. EXPECT_FALSE(event_.IsSignaled());
  48. }
  49. TEST_F(ThreadHealthCheckerTest, FiresTimeoutWhenTaskRunnerFlushesTooLate) {
  50. CREATE_THREAD_HEALTH_CHECKER(thc);
  51. // Advance the doctor before the patient, to simulate a task in the patient
  52. // that takes too long.
  53. doctor_->FastForwardBy(base::Seconds(6));
  54. patient_->FastForwardBy(base::Seconds(6));
  55. // Flush the task runner so the health check sentinel task is run.
  56. EXPECT_TRUE(event_.IsSignaled());
  57. }
  58. TEST_F(ThreadHealthCheckerTest, FiresTimeoutOnLaterIteration) {
  59. CREATE_THREAD_HEALTH_CHECKER(thc);
  60. // Advance the doctor enough to start the check.
  61. doctor_->FastForwardBy(base::Seconds(4));
  62. // Advance the patient enough to run the task.
  63. patient_->FastForwardBy(base::Seconds(4));
  64. // Advance the doctor enough to start the check again.
  65. doctor_->FastForwardBy(base::Seconds(4));
  66. EXPECT_FALSE(event_.IsSignaled());
  67. // Advance the doctor enough for the timeout from the second check to fire.
  68. doctor_->FastForwardBy(base::Seconds(2));
  69. EXPECT_TRUE(event_.IsSignaled());
  70. }
  71. TEST_F(ThreadHealthCheckerTest, NoCrashWhenDestroyed) {
  72. {
  73. CREATE_THREAD_HEALTH_CHECKER(thc);
  74. doctor_->RunUntilIdle();
  75. }
  76. doctor_->RunUntilIdle();
  77. }
  78. TEST_F(ThreadHealthCheckerTest, DropPendingEventsAfterDestruction) {
  79. {
  80. CREATE_THREAD_HEALTH_CHECKER(thc);
  81. // Fast forward by enough time to have scheduled a health check.
  82. doctor_->FastForwardBy(base::Seconds(4));
  83. EXPECT_FALSE(event_.IsSignaled());
  84. }
  85. // Fast forward by enough time for the health check to have executed.
  86. // However, we want all pending events to be dropped after the destructor is
  87. // called, so the event should not be signalled.
  88. doctor_->FastForwardBy(base::Seconds(2));
  89. EXPECT_FALSE(event_.IsSignaled());
  90. }
  91. } // namespace chromecast