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

Side by Side Diff: media/base/android/media_service_throttler_unittest.cc

Issue 2471903002: Add MediaServiceThrottler (Closed)
Patch Set: Adressed comments Created 4 years, 1 month 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
« no previous file with comments | « media/base/android/media_service_throttler.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 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 "media/base/android/media_service_throttler.h"
6
7 #include "base/message_loop/message_loop.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "media/base/android/media_server_crash_listener.h"
10 #include "media/base/fake_single_thread_task_runner.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace media {
15
16 // From base/media/android/media_service_throttler.cc
17 const int kMaxBurstClients = 10;
18
19 class MediaServiceThrottlerTest : public testing::Test {
20 public:
21 MediaServiceThrottlerTest() : clock_(new base::SimpleTestTickClock()) {
22 throttler_ = MediaServiceThrottler::GetInstance();
23 clock_->SetNowTicks(base::TimeTicks());
24 throttler_->SetTickClockForTesting(clock_);
25 test_task_runner_ =
26 make_scoped_refptr(new FakeSingleThreadTaskRunner(clock_));
27 throttler_->ResetInternalStateForTesting();
28 throttler_->SetCrashListenerTaskRunnerForTesting(test_task_runner_);
29 base_delay_ = throttler_->GetBaseThrottlingRateForTesting();
30 }
31
32 void SimulateCrashes(int number_of_crashes) {
33 for (int i = 0; i < number_of_crashes; ++i)
34 throttler_->OnMediaServerCrash(false);
35 }
36
37 // Simulates the scheduling of |number_of_clients| and returns the last
38 // scheduling delay.
39 base::TimeDelta SimulateClientCreations(int number_of_clients) {
40 for (int i = 0; i < number_of_clients - 1; ++i)
41 throttler_->GetDelayForClientCreation();
42
43 return throttler_->GetDelayForClientCreation();
44 }
45
46 base::TimeDelta GetCurrentDelayBetweenClients() {
47 // Schedule two clients and return the difference between their scheduling
48 // slots.
49 return -(throttler_->GetDelayForClientCreation() -
50 throttler_->GetDelayForClientCreation());
51 }
52
53 base::TimeTicks TestNow() { return clock_->NowTicks(); }
54
55 MediaServiceThrottler* throttler_;
56 base::SimpleTestTickClock* clock_;
57
58 base::TimeDelta base_delay_;
59
60 scoped_refptr<FakeSingleThreadTaskRunner> test_task_runner_;
61
62 // Necessary, or else base::ThreadTaskRunnerHandle::Get() fails.
63 base::MessageLoop message_loop_;
64
65 private:
66 DISALLOW_COPY_AND_ASSIGN(MediaServiceThrottlerTest);
67 };
68
69 // Canary test case.
70 TEST_F(MediaServiceThrottlerTest, BaseCase) {
71 EXPECT_EQ(base::TimeDelta(), throttler_->GetDelayForClientCreation());
72 }
73
74 // Makes sure we can "burst" schedule clients.
75 TEST_F(MediaServiceThrottlerTest, NoCrash_UnderBurstThreshold_ShouldNotDelay) {
76 int number_burst_client_scheduled = 0;
77
78 while (base::TimeDelta() == throttler_->GetDelayForClientCreation())
79 number_burst_client_scheduled++;
80
81 EXPECT_EQ(kMaxBurstClients, number_burst_client_scheduled);
82 }
83
84 // Makes sure that, when burst scheduling a client, we schedule it from
85 // last scheduled burst client (vs scheduling it |base_delay_| from now).
86 TEST_F(MediaServiceThrottlerTest, NoCrash_OverBurstThreshold_ShouldDelay) {
87 // Schedule clients until the next one would be over burst threshold.
88 SimulateClientCreations(kMaxBurstClients);
89
90 // Make sure the next client that is scheduled is scheduled in its normal time
91 // slot.
92 EXPECT_EQ(base_delay_ * (kMaxBurstClients + 1),
93 throttler_->GetDelayForClientCreation());
94 }
95
96 // Makes sure that clients are scheduled |base_delay| apart.
97 TEST_F(MediaServiceThrottlerTest,
98 NoCrash_OverBurstThreshold_ShouldDelayLinearly) {
99 // Schedule clients until the next one would be over burst threshold.
100 SimulateClientCreations(kMaxBurstClients);
101
102 // Delays between two clients should be |base_delay_| appart.
103 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
104
105 // Delays should remain constant (GetCurrentDelayBetweenClients() is not
106 // idempotent and actually schedules new clients).
107 EXPECT_EQ(GetCurrentDelayBetweenClients(), GetCurrentDelayBetweenClients());
108 }
109
110 // Makes sure that for every |base_delay_| that has elapsed, we can burst
111 // schedule an extra client.
112 TEST_F(MediaServiceThrottlerTest,
113 NoCrash_BurstThreshold_ShouldBeSlidingWindow) {
114 // Schedule some clients below the burst threshold.
115 SimulateClientCreations(7);
116
117 clock_->Advance(base_delay_ * 5);
118
119 // Make sure the passage of allows for more clients to be scheduled, since
120 // 7 + 8 > kMaxBurstClients.
121 EXPECT_EQ(base::TimeDelta(), SimulateClientCreations(8));
122 }
123
124 // Makes sure that, if not enough time has elapsed, we do not burst schedule
125 // new clients.
126 TEST_F(MediaServiceThrottlerTest,
127 NoCrash_OverBurstThresholdWithTimeLapse_ShouldDelay) {
128 // Schedule some clients way above the burst threshold.
129 SimulateClientCreations(3 * kMaxBurstClients);
130
131 // Advance the time so there are still 2 * kMaxBurstClients pending clients.
132 clock_->Advance(base_delay_ * kMaxBurstClients);
133
134 // Make sure delay we do not burst schedule new clients.
135 EXPECT_NE(base::TimeDelta(), throttler_->GetDelayForClientCreation());
136 }
137
138 // Makes sure that after a certain amount of inactivity, the scheduling clock is
139 // reset.
140 TEST_F(MediaServiceThrottlerTest, NoCrash_LongInactivity_ShouldReset) {
141 // Schedule two minute's worth of clients
142 SimulateClientCreations(base::TimeDelta::FromMinutes(2) / base_delay_);
143
144 // Advance the time so the scheduler perceived a full minute of inactivity.
145 clock_->Advance(base::TimeDelta::FromSeconds(61));
146
147 // Make sure new clients are burst scheduled.
148 EXPECT_EQ(base::TimeDelta(), throttler_->GetDelayForClientCreation());
149 }
150
151 // Makes sure that crashes increase the scheduling delay.
152 TEST_F(MediaServiceThrottlerTest, WithCrash_BaseCase_DelayShouldIncrease) {
153 // Schedule clients until the next one would be over burst threshold.
154 SimulateClientCreations(kMaxBurstClients);
155
156 base::TimeDelta no_crash_delay = GetCurrentDelayBetweenClients();
157
158 SimulateCrashes(1);
159
160 base::TimeDelta crash_delay = GetCurrentDelayBetweenClients();
161
162 EXPECT_NE(base_delay_, crash_delay);
163 EXPECT_GT(crash_delay, no_crash_delay);
164 }
165
166 // Makes sure that we tolerate 1 crash per minute.
167 TEST_F(MediaServiceThrottlerTest,
168 WithCrash_SingleCrash_DelayShouldNotIncrease) {
169 // Schedule clients until the next one would be over burst threshold.
170 SimulateClientCreations(kMaxBurstClients);
171
172 SimulateCrashes(1);
173 clock_->Advance(base::TimeDelta::FromMilliseconds(1));
174
175 // Because we use the floor function when calculating crashes, a small time
176 // advance should nullify a single crash.
177 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
178 }
179
180 // Makes sure that more than 1 crash per minute causes increased delays.
181 TEST_F(MediaServiceThrottlerTest, WithCrash_ManyCrashes_DelayShouldIncrease) {
182 // Schedule clients until the next one would be over burst threshold.
183 SimulateClientCreations(kMaxBurstClients);
184
185 SimulateCrashes(2);
186 clock_->Advance(base::TimeDelta::FromMilliseconds(1));
187
188 // The delay after crashes should be greater than the base delay.
189 EXPECT_LT(base_delay_, GetCurrentDelayBetweenClients());
190 }
191
192 // Makes sure that an increase in server crashes leads to delay increases.
193 TEST_F(MediaServiceThrottlerTest,
194 WithCrash_ConsecutiveCrashes_DelayShouldIncreaseWithNumberOfCrashes) {
195 // Schedule clients until the next one would be over burst threshold.
196 SimulateClientCreations(kMaxBurstClients);
197
198 base::TimeDelta last_delay = base_delay_;
199
200 for (int i = 0; i < 5; ++i) {
201 SimulateCrashes(1);
202 base::TimeDelta current_delay = GetCurrentDelayBetweenClients();
203 EXPECT_LT(last_delay, current_delay);
204 last_delay = current_delay;
205 }
206 }
207
208 // Makes sure that crashes affect the number of burst clients we can schedule.
209 TEST_F(MediaServiceThrottlerTest, WithCrash_ShouldAllowFewerBurstClients) {
210 int number_burst_client_scheduled = 0;
211
212 SimulateCrashes(1);
213
214 while (base::TimeDelta() == throttler_->GetDelayForClientCreation())
215 number_burst_client_scheduled++;
216
217 EXPECT_GT(kMaxBurstClients, number_burst_client_scheduled);
218 }
219
220 // Makes sure delays are capped to a certain maximal value.
221 TEST_F(MediaServiceThrottlerTest, WithCrash_ManyCrashes_DelayShouldMaxOut) {
222 // Schedule clients until the next one would be over burst threshold.
223 SimulateClientCreations(kMaxBurstClients);
224
225 SimulateCrashes(10);
226 base::TimeDelta capped_delay = GetCurrentDelayBetweenClients();
227
228 SimulateCrashes(10);
229
230 EXPECT_EQ(capped_delay, GetCurrentDelayBetweenClients());
231 }
232
233 // Makes sure a minute without crashes resets the crash counter.
234 TEST_F(MediaServiceThrottlerTest, WithCrash_NoCrashesForAMinute_ShouldReset) {
235 SimulateCrashes(10);
236
237 // The effective server crash count should be reset because it has been over
238 // a minute since the last crash.
239 clock_->Advance(base::TimeDelta::FromSeconds(61));
240
241 SimulateClientCreations(kMaxBurstClients);
242
243 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
244 }
245
246 // Makes sure a steady crashes do not resets the crash counter.
247 TEST_F(MediaServiceThrottlerTest, WithCrash_ConstantCrashes_ShouldNotReset) {
248 SimulateCrashes(9);
249
250 // The effective server crash count should not be reset.
251 clock_->Advance(base::TimeDelta::FromSeconds(59));
252 SimulateCrashes(1);
253 clock_->Advance(base::TimeDelta::FromSeconds(2));
254
255 SimulateClientCreations(kMaxBurstClients);
256
257 EXPECT_LT(base_delay_, GetCurrentDelayBetweenClients());
258 }
259
260 // Makes sure the crash listener shuts down after a minute of not having
261 // received any client creation request.
262 TEST_F(MediaServiceThrottlerTest, CrashListener_NoRequests_ShouldShutDown) {
263 // Schedule many minutes worth of clients. This is to prove that the
264 // MediaServerCrashListener's clean up happens after lack of requests, as
265 // opposed to lack of actually scheduled clients.
266 SimulateClientCreations(base::TimeDelta::FromMinutes(3) / base_delay_);
267
268 // The MediaServerCrashListener should be alive, with 1s second to spare.
269 clock_->Advance(base::TimeDelta::FromSeconds(59));
270 test_task_runner_->RunTasks();
271 EXPECT_TRUE(throttler_->IsCrashListenerAliveForTesting());
272
273 // Requesting a new client creation should reset the interal timer, and
274 // cancel the release request that was scheduled 59 seconds ago.
275 throttler_->GetDelayForClientCreation();
276
277 // The MediaServerCrashListener should be alive, with 58s second to spare.
278 clock_->Advance(base::TimeDelta::FromSeconds(2));
279 test_task_runner_->RunTasks();
280 EXPECT_TRUE(throttler_->IsCrashListenerAliveForTesting());
281
282 // The MediaServerCrashListener should be dead.
283 clock_->Advance(base::TimeDelta::FromSeconds(59));
284 test_task_runner_->RunTasks();
285 EXPECT_FALSE(throttler_->IsCrashListenerAliveForTesting());
286 }
287
288 // Makes sure the crash listener shuts down after a minute of not having
289 // received any client creation request, regardless of when crashes occur.
290 TEST_F(MediaServiceThrottlerTest,
291 CrashListener_NoRequestsWithCrashes_ShouldShutDown) {
292 // Schedule many minutes worth of clients. This is to prove that the
293 // MediaServerCrashListener's clean up happens after lack of requests, as
294 // opposed to lack of actually scheduled clients.
295 SimulateClientCreations(base::TimeDelta::FromMinutes(3) / base_delay_);
296
297 // The MediaServerCrashListener should be alive, with 1s second to spare.
298 clock_->Advance(base::TimeDelta::FromSeconds(59));
299 test_task_runner_->RunTasks();
300 EXPECT_TRUE(throttler_->IsCrashListenerAliveForTesting());
301
302 // Crashes should not affect the MediaServerCrashListener's lifetime.
303 SimulateCrashes(1);
304
305 // The MediaServerCrashListener should be dead.
306 clock_->Advance(base::TimeDelta::FromSeconds(2));
307 test_task_runner_->RunTasks();
308 EXPECT_FALSE(throttler_->IsCrashListenerAliveForTesting());
309 }
310
311 } // namespace media
OLDNEW
« no previous file with comments | « media/base/android/media_service_throttler.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698