Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(145)

Side by Side Diff: services/resource_coordinator/coordination_unit/coordination_unit_impl_unittest.cc

Issue 2798713002: Global Resource Coordinator: Basic service internals (Closed)
Patch Set: Use MurmurHash64A instead of MurmurHash2 now that id is 64bit Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include <memory>
6 #include <vector>
7
8 #include "base/bind.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h"
11 #include "services/resource_coordinator/coordination_unit/coordination_unit_prov ider_impl.h"
12 #include "services/service_manager/public/cpp/service_context_ref.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace resource_coordinator {
16
17 namespace {
18
19 void OnLastServiceRefDestroyed() {
20 // No-op. This is required by service_manager::ServiceContextRefFactory
21 // construction but not needed for the tests.
22 }
23
24 class CoordinationUnitImplTest : public testing::Test {
25 public:
26 CoordinationUnitImplTest()
27 : service_ref_factory_(base::Bind(&OnLastServiceRefDestroyed)),
28 provider_(&service_ref_factory_) {}
29 ~CoordinationUnitImplTest() override {}
30
31 // testing::Test:
32 void TearDown() override { base::RunLoop().RunUntilIdle(); }
33
34 protected:
35 CoordinationUnitProviderImpl* provider() { return &provider_; }
36
37 private:
38 base::MessageLoop message_loop_;
39
40 service_manager::ServiceContextRefFactory service_ref_factory_;
41 CoordinationUnitProviderImpl provider_;
42 };
43
44 class TestCoordinationUnit : public mojom::PolicyCallback {
45 public:
46 TestCoordinationUnit(CoordinationUnitProviderImpl* provider,
47 const CoordinationUnitType& type,
48 const std::string& id)
49 : binding_(this) {
50 CHECK(provider);
51
52 CoordinationUnitID new_cu_id(type, id);
53 mojom::CoordinationUnitPtr coordination_unit;
54 provider->CreateCoordinationUnit(mojo::MakeRequest(&coordination_unit_),
55 new_cu_id);
56
57 base::RunLoop callback;
58 SetGetIDClosure(callback.QuitClosure());
59 coordination_unit_->SetPolicyCallback(GetPolicyCallback());
60 // Forces us to wait for the creation of the CUID to finish.
61 coordination_unit_->GetID(base::Bind(&TestCoordinationUnit::GetIDCallback,
62 base::Unretained(this)));
63
64 callback.Run();
65 }
66
67 void GetIDCallback(const CoordinationUnitID& cu_id) {
68 id_ = cu_id;
69 get_id_closure_.Run();
70 }
71
72 void SetGetIDClosure(const base::Closure& get_id_closure) {
73 get_id_closure_ = get_id_closure;
74 }
75
76 void SetPolicyClosure(const base::Closure& policy_closure) {
77 policy_update_closure_ = policy_closure;
78 }
79
80 mojom::PolicyCallbackPtr GetPolicyCallback() {
81 return binding_.CreateInterfacePtrAndBind();
82 }
83
84 // The CU will always send policy updates on events (including parent events)
85 void ForcePolicyUpdates() {
86 base::RunLoop callback;
87 SetPolicyClosure(callback.QuitClosure());
88 mojom::EventPtr event = mojom::Event::New();
89 event->type = mojom::EventType::kTestEvent;
90 coordination_unit_->SendEvent(std::move(event));
91 callback.Run();
92 }
93
94 const mojom::CoordinationUnitPtr& interface() const {
95 return coordination_unit_;
96 }
97
98 const CoordinationUnitID& id() const { return id_; }
99
100 // mojom::PolicyCallback:
101 void SetPolicy(resource_coordinator::mojom::PolicyPtr policy) override {
102 if (policy_update_closure_)
103 policy_update_closure_.Run();
104 }
105
106 private:
107 base::Closure policy_update_closure_;
108 base::Closure get_id_closure_;
109
110 mojo::Binding<mojom::PolicyCallback> binding_;
111 mojom::CoordinationUnitPtr coordination_unit_;
112 CoordinationUnitID id_;
113 };
114
115 } // namespace
116
117 TEST_F(CoordinationUnitImplTest, BasicPolicyCallback) {
118 TestCoordinationUnit test_coordination_unit(
119 provider(), CoordinationUnitType::kWebContents, "test_id");
120 test_coordination_unit.ForcePolicyUpdates();
121 }
122
123 TEST_F(CoordinationUnitImplTest, AddChild) {
124 TestCoordinationUnit parent_unit(
125 provider(), CoordinationUnitType::kWebContents, "parent_unit");
126
127 TestCoordinationUnit child_unit(
128 provider(), CoordinationUnitType::kWebContents, "child_unit");
129
130 child_unit.ForcePolicyUpdates();
131 parent_unit.ForcePolicyUpdates();
132
133 {
134 base::RunLoop callback;
135 child_unit.SetPolicyClosure(callback.QuitClosure());
136 parent_unit.interface()->AddChild(child_unit.id());
137 callback.Run();
138 }
139
140 {
141 base::RunLoop parent_callback;
142 base::RunLoop child_callback;
143 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
144 child_unit.SetPolicyClosure(child_callback.QuitClosure());
145
146 // This event should force the policy to recalculated for all children.
147 mojom::EventPtr event = mojom::Event::New();
148 event->type = mojom::EventType::kTestEvent;
149 parent_unit.interface()->SendEvent(std::move(event));
150
151 parent_callback.Run();
152 child_callback.Run();
153 }
154 }
155
156 TEST_F(CoordinationUnitImplTest, CyclicGraphUnits) {
157 TestCoordinationUnit parent_unit(
158 provider(), CoordinationUnitType::kWebContents, std::string());
159
160 TestCoordinationUnit child_unit(
161 provider(), CoordinationUnitType::kWebContents, std::string());
162
163 child_unit.ForcePolicyUpdates();
164 parent_unit.ForcePolicyUpdates();
165
166 {
167 base::RunLoop callback;
168 child_unit.SetPolicyClosure(callback.QuitClosure());
169 parent_unit.interface()->AddChild(child_unit.id());
170 callback.Run();
171 }
172
173 // This should fail, due to the existing child-parent relationship.
174 // Otherwise we end up with infinite recursion and crash when recalculating
175 // policies below.
176 child_unit.interface()->AddChild(parent_unit.id());
177
178 {
179 base::RunLoop parent_callback;
180 base::RunLoop child_callback;
181 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
182 child_unit.SetPolicyClosure(child_callback.QuitClosure());
183
184 // This event should force the policy to recalculated for all children.
185 mojom::EventPtr event = mojom::Event::New();
186 event->type = mojom::EventType::kTestEvent;
187 parent_unit.interface()->SendEvent(std::move(event));
188
189 parent_callback.Run();
190 child_callback.Run();
191 }
192 }
193
194 } // namespace resource_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698