1 // Copyright 2013 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.
5 #include "components/drive/job_queue.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(JobQueueTest
, BasicJobQueueOperations
) {
12 const size_t kNumMaxConcurrentJobs
= 3;
13 const size_t kNumPriorityLevels
= 2;
14 const size_t kNumMaxBatchJob
= 0;
15 const size_t kMaxBatchSize
= 0;
16 enum {HIGH_PRIORITY
, LOW_PRIORITY
};
18 // Create a queue. Number of jobs are initially zero.
19 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
, kNumMaxBatchJob
,
21 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
24 queue
.Push(101, LOW_PRIORITY
, false, 0);
25 queue
.Push(102, HIGH_PRIORITY
, false, 0);
26 queue
.Push(103, LOW_PRIORITY
, false, 0);
27 queue
.Push(104, HIGH_PRIORITY
, false, 0);
29 // High priority jobs should be popped first.
30 std::vector
<JobID
> ids
;
31 queue
.PopForRun(LOW_PRIORITY
, &ids
);
32 ASSERT_EQ(1u, ids
.size());
33 EXPECT_EQ(102, ids
[0]);
34 queue
.PopForRun(LOW_PRIORITY
, &ids
);
35 ASSERT_EQ(1u, ids
.size());
36 EXPECT_EQ(104, ids
[0]);
38 // Then low priority jobs follow.
39 queue
.PopForRun(LOW_PRIORITY
, &ids
);
40 ASSERT_EQ(1u, ids
.size());
41 EXPECT_EQ(101, ids
[0]);
43 // The queue allows at most 3 parallel runs. So returns false here.
44 queue
.PopForRun(LOW_PRIORITY
, &ids
);
45 ASSERT_EQ(0u, ids
.size());
47 // No jobs finished yet, so the job count is four.
48 EXPECT_EQ(4U, queue
.GetNumberOfJobs());
50 // Mark one job as finished.
51 queue
.MarkFinished(104);
52 EXPECT_EQ(3U, queue
.GetNumberOfJobs());
54 // Then the next jobs can be popped.
55 queue
.PopForRun(LOW_PRIORITY
, &ids
);
56 ASSERT_EQ(1u, ids
.size());
57 EXPECT_EQ(103, ids
[0]);
60 queue
.Push(105, LOW_PRIORITY
, false, 0);
62 // Finish all 3 running jobs.
63 queue
.MarkFinished(101);
64 queue
.MarkFinished(102);
65 queue
.MarkFinished(103);
66 EXPECT_EQ(1U, queue
.GetNumberOfJobs());
68 // The remaining jobs is of low priority, so under HIGH_PRIORITY context, it
69 // cannot be popped for running.
70 queue
.PopForRun(HIGH_PRIORITY
, &ids
);
71 ASSERT_EQ(0u, ids
.size());
73 // Under the low priority context, it is fine.
74 queue
.PopForRun(LOW_PRIORITY
, &ids
);
75 ASSERT_EQ(1u, ids
.size());
76 EXPECT_EQ(105, ids
[0]);
79 TEST(JobQueueTest
, JobQueueRemove
) {
80 const size_t kNumMaxConcurrentJobs
= 3;
81 const size_t kNumPriorityLevels
= 2;
82 const size_t kNumMaxBatchJob
= 0;
83 const size_t kMaxBatchSize
= 0;
84 enum {HIGH_PRIORITY
, LOW_PRIORITY
};
86 // Create a queue. Number of jobs are initially zero.
87 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
, kNumMaxBatchJob
,
89 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
92 queue
.Push(101, LOW_PRIORITY
, false, 0);
93 queue
.Push(102, HIGH_PRIORITY
, false, 0);
94 queue
.Push(103, LOW_PRIORITY
, false, 0);
95 queue
.Push(104, HIGH_PRIORITY
, false, 0);
96 EXPECT_EQ(4U, queue
.GetNumberOfJobs());
101 EXPECT_EQ(2U, queue
.GetNumberOfJobs());
104 std::vector
<JobID
> ids
;
105 queue
.PopForRun(LOW_PRIORITY
, &ids
);
106 ASSERT_EQ(1u, ids
.size());
107 EXPECT_EQ(102, ids
[0]);
108 queue
.PopForRun(LOW_PRIORITY
, &ids
);
109 ASSERT_EQ(1u, ids
.size());
110 EXPECT_EQ(103, ids
[0]);
111 queue
.MarkFinished(102);
112 queue
.MarkFinished(103);
115 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
116 queue
.PopForRun(LOW_PRIORITY
, &ids
);
117 ASSERT_EQ(0u, ids
.size());
120 TEST(JobQueueTest
, BatchRequest
) {
121 const size_t kNumMaxConcurrentJobs
= 1;
122 const size_t kNumPriorityLevels
= 2;
123 const size_t kNumMaxBatchJob
= 100;
124 const size_t kMaxBatchSize
= 5;
125 enum { HIGH_PRIORITY
, LOW_PRIORITY
};
127 // Create a queue. Number of jobs are initially zero.
128 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
, kNumMaxBatchJob
,
130 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
133 queue
.Push(101, LOW_PRIORITY
, true, 1);
134 queue
.Push(102, HIGH_PRIORITY
, true, 1);
135 queue
.Push(103, LOW_PRIORITY
, false, 1);
136 queue
.Push(104, HIGH_PRIORITY
, true, 1);
137 queue
.Push(105, LOW_PRIORITY
, true, 1);
138 queue
.Push(106, HIGH_PRIORITY
, true, 10);
140 EXPECT_EQ(6U, queue
.GetNumberOfJobs());
143 std::vector
<JobID
> ids
;
144 queue
.PopForRun(LOW_PRIORITY
, &ids
);
145 ASSERT_EQ(2u, ids
.size());
146 EXPECT_EQ(102, ids
[0]);
147 EXPECT_EQ(104, ids
[1]);
148 queue
.MarkFinished(102);
149 queue
.MarkFinished(104);
150 queue
.PopForRun(LOW_PRIORITY
, &ids
);
151 ASSERT_EQ(1u, ids
.size());
152 EXPECT_EQ(106, ids
[0]);
153 queue
.MarkFinished(106);
154 queue
.PopForRun(LOW_PRIORITY
, &ids
);
155 ASSERT_EQ(1u, ids
.size());
156 EXPECT_EQ(101, ids
[0]);
157 queue
.MarkFinished(101);
158 queue
.PopForRun(LOW_PRIORITY
, &ids
);
159 ASSERT_EQ(1u, ids
.size());
160 EXPECT_EQ(103, ids
[0]);
161 queue
.MarkFinished(103);
162 queue
.PopForRun(LOW_PRIORITY
, &ids
);
163 ASSERT_EQ(1u, ids
.size());
164 EXPECT_EQ(105, ids
[0]);
165 queue
.MarkFinished(105);
166 queue
.PopForRun(LOW_PRIORITY
, &ids
);
167 EXPECT_EQ(0u, ids
.size());
170 TEST(JobQueueTest
, BatchRequstNumMaxJobs
) {
171 const size_t kNumMaxConcurrentJobs
= 1;
172 const size_t kNumPriorityLevels
= 2;
173 const size_t kNumMaxBatchJob
= 5;
174 const size_t kMaxBatchSize
= 100;
175 enum { HIGH_PRIORITY
, LOW_PRIORITY
};
177 // Create a queue. Number of jobs are initially zero.
178 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
, kNumMaxBatchJob
,
180 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
183 queue
.Push(101, LOW_PRIORITY
, true, 1);
184 queue
.Push(102, LOW_PRIORITY
, true, 1);
185 queue
.Push(103, LOW_PRIORITY
, true, 1);
186 queue
.Push(104, LOW_PRIORITY
, true, 1);
187 queue
.Push(105, LOW_PRIORITY
, true, 1);
188 queue
.Push(106, LOW_PRIORITY
, true, 1);
190 EXPECT_EQ(6U, queue
.GetNumberOfJobs());
192 // Pop the 5 of 6 jobs.
193 std::vector
<JobID
> ids
;
194 queue
.PopForRun(LOW_PRIORITY
, &ids
);
195 EXPECT_EQ(5u, ids
.size());