1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <ctype.h>
#include <string>
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/scoped_vector.h"
#include "net/base/prioritized_dispatcher.h"
#include "net/base/request_priority.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
// We rely on the priority enum values being sequential having starting at 0,
// and increasing for higher priorities.
COMPILE_ASSERT(MINIMUM_PRIORITY == 0u &&
MINIMUM_PRIORITY == IDLE &&
IDLE < LOWEST &&
LOWEST < HIGHEST &&
HIGHEST < NUM_PRIORITIES,
priority_indexes_incompatible);
class PrioritizedDispatcherTest : public testing::Test {
public:
typedef PrioritizedDispatcher::Priority Priority;
// A job that appends |data| to |log_| when started and '.' when finished.
// This is intended to confirm the execution order of a sequence of jobs added
// to the dispatcher.
class TestJob : public PrioritizedDispatcher::Job {
public:
TestJob(PrioritizedDispatcherTest* test, char data, Priority priority)
: test_(test), data_(data), priority_(priority), running_(false) {}
// MSVS does not accept EXPECT_EQ(this, ...) so wrap it up.
PrioritizedDispatcher::Job* this_job() {
return this;
}
void Add() {
EXPECT_TRUE(handle_.is_null());
EXPECT_FALSE(running_);
size_t num_queued = dispatch().num_queued_jobs();
size_t num_running = dispatch().num_running_jobs();
handle_ = dispatch().Add(this, priority_);
if (handle_.is_null()) {
EXPECT_EQ(num_queued, dispatch().num_queued_jobs());
EXPECT_TRUE(running_);
EXPECT_EQ(num_running + 1, dispatch().num_running_jobs());
} else {
EXPECT_FALSE(running_);
EXPECT_EQ(priority_, handle_.priority());
EXPECT_EQ(this_job(), handle_.value());
EXPECT_EQ(num_running, dispatch().num_running_jobs());
}
}
void ChangePriority(Priority priority) {
EXPECT_FALSE(running_);
ASSERT_FALSE(handle_.is_null());
size_t num_queued = dispatch().num_queued_jobs();
size_t num_running = dispatch().num_running_jobs();
handle_ = dispatch().ChangePriority(handle_, priority);
if (handle_.is_null()) {
EXPECT_TRUE(running_);
EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs());
EXPECT_EQ(num_running + 1, dispatch().num_running_jobs());
} else {
EXPECT_FALSE(running_);
EXPECT_EQ(priority, handle_.priority());
EXPECT_EQ(this_job(), handle_.value());
EXPECT_EQ(num_queued, dispatch().num_queued_jobs());
EXPECT_EQ(num_running, dispatch().num_running_jobs());
}
}
void Cancel() {
EXPECT_FALSE(running_);
ASSERT_FALSE(handle_.is_null());
size_t num_queued = dispatch().num_queued_jobs();
dispatch().Cancel(handle_);
EXPECT_EQ(num_queued - 1, dispatch().num_queued_jobs());
handle_ = PrioritizedDispatcher::Handle();
}
void Finish() {
EXPECT_TRUE(running_);
running_ = false;
test_->log_.append(1u, '.');
dispatch().OnJobFinished();
}
// PriorityDispatch::Job interface
virtual void Start() OVERRIDE {
EXPECT_FALSE(running_);
handle_ = PrioritizedDispatcher::Handle();
running_ = true;
test_->log_.append(1u, data_);
}
private:
PrioritizedDispatcher& dispatch() { return *(test_->dispatch_); }
PrioritizedDispatcherTest* test_;
char data_;
Priority priority_;
PrioritizedDispatcher::Handle handle_;
bool running_;
};
protected:
void Prepare(const PrioritizedDispatcher::Limits& limits) {
dispatch_.reset(new PrioritizedDispatcher(limits));
}
TestJob* AddJob(char data, Priority priority) {
TestJob* job = new TestJob(this, data, priority);
jobs_.push_back(job);
job->Add();
return job;
}
void Expect(std::string log) {
EXPECT_EQ(0u, dispatch_->num_queued_jobs());
EXPECT_EQ(0u, dispatch_->num_running_jobs());
EXPECT_EQ(log, log_);
log_.clear();
}
std::string log_;
scoped_ptr<PrioritizedDispatcher> dispatch_;
ScopedVector<TestJob> jobs_;
};
TEST_F(PrioritizedDispatcherTest, AddAFIFO) {
// Allow only one running job.
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE);
TestJob* job_b = AddJob('b', IDLE);
TestJob* job_c = AddJob('c', IDLE);
TestJob* job_d = AddJob('d', IDLE);
job_a->Finish();
job_b->Finish();
job_c->Finish();
job_d->Finish();
Expect("a.b.c.d.");
}
TEST_F(PrioritizedDispatcherTest, AddPriority) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE);
TestJob* job_b = AddJob('b', MEDIUM);
TestJob* job_c = AddJob('c', HIGHEST);
TestJob* job_d = AddJob('d', HIGHEST);
TestJob* job_e = AddJob('e', MEDIUM);
job_a->Finish();
job_c->Finish();
job_d->Finish();
job_b->Finish();
job_e->Finish();
Expect("a.c.d.b.e.");
}
TEST_F(PrioritizedDispatcherTest, EnforceLimits) {
// Reserve 2 for HIGHEST and 1 for LOW or higher.
// This leaves 2 for LOWEST or lower.
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 5);
limits.reserved_slots[HIGHEST] = 2;
limits.reserved_slots[LOW] = 1;
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE); // Uses unreserved slot.
TestJob* job_b = AddJob('b', IDLE); // Uses unreserved slot.
TestJob* job_c = AddJob('c', LOWEST); // Must wait.
TestJob* job_d = AddJob('d', LOW); // Uses reserved slot.
TestJob* job_e = AddJob('e', MEDIUM); // Must wait.
TestJob* job_f = AddJob('f', HIGHEST); // Uses reserved slot.
TestJob* job_g = AddJob('g', HIGHEST); // Uses reserved slot.
TestJob* job_h = AddJob('h', HIGHEST); // Must wait.
EXPECT_EQ(5u, dispatch_->num_running_jobs());
EXPECT_EQ(3u, dispatch_->num_queued_jobs());
job_a->Finish(); // Releases h.
job_b->Finish();
job_d->Finish();
job_f->Finish(); // Releases e.
job_g->Finish();
job_h->Finish(); // Releases c.
job_e->Finish();
job_c->Finish();
Expect("abdfg.h...e..c..");
}
TEST_F(PrioritizedDispatcherTest, ChangePriority) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE);
TestJob* job_b = AddJob('b', MEDIUM);
TestJob* job_c = AddJob('c', HIGHEST);
TestJob* job_d = AddJob('d', HIGHEST);
job_b->ChangePriority(HIGHEST);
job_c->ChangePriority(MEDIUM);
job_a->Finish();
job_d->Finish();
job_b->Finish();
job_c->Finish();
Expect("a.d.b.c.");
}
TEST_F(PrioritizedDispatcherTest, Cancel) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE);
TestJob* job_b = AddJob('b', IDLE);
TestJob* job_c = AddJob('c', IDLE);
TestJob* job_d = AddJob('d', IDLE);
TestJob* job_e = AddJob('e', IDLE);
job_b->Cancel();
job_d->Cancel();
job_a->Finish();
job_c->Finish();
job_e->Finish();
Expect("a.c.e.");
}
TEST_F(PrioritizedDispatcherTest, Evict) {
PrioritizedDispatcher::Limits limits(NUM_PRIORITIES, 1);
Prepare(limits);
TestJob* job_a = AddJob('a', IDLE);
TestJob* job_b = AddJob('b', LOW);
TestJob* job_c = AddJob('c', HIGHEST);
TestJob* job_d = AddJob('d', LOW);
TestJob* job_e = AddJob('e', HIGHEST);
EXPECT_EQ(job_b, dispatch_->EvictOldestLowest());
EXPECT_EQ(job_d, dispatch_->EvictOldestLowest());
job_a->Finish();
job_c->Finish();
job_e->Finish();
Expect("a.c.e.");
}
} // namespace
} // namespace net
|