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

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: Review fixes 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 "testing/gtest/include/gtest/gtest.h"
13
14 namespace resource_coordinator {
15
16 namespace {
17
18 class CoordinationUnitImplTest : public testing::Test {
19 public:
20 CoordinationUnitImplTest() : provider_(nullptr) {}
21 ~CoordinationUnitImplTest() override {}
22
23 // testing::Test:
24 void TearDown() override { base::RunLoop().RunUntilIdle(); }
25
26 protected:
27 CoordinationUnitProviderImpl* provider() { return &provider_; }
28
29 private:
30 base::MessageLoop message_loop_;
31
32 CoordinationUnitProviderImpl provider_;
33 };
34
35 class TestCoordinationUnit : public mojom::PolicyCallback {
36 public:
37 TestCoordinationUnit(CoordinationUnitProviderImpl* provider,
38 const CoordinationUnitType& type,
39 const std::string& id)
40 : binding_(this), id_(type, id) {
41 CHECK(provider);
42
43 mojom::CoordinationUnitPtr coordination_unit;
44 provider->CreateCoordinationUnit(mojo::MakeRequest(&coordination_unit_),
45 id_);
46
47 base::RunLoop policy_callback;
48 SetPolicyClosure(policy_callback.QuitClosure());
49 coordination_unit_->SetPolicyCallback(GetPolicyCallback());
50 // Forces us to wait for the creation of the CUID to finish.
51 policy_callback.Run();
52 }
53
54 void SetPolicyClosure(const base::Closure& policy_closure) {
55 policy_update_closure_ = policy_closure;
56 }
57
58 mojom::PolicyCallbackPtr GetPolicyCallback() {
59 return binding_.CreateInterfacePtrAndBind();
60 }
61
62 // The CU will always send policy updates on events (including parent events)
63 void ForcePolicyUpdates() {
64 base::RunLoop callback;
65 SetPolicyClosure(callback.QuitClosure());
66 mojom::EventPtr event = mojom::Event::New();
67 event->type = EventType::TEST_EVENT;
68 coordination_unit_->SendEvent(std::move(event));
69 callback.Run();
70 }
71
72 const mojom::CoordinationUnitPtr& interface() const {
73 return coordination_unit_;
74 }
75
76 const CoordinationUnitID& id() const { return id_; }
77
78 // mojom::PolicyCallback:
79 void SetPolicy(resource_coordinator::mojom::PolicyPtr policy) override {
80 policy_update_closure_.Run();
81 }
82
83 private:
84 base::Closure policy_update_closure_;
85
86 mojo::Binding<mojom::PolicyCallback> binding_;
87 mojom::CoordinationUnitPtr coordination_unit_;
88 CoordinationUnitID id_;
89 };
90
91 } // namespace
92
93 TEST_F(CoordinationUnitImplTest, BasicPolicyCallback) {
94 TestCoordinationUnit test_coordination_unit(
95 provider(), CoordinationUnitType::WEBCONTENTS, "test_id");
96 test_coordination_unit.ForcePolicyUpdates();
97 }
98
99 TEST_F(CoordinationUnitImplTest, AddChild) {
100 TestCoordinationUnit parent_unit(
101 provider(), CoordinationUnitType::WEBCONTENTS, "parent_unit");
102
103 TestCoordinationUnit child_unit(provider(), CoordinationUnitType::WEBCONTENTS,
104 "child_unit");
105
106 child_unit.ForcePolicyUpdates();
107 parent_unit.ForcePolicyUpdates();
108
109 {
110 base::RunLoop callback;
111 child_unit.SetPolicyClosure(callback.QuitClosure());
112 parent_unit.interface()->AddChild(child_unit.id());
113 callback.Run();
114 }
115
116 {
117 base::RunLoop parent_callback;
118 base::RunLoop child_callback;
119 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
120 child_unit.SetPolicyClosure(child_callback.QuitClosure());
121
122 // This event should force the policy to recalculated for all children.
123 mojom::EventPtr event = mojom::Event::New();
124 event->type = EventType::TEST_EVENT;
125 parent_unit.interface()->SendEvent(std::move(event));
126
127 parent_callback.Run();
128 child_callback.Run();
129 }
130 }
131
132 TEST_F(CoordinationUnitImplTest, CyclicGraphUnits) {
133 TestCoordinationUnit parent_unit(
134 provider(), CoordinationUnitType::WEBCONTENTS, "parent_unit");
135
136 TestCoordinationUnit child_unit(provider(), CoordinationUnitType::WEBCONTENTS,
137 "child_unit");
138
139 child_unit.ForcePolicyUpdates();
140 parent_unit.ForcePolicyUpdates();
141
142 {
143 base::RunLoop callback;
144 child_unit.SetPolicyClosure(callback.QuitClosure());
145 parent_unit.interface()->AddChild(child_unit.id());
146 callback.Run();
147 }
148
149 // This should fail, due to the existing child-parent relationship.
150 // Otherwise we end up with infinite recursion and crash when recalculating
151 // policies below.
152 child_unit.interface()->AddChild(parent_unit.id());
153
154 {
155 base::RunLoop parent_callback;
156 base::RunLoop child_callback;
157 parent_unit.SetPolicyClosure(parent_callback.QuitClosure());
158 child_unit.SetPolicyClosure(child_callback.QuitClosure());
159
160 // This event should force the policy to recalculated for all children.
161 mojom::EventPtr event = mojom::Event::New();
162 event->type = EventType::TEST_EVENT;
163 parent_unit.interface()->SendEvent(std::move(event));
164
165 parent_callback.Run();
166 child_callback.Run();
167 }
168 }
169
170 } // namespace resource_coordinator
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698