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

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

Issue 2471903002: Add MediaServiceThrottler (Closed)
Patch Set: Integrate throttler into MediaPlayerRender 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
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 "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 namespace media {
14
15 // From base/media/android/media_service_throttler.cc
16 const int kMaxBurstClients = 10;
17
18 class MediaServiceThrottlerTest : public testing::Test {
19 public:
20 MediaServiceThrottlerTest() : clock_(new base::SimpleTestTickClock()) {
21 throttler_ = MediaServiceThrottler::GetInstance();
22 clock_->SetNowTicks(base::TimeTicks());
23 throttler_->SetTickClockForTesting(clock_);
24 MediaServerCrashListener::GetInstance()
25 ->ClearOnServerCrashCallbackForTesting();
26 throttler_->ResetInternalStateForTesting();
27 base_delay_ = throttler_->GetBaseThrottlingRateForTesting();
28 }
29
30 void SimulateCrashes(int number_of_crashes) {
31 for (int i = 0; i < number_of_crashes; ++i)
32 throttler_->OnMediaServerCrash();
33 }
34
35 // Simulates the scheduling of |number_of_clients| and returns the last
36 // scheduling delay.
37 base::TimeDelta BatchScheduleClientCreations(int number_of_clients) {
38 for (int i = 0; i < number_of_clients - 1; ++i)
39 throttler_->ScheduleClientCreation();
40
41 return throttler_->ScheduleClientCreation();
42 }
43
44 base::TimeDelta GetCurrentDelayBetweenClients() {
45 // Schedule two clients and return the difference between their scheduling
46 // slots.
47 return -(throttler_->ScheduleClientCreation() -
48 throttler_->ScheduleClientCreation());
49 }
50
51 base::TimeTicks TestNow() { return clock_->NowTicks(); }
52
53 MediaServiceThrottler* throttler_;
54 base::SimpleTestTickClock* clock_;
55
56 base::TimeDelta base_delay_;
57
58 // Necessary, or else base::ThreadTaskRunnerHandle::Get() fails.
59 base::MessageLoop message_loop_;
60
61 private:
62 DISALLOW_COPY_AND_ASSIGN(MediaServiceThrottlerTest);
63 };
64
65 // Canary test case.
66 TEST_F(MediaServiceThrottlerTest, BaseCase) {
67 EXPECT_EQ(base::TimeDelta(), throttler_->ScheduleClientCreation());
68 }
69
70 // Makes sure we can "burst" schedule clients.
71 TEST_F(MediaServiceThrottlerTest, NoCrash_UnderBurstThreshold_ShouldNotDelay) {
72 int number_burst_client_scheduled = 0;
73
74 while (base::TimeDelta() == throttler_->ScheduleClientCreation())
75 number_burst_client_scheduled++;
76
77 EXPECT_EQ(kMaxBurstClients, number_burst_client_scheduled);
78 }
79
80 // Makes sure that, when burst scheduling a client, we schedule it from
81 // last scheduled burst client (vs scheduling it |base_delay_| from now).
82 TEST_F(MediaServiceThrottlerTest, NoCrash_OverBurstThreshold_ShouldDelay) {
83 // Schedule clients until the next one would be over burst threshold.
84 BatchScheduleClientCreations(kMaxBurstClients);
85
86 // Make sure the next client that is scheduled is scheduled in its normal time
87 // slot.
88 EXPECT_EQ(base_delay_ * (kMaxBurstClients + 1),
89 throttler_->ScheduleClientCreation());
90 }
91
92 // Makes sure that clients are scheduled |base_delay| apart.
93 TEST_F(MediaServiceThrottlerTest,
94 NoCrash_OverBurstThreshold_ShouldDelayLinearly) {
95 // Schedule clients until the next one would be over burst threshold.
96 BatchScheduleClientCreations(kMaxBurstClients);
97
98 // Delays between two clients should be |base_delay_| appart.
99 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
100
101 // Delays should remain constant (GetCurrentDelayBetweenClients() is not
102 // idempotent and actually schedules new clients).
103 EXPECT_EQ(GetCurrentDelayBetweenClients(), GetCurrentDelayBetweenClients());
104 }
105
106 // Makes sure that for every |base_delay_| that has elapsed, we can burst
107 // schedule an extra client.
108 TEST_F(MediaServiceThrottlerTest,
109 NoCrash_BurstThreshold_ShouldBeSlidingWindow) {
110 // Schedule some clients below the burst threshold.
111 BatchScheduleClientCreations(7);
112
113 clock_->Advance(base_delay_ * 5);
114
115 // Make sure the passage of allows for more clients to be scheduled, since
116 // 7 + 8 > kMaxBurstClients.
117 EXPECT_EQ(base::TimeDelta(), BatchScheduleClientCreations(8));
118 }
119
120 // Makes sure that, if not enough time has elapsed, we do not burst schedule
121 // new clients.
122 TEST_F(MediaServiceThrottlerTest,
123 NoCrash_OverBurstThresholdWithTimeLapse_ShouldDelay) {
124 // Schedule some clients way above the burst threshold.
125 BatchScheduleClientCreations(3 * kMaxBurstClients);
126
127 // Advance the time so there are still 2 * kMaxBurstClients pending clients.
128 clock_->Advance(base_delay_ * kMaxBurstClients);
129
130 // Make sure delay we do not burst schedule new clients.
131 EXPECT_NE(base::TimeDelta(), throttler_->ScheduleClientCreation());
132 }
133
134 // Makes sure that after a certain amount of inactivity, the scheduling clock is
135 // reset.
136 TEST_F(MediaServiceThrottlerTest, NoCrash_LongInactivity_ShouldReset) {
137 // Schedule two minute's worth of clients
138 BatchScheduleClientCreations(base::TimeDelta::FromMinutes(2) / base_delay_);
139
140 // Advance the time so the scheduler perceived a full minute of inactivity.
141 clock_->Advance(base::TimeDelta::FromSeconds(61));
142
143 // Make sure new clients are burst scheduled.
144 EXPECT_EQ(base::TimeDelta(), throttler_->ScheduleClientCreation());
145 }
146
147 // Makes sure that crashes increase the scheduling delay.
148 TEST_F(MediaServiceThrottlerTest, WithCrash_BaseCase_DelayShouldIncrease) {
149 // Schedule clients until the next one would be over burst threshold.
150 BatchScheduleClientCreations(kMaxBurstClients);
151
152 base::TimeDelta no_crash_delay = GetCurrentDelayBetweenClients();
153
154 SimulateCrashes(1);
155
156 base::TimeDelta crash_delay = GetCurrentDelayBetweenClients();
157
158 EXPECT_NE(base_delay_, crash_delay);
159 EXPECT_GT(crash_delay, no_crash_delay);
160 }
161
162 // Makes sure that we tolerate 1 crash per minute.
163 TEST_F(MediaServiceThrottlerTest,
164 WithCrash_SingleCrash_DelayShouldNotIncrease) {
165 // Schedule clients until the next one would be over burst threshold.
166 BatchScheduleClientCreations(kMaxBurstClients);
167
168 SimulateCrashes(1);
169 clock_->Advance(base::TimeDelta::FromMilliseconds(1));
170
171 // Because we use the floor function when calculating crashes, a small time
172 // advance should nullify a single crash.
173 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
174 }
175
176 // Makes sure that more than 1 crash per minute causes increased delays.
177 TEST_F(MediaServiceThrottlerTest, WithCrash_ManyCrashes_DelayShouldIncrease) {
178 // Schedule clients until the next one would be over burst threshold.
179 BatchScheduleClientCreations(kMaxBurstClients);
180
181 SimulateCrashes(2);
182 clock_->Advance(base::TimeDelta::FromMilliseconds(1));
183
184 // The delay after crashes should be greater than the base delay.
185 EXPECT_LT(base_delay_, GetCurrentDelayBetweenClients());
186 }
187
188 // Makes sure that an increase in server crashes leads to delay increases.
189 TEST_F(MediaServiceThrottlerTest,
190 WithCrash_ConsecutiveCrashes_DelayShouldIncreaseWithNumberOfCrashes) {
191 // Schedule clients until the next one would be over burst threshold.
192 BatchScheduleClientCreations(kMaxBurstClients);
193
194 base::TimeDelta last_delay = base_delay_;
195
196 for (int i = 0; i < 5; ++i) {
197 SimulateCrashes(1);
198 base::TimeDelta current_delay = GetCurrentDelayBetweenClients();
199 EXPECT_LT(last_delay, current_delay);
200 last_delay = current_delay;
201 }
202 }
203
204 // Makes sure that crashes affect the number of burst clients we can schedule.
205 TEST_F(MediaServiceThrottlerTest, WithCrash_ShouldAllowFewerBurstClients) {
206 int number_burst_client_scheduled = 0;
207
208 SimulateCrashes(1);
209
210 while (base::TimeDelta() == throttler_->ScheduleClientCreation())
211 number_burst_client_scheduled++;
212
213 EXPECT_GT(kMaxBurstClients, number_burst_client_scheduled);
214 }
215
216 // Makes sure delays are capped to a certain maximal value.
217 TEST_F(MediaServiceThrottlerTest, WithCrash_ManyCrashes_DelayShouldMaxOut) {
218 // Schedule clients until the next one would be over burst threshold.
219 BatchScheduleClientCreations(kMaxBurstClients);
220
221 SimulateCrashes(10);
222 base::TimeDelta capped_delay = GetCurrentDelayBetweenClients();
223
224 SimulateCrashes(10);
225
226 EXPECT_EQ(capped_delay, GetCurrentDelayBetweenClients());
227 }
228
229 // Makes sure a minute without crashes resets the crash counter.
230 TEST_F(MediaServiceThrottlerTest, WithCrash_NoCrashesForAMinute_ShouldReset) {
231 SimulateCrashes(10);
232
233 // The effective server crash count should be reset because it has been over
234 // a minute since the last crash.
235 clock_->Advance(base::TimeDelta::FromSeconds(61));
236
237 BatchScheduleClientCreations(kMaxBurstClients);
238
239 EXPECT_EQ(base_delay_, GetCurrentDelayBetweenClients());
240 }
241
242 // Makes sure a steady crashes do not resets the crash counter.
243 TEST_F(MediaServiceThrottlerTest, WithCrash_ConstantCrashes_ShouldNotReset) {
244 SimulateCrashes(9);
245
246 // The effective server crash count should not be reset.
247 clock_->Advance(base::TimeDelta::FromSeconds(59));
248 SimulateCrashes(1);
249 clock_->Advance(base::TimeDelta::FromSeconds(2));
250
251 BatchScheduleClientCreations(kMaxBurstClients);
252
253 EXPECT_LT(base_delay_, GetCurrentDelayBetweenClients());
254 }
255
256 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698