system_memory_pressure_evaluator_fuchsia_unittest.cc 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "components/memory_pressure/system_memory_pressure_evaluator_fuchsia.h"
  5. #include <fuchsia/memorypressure/cpp/fidl.h>
  6. #include <fuchsia/memorypressure/cpp/fidl_test_base.h>
  7. #include "base/fuchsia/scoped_service_binding.h"
  8. #include "base/fuchsia/test_component_context_for_process.h"
  9. #include "base/run_loop.h"
  10. #include "base/test/task_environment.h"
  11. #include "components/memory_pressure/multi_source_memory_pressure_monitor.h"
  12. #include "testing/gmock/include/gmock/gmock.h"
  13. #include "testing/gtest/include/gtest/gtest.h"
  14. namespace memory_pressure {
  15. namespace {
  16. class MockMemoryPressureVoter : public MemoryPressureVoter {
  17. public:
  18. MOCK_METHOD2(SetVote,
  19. void(base::MemoryPressureListener::MemoryPressureLevel, bool));
  20. };
  21. class TestSystemMemoryPressureEvaluator
  22. : public SystemMemoryPressureEvaluatorFuchsia {
  23. public:
  24. TestSystemMemoryPressureEvaluator(std::unique_ptr<MemoryPressureVoter> voter)
  25. : SystemMemoryPressureEvaluatorFuchsia(std::move(voter)) {}
  26. TestSystemMemoryPressureEvaluator(const TestSystemMemoryPressureEvaluator&) =
  27. delete;
  28. TestSystemMemoryPressureEvaluator& operator=(
  29. const TestSystemMemoryPressureEvaluator&) = delete;
  30. MOCK_METHOD1(OnMemoryPressure,
  31. void(base::MemoryPressureListener::MemoryPressureLevel level));
  32. };
  33. } // namespace
  34. class SystemMemoryPressureEvaluatorFuchsiaTest
  35. : public testing::Test,
  36. public fuchsia::memorypressure::testing::Provider_TestBase {
  37. public:
  38. SystemMemoryPressureEvaluatorFuchsiaTest()
  39. : task_environment_(base::test::TaskEnvironment::MainThreadType::IO,
  40. base::test::TaskEnvironment::TimeSource::MOCK_TIME) {}
  41. void SendPressureLevel(fuchsia::memorypressure::Level level) {
  42. base::RunLoop wait_loop;
  43. watcher_->OnLevelChanged(
  44. level, [quit_loop = wait_loop.QuitClosure()]() { quit_loop.Run(); });
  45. wait_loop.Run();
  46. }
  47. bool have_watcher() const { return watcher_.is_bound(); }
  48. // fuchsia::memorypressure::Provider implementation.
  49. void RegisterWatcher(fidl::InterfaceHandle<fuchsia::memorypressure::Watcher>
  50. watcher) override {
  51. watcher_.Bind(std::move(watcher));
  52. SendPressureLevel(fuchsia::memorypressure::Level::NORMAL);
  53. }
  54. protected:
  55. // fuchsia::memorypressure::testing::Provider_TestBase implementation.
  56. void NotImplemented_(const std::string& name) override {
  57. ADD_FAILURE() << "Unexpected call to method: " << name;
  58. }
  59. base::test::SingleThreadTaskEnvironment task_environment_;
  60. base::TestComponentContextForProcess test_context_;
  61. fuchsia::memorypressure::WatcherPtr watcher_;
  62. };
  63. using SystemMemoryPressureEvaluatorFuchsiaDeathTest =
  64. SystemMemoryPressureEvaluatorFuchsiaTest;
  65. TEST_F(SystemMemoryPressureEvaluatorFuchsiaDeathTest, ProviderUnavailable) {
  66. auto voter = std::make_unique<MockMemoryPressureVoter>();
  67. TestSystemMemoryPressureEvaluator evaluator(std::move(voter));
  68. // Spin the loop to allow the evaluator to notice that the Provider is not
  69. // available and verify that this causes a fatal failure.
  70. ASSERT_DEATH(base::RunLoop().RunUntilIdle(),
  71. "fuchsia\\.memorypressure\\.Provider disconnected unexpectedly, "
  72. "exiting: ZX_ERR_PEER_CLOSED \\(-24\\)");
  73. }
  74. TEST_F(SystemMemoryPressureEvaluatorFuchsiaTest, Basic) {
  75. base::ScopedServiceBinding<::fuchsia::memorypressure::Provider>
  76. publish_provider(test_context_.additional_services(), this);
  77. auto voter = std::make_unique<MockMemoryPressureVoter>();
  78. // NONE pressure will be reported once the first Watch() call returns, and
  79. // then again when the fakes system level transitions from CRITICAL->NORMAL.
  80. EXPECT_CALL(
  81. *voter,
  82. SetVote(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE, false))
  83. .Times(2);
  84. EXPECT_CALL(
  85. *voter,
  86. SetVote(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL,
  87. true));
  88. EXPECT_CALL(
  89. *voter,
  90. SetVote(base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE,
  91. true));
  92. TestSystemMemoryPressureEvaluator evaluator(std::move(voter));
  93. // Spin the loop to ensure that RegisterWatcher() is processed.
  94. base::RunLoop().RunUntilIdle();
  95. ASSERT_TRUE(have_watcher());
  96. SendPressureLevel(fuchsia::memorypressure::Level::CRITICAL);
  97. EXPECT_EQ(evaluator.current_vote(),
  98. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
  99. SendPressureLevel(fuchsia::memorypressure::Level::NORMAL);
  100. EXPECT_EQ(evaluator.current_vote(),
  101. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
  102. SendPressureLevel(fuchsia::memorypressure::Level::WARNING);
  103. EXPECT_EQ(evaluator.current_vote(),
  104. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
  105. }
  106. TEST_F(SystemMemoryPressureEvaluatorFuchsiaTest, Periodic) {
  107. base::ScopedServiceBinding<fuchsia::memorypressure::Provider>
  108. publish_provider(test_context_.additional_services(), this);
  109. MultiSourceMemoryPressureMonitor monitor;
  110. monitor.ResetSystemEvaluatorForTesting();
  111. testing::StrictMock<TestSystemMemoryPressureEvaluator> evaluator(
  112. monitor.CreateVoter());
  113. // Spin the loop to ensure that RegisterWatcher() is processed.
  114. base::RunLoop().RunUntilIdle();
  115. ASSERT_TRUE(have_watcher());
  116. base::MemoryPressureListener listener(
  117. FROM_HERE,
  118. base::BindRepeating(&TestSystemMemoryPressureEvaluator::OnMemoryPressure,
  119. base::Unretained(&evaluator)));
  120. EXPECT_CALL(
  121. evaluator,
  122. OnMemoryPressure(
  123. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
  124. SendPressureLevel(fuchsia::memorypressure::Level::WARNING);
  125. EXPECT_EQ(evaluator.current_vote(),
  126. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE);
  127. testing::Mock::VerifyAndClearExpectations(&evaluator);
  128. // Verify that MODERATE pressure level is reported periodically.
  129. EXPECT_CALL(
  130. evaluator,
  131. OnMemoryPressure(
  132. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE));
  133. task_environment_.FastForwardBy(evaluator.kRenotifyVotePeriod);
  134. testing::Mock::VerifyAndClearExpectations(&evaluator);
  135. EXPECT_CALL(
  136. evaluator,
  137. OnMemoryPressure(
  138. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL));
  139. SendPressureLevel(fuchsia::memorypressure::Level::CRITICAL);
  140. EXPECT_EQ(evaluator.current_vote(),
  141. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL);
  142. testing::Mock::VerifyAndClearExpectations(&evaluator);
  143. // Verify that CRITICAL pressure level is reported periodically.
  144. EXPECT_CALL(
  145. evaluator,
  146. OnMemoryPressure(
  147. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL));
  148. task_environment_.FastForwardBy(evaluator.kRenotifyVotePeriod);
  149. testing::Mock::VerifyAndClearExpectations(&evaluator);
  150. SendPressureLevel(fuchsia::memorypressure::Level::NORMAL);
  151. EXPECT_EQ(evaluator.current_vote(),
  152. base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE);
  153. // Verify that NONE pressure level is not reported periodically.
  154. task_environment_.FastForwardBy(evaluator.kRenotifyVotePeriod);
  155. testing::Mock::VerifyAndClearExpectations(&evaluator);
  156. }
  157. } // namespace memory_pressure