override_vote_aggregator_unittest.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright 2019 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/performance_manager/execution_context_priority/override_vote_aggregator.h"
  5. #include "components/performance_manager/test_support/voting.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. namespace performance_manager {
  8. namespace execution_context_priority {
  9. using DummyVoteObserver = voting::test::DummyVoteObserver<Vote>;
  10. // Some dummy execution contexts.
  11. const ExecutionContext* kExecutionContext0 =
  12. reinterpret_cast<const ExecutionContext*>(0xDEADBEEF);
  13. const ExecutionContext* kExecutionContext1 =
  14. reinterpret_cast<const ExecutionContext*>(0xBAADF00D);
  15. static const Vote kLowPriorityVote0(base::TaskPriority::LOWEST, "low reason 0");
  16. static const Vote kLowPriorityVote1(base::TaskPriority::LOWEST, "low reason 1");
  17. static const Vote kMediumPriorityVote0(base::TaskPriority::USER_VISIBLE,
  18. "medium reason 0");
  19. static const Vote kMediumPriorityVote1(base::TaskPriority::USER_VISIBLE,
  20. "medium reason 1");
  21. static const Vote kHighPriorityVote0(base::TaskPriority::HIGHEST,
  22. "high reason 0");
  23. static const Vote kHighPriorityVote1(base::TaskPriority::HIGHEST,
  24. "high reason 1");
  25. class OverrideVoteAggregatorTest : public testing::Test {
  26. public:
  27. OverrideVoteAggregatorTest() = default;
  28. ~OverrideVoteAggregatorTest() override = default;
  29. void SetUp() override {
  30. VotingChannel channel = observer_.BuildVotingChannel();
  31. aggregator_voter_id_ = channel.voter_id();
  32. aggregator_.SetUpstreamVotingChannel(std::move(channel));
  33. }
  34. void TearDown() override {}
  35. VoterId aggregator_voter_id() const { return aggregator_voter_id_; }
  36. const DummyVoteObserver& observer() const { return observer_; }
  37. OverrideVoteAggregator* aggregator() { return &aggregator_; }
  38. void TestSingleVoter(VotingChannel* voter) {
  39. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  40. voter->SubmitVote(kExecutionContext0, kLowPriorityVote0);
  41. EXPECT_EQ(observer().GetVoteCount(), 1u);
  42. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  43. kLowPriorityVote0));
  44. // Change only the reason.
  45. voter->ChangeVote(kExecutionContext0, kLowPriorityVote1);
  46. EXPECT_EQ(observer().GetVoteCount(), 1u);
  47. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  48. kLowPriorityVote1));
  49. // Change the priority.
  50. voter->ChangeVote(kExecutionContext0, kHighPriorityVote0);
  51. EXPECT_EQ(observer().GetVoteCount(), 1u);
  52. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  53. kHighPriorityVote0));
  54. // Add a vote for a different execution context.
  55. voter->SubmitVote(kExecutionContext1, kMediumPriorityVote0);
  56. EXPECT_EQ(observer().GetVoteCount(), 2u);
  57. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  58. kHighPriorityVote0));
  59. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext1,
  60. kMediumPriorityVote0));
  61. voter->ChangeVote(kExecutionContext1, kHighPriorityVote1);
  62. EXPECT_EQ(observer().GetVoteCount(), 2u);
  63. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  64. kHighPriorityVote0));
  65. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext1,
  66. kHighPriorityVote1));
  67. // Invalidate vote for the first execution context.
  68. voter->InvalidateVote(kExecutionContext0);
  69. EXPECT_EQ(observer().GetVoteCount(), 1u);
  70. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  71. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext1,
  72. kHighPriorityVote1));
  73. voter->InvalidateVote(kExecutionContext1);
  74. EXPECT_EQ(observer().GetVoteCount(), 0u);
  75. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  76. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  77. }
  78. private:
  79. DummyVoteObserver observer_;
  80. OverrideVoteAggregator aggregator_;
  81. VoterId aggregator_voter_id_;
  82. };
  83. // Tests that in the case of a single voter, the vote is simply propagated
  84. // upwards.
  85. TEST_F(OverrideVoteAggregatorTest, SingleVoter) {
  86. VotingChannel default_voter = aggregator()->GetDefaultVotingChannel();
  87. VotingChannel override_voter = aggregator()->GetOverrideVotingChannel();
  88. TestSingleVoter(&default_voter);
  89. TestSingleVoter(&override_voter);
  90. }
  91. TEST_F(OverrideVoteAggregatorTest, OneContext) {
  92. VotingChannel default_voter = aggregator()->GetDefaultVotingChannel();
  93. VotingChannel override_voter = aggregator()->GetOverrideVotingChannel();
  94. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  95. // Submit a default vote for the execution context.
  96. default_voter.SubmitVote(kExecutionContext0, kMediumPriorityVote0);
  97. EXPECT_EQ(observer().GetVoteCount(), 1u);
  98. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  99. kMediumPriorityVote0));
  100. // Submit an override vote. The override vote will always be upstreamed,
  101. // regardless of the priority.
  102. override_voter.SubmitVote(kExecutionContext0, kLowPriorityVote1);
  103. EXPECT_EQ(observer().GetVoteCount(), 1u);
  104. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  105. kLowPriorityVote1));
  106. // Change the override vote. The upstream will also be changed.
  107. override_voter.ChangeVote(kExecutionContext0, kHighPriorityVote1);
  108. EXPECT_EQ(observer().GetVoteCount(), 1u);
  109. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  110. kHighPriorityVote1));
  111. // Change the default vote. The upstream will not change, but the default vote
  112. // will be remembered.
  113. default_voter.ChangeVote(kExecutionContext0, kHighPriorityVote0);
  114. EXPECT_EQ(observer().GetVoteCount(), 1u);
  115. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  116. kHighPriorityVote1));
  117. // Invalidate the override vote. The upstream will change to the default vote.
  118. override_voter.InvalidateVote(kExecutionContext0);
  119. EXPECT_EQ(observer().GetVoteCount(), 1u);
  120. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  121. kHighPriorityVote0));
  122. // Cleanup.
  123. default_voter.InvalidateVote(kExecutionContext0);
  124. EXPECT_EQ(observer().GetVoteCount(), 0u);
  125. EXPECT_FALSE(observer().HasVote(aggregator_voter_id(), kExecutionContext0));
  126. }
  127. // A less extensive test than OneContext that sanity checks that votes for
  128. // different contexts are aggregated independently.
  129. TEST_F(OverrideVoteAggregatorTest, MultipleContexts) {
  130. VotingChannel default_voter = aggregator()->GetDefaultVotingChannel();
  131. VotingChannel override_voter = aggregator()->GetOverrideVotingChannel();
  132. // Vote for execution context 1. The override vote lowers the priority of the
  133. // upstreamed vote.
  134. default_voter.SubmitVote(kExecutionContext0, kHighPriorityVote0);
  135. override_voter.SubmitVote(kExecutionContext0, kMediumPriorityVote1);
  136. // Vote for execution context 2. The override vote increases the priority of
  137. // the upstreamed vote.
  138. default_voter.SubmitVote(kExecutionContext1, kLowPriorityVote0);
  139. override_voter.SubmitVote(kExecutionContext1, kHighPriorityVote1);
  140. // There is an aggregated vote for each context, and their values are coming
  141. // from the override voter.
  142. EXPECT_EQ(observer().GetVoteCount(), 2u);
  143. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext0,
  144. kMediumPriorityVote1));
  145. EXPECT_TRUE(observer().HasVote(aggregator_voter_id(), kExecutionContext1,
  146. kHighPriorityVote1));
  147. // Cleanup.
  148. default_voter.InvalidateVote(kExecutionContext0);
  149. default_voter.InvalidateVote(kExecutionContext1);
  150. override_voter.InvalidateVote(kExecutionContext0);
  151. override_voter.InvalidateVote(kExecutionContext1);
  152. EXPECT_EQ(observer().GetVoteCount(), 0u);
  153. }
  154. } // namespace execution_context_priority
  155. } // namespace performance_manager