summaryrefslogtreecommitdiffstats
path: root/components/drive/job_queue_unittest.cc
blob: 1390b1f265b7a79d06250622406b6bb6eddba70d (plain)
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
// Copyright 2013 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 "components/drive/job_queue.h"

#include <stddef.h>

#include "testing/gtest/include/gtest/gtest.h"

namespace drive {

TEST(JobQueueTest, BasicJobQueueOperations) {
  const size_t kNumMaxConcurrentJobs = 3;
  const size_t kNumPriorityLevels = 2;
  const size_t kNumMaxBatchJob = 0;
  const size_t kMaxBatchSize = 0;
  enum {HIGH_PRIORITY, LOW_PRIORITY};

  // Create a queue. Number of jobs are initially zero.
  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels, kNumMaxBatchJob,
                 kMaxBatchSize);
  EXPECT_EQ(0U, queue.GetNumberOfJobs());

  // Push 4 jobs.
  queue.Push(101, LOW_PRIORITY, false, 0);
  queue.Push(102, HIGH_PRIORITY, false, 0);
  queue.Push(103, LOW_PRIORITY, false, 0);
  queue.Push(104, HIGH_PRIORITY, false, 0);

  // High priority jobs should be popped first.
  std::vector<JobID> ids;
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(102, ids[0]);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(104, ids[0]);

  // Then low priority jobs follow.
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(101, ids[0]);

  // The queue allows at most 3 parallel runs. So returns false here.
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(0u, ids.size());

  // No jobs finished yet, so the job count is four.
  EXPECT_EQ(4U, queue.GetNumberOfJobs());

  // Mark one job as finished.
  queue.MarkFinished(104);
  EXPECT_EQ(3U, queue.GetNumberOfJobs());

  // Then the next jobs can be popped.
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(103, ids[0]);

  // Push 1 more job.
  queue.Push(105, LOW_PRIORITY, false, 0);

  // Finish all 3 running jobs.
  queue.MarkFinished(101);
  queue.MarkFinished(102);
  queue.MarkFinished(103);
  EXPECT_EQ(1U, queue.GetNumberOfJobs());

  // The remaining jobs is of low priority, so under HIGH_PRIORITY context, it
  // cannot be popped for running.
  queue.PopForRun(HIGH_PRIORITY, &ids);
  ASSERT_EQ(0u, ids.size());

  // Under the low priority context, it is fine.
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(105, ids[0]);
}

TEST(JobQueueTest, JobQueueRemove) {
  const size_t kNumMaxConcurrentJobs = 3;
  const size_t kNumPriorityLevels = 2;
  const size_t kNumMaxBatchJob = 0;
  const size_t kMaxBatchSize = 0;
  enum {HIGH_PRIORITY, LOW_PRIORITY};

  // Create a queue. Number of jobs are initially zero.
  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels, kNumMaxBatchJob,
                 kMaxBatchSize);
  EXPECT_EQ(0U, queue.GetNumberOfJobs());

  // Push 4 jobs.
  queue.Push(101, LOW_PRIORITY, false, 0);
  queue.Push(102, HIGH_PRIORITY, false, 0);
  queue.Push(103, LOW_PRIORITY, false, 0);
  queue.Push(104, HIGH_PRIORITY, false, 0);
  EXPECT_EQ(4U, queue.GetNumberOfJobs());

  // Remove 2.
  queue.Remove(101);
  queue.Remove(104);
  EXPECT_EQ(2U, queue.GetNumberOfJobs());

  // Pop the 2 jobs.
  std::vector<JobID> ids;
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(102, ids[0]);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(103, ids[0]);
  queue.MarkFinished(102);
  queue.MarkFinished(103);

  // 0 job left.
  EXPECT_EQ(0U, queue.GetNumberOfJobs());
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(0u, ids.size());
}

TEST(JobQueueTest, BatchRequest) {
  const size_t kNumMaxConcurrentJobs = 1;
  const size_t kNumPriorityLevels = 2;
  const size_t kNumMaxBatchJob = 100;
  const size_t kMaxBatchSize = 5;
  enum { HIGH_PRIORITY, LOW_PRIORITY };

  // Create a queue. Number of jobs are initially zero.
  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels, kNumMaxBatchJob,
                 kMaxBatchSize);
  EXPECT_EQ(0U, queue.GetNumberOfJobs());

  // Push 6 jobs.
  queue.Push(101, LOW_PRIORITY, true, 1);
  queue.Push(102, HIGH_PRIORITY, true, 1);
  queue.Push(103, LOW_PRIORITY, false, 1);
  queue.Push(104, HIGH_PRIORITY, true, 1);
  queue.Push(105, LOW_PRIORITY, true, 1);
  queue.Push(106, HIGH_PRIORITY, true, 10);

  EXPECT_EQ(6U, queue.GetNumberOfJobs());

  // Pop the 6 jobs.
  std::vector<JobID> ids;
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(2u, ids.size());
  EXPECT_EQ(102, ids[0]);
  EXPECT_EQ(104, ids[1]);
  queue.MarkFinished(102);
  queue.MarkFinished(104);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(106, ids[0]);
  queue.MarkFinished(106);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(101, ids[0]);
  queue.MarkFinished(101);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(103, ids[0]);
  queue.MarkFinished(103);
  queue.PopForRun(LOW_PRIORITY, &ids);
  ASSERT_EQ(1u, ids.size());
  EXPECT_EQ(105, ids[0]);
  queue.MarkFinished(105);
  queue.PopForRun(LOW_PRIORITY, &ids);
  EXPECT_EQ(0u, ids.size());
}

TEST(JobQueueTest, BatchRequstNumMaxJobs) {
  const size_t kNumMaxConcurrentJobs = 1;
  const size_t kNumPriorityLevels = 2;
  const size_t kNumMaxBatchJob = 5;
  const size_t kMaxBatchSize = 100;
  enum { HIGH_PRIORITY, LOW_PRIORITY };

  // Create a queue. Number of jobs are initially zero.
  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels, kNumMaxBatchJob,
                 kMaxBatchSize);
  EXPECT_EQ(0U, queue.GetNumberOfJobs());

  // Push 6 jobs.
  queue.Push(101, LOW_PRIORITY, true, 1);
  queue.Push(102, LOW_PRIORITY, true, 1);
  queue.Push(103, LOW_PRIORITY, true, 1);
  queue.Push(104, LOW_PRIORITY, true, 1);
  queue.Push(105, LOW_PRIORITY, true, 1);
  queue.Push(106, LOW_PRIORITY, true, 1);

  EXPECT_EQ(6U, queue.GetNumberOfJobs());

  // Pop the 5 of 6 jobs.
  std::vector<JobID> ids;
  queue.PopForRun(LOW_PRIORITY, &ids);
  EXPECT_EQ(5u, ids.size());
}

}  // namespace drive