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 "chrome/browser/chromeos/drive/job_queue.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(JobQueueTest
, BasicJobQueueOperations
) {
12 const int kNumMaxConcurrentJobs
= 3;
13 const int kNumPriorityLevels
= 2;
14 enum {HIGH_PRIORITY
, LOW_PRIORITY
};
16 // Create a queue. Number of jobs are initially zero.
17 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
);
18 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
21 queue
.Push(101, LOW_PRIORITY
);
22 queue
.Push(102, HIGH_PRIORITY
);
23 queue
.Push(103, LOW_PRIORITY
);
24 queue
.Push(104, HIGH_PRIORITY
);
26 // High priority jobs should be popped first.
28 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
30 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
33 // Then low priority jobs follow.
34 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
37 // The queue allows at most 3 parallel runs. So returns false here.
38 EXPECT_FALSE(queue
.PopForRun(LOW_PRIORITY
, &id
));
40 // No jobs finished yet, so the job count is four.
41 EXPECT_EQ(4U, queue
.GetNumberOfJobs());
43 // Mark one job as finished.
44 queue
.MarkFinished(104);
45 EXPECT_EQ(3U, queue
.GetNumberOfJobs());
47 // Then the next jobs can be popped.
48 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
52 queue
.Push(105, LOW_PRIORITY
);
54 // Finish all 3 running jobs.
55 queue
.MarkFinished(101);
56 queue
.MarkFinished(102);
57 queue
.MarkFinished(103);
58 EXPECT_EQ(1U, queue
.GetNumberOfJobs());
60 // The remaining jobs is of low priority, so under HIGH_PRIORITY context, it
61 // cannot be popped for running.
62 EXPECT_FALSE(queue
.PopForRun(HIGH_PRIORITY
, &id
));
64 // Under the low priority context, it is fine.
65 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
69 TEST(JobQueueTest
, JobQueueRemove
) {
70 const int kNumMaxConcurrentJobs
= 3;
71 const int kNumPriorityLevels
= 2;
72 enum {HIGH_PRIORITY
, LOW_PRIORITY
};
74 // Create a queue. Number of jobs are initially zero.
75 JobQueue
queue(kNumMaxConcurrentJobs
, kNumPriorityLevels
);
76 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
79 queue
.Push(101, LOW_PRIORITY
);
80 queue
.Push(102, HIGH_PRIORITY
);
81 queue
.Push(103, LOW_PRIORITY
);
82 queue
.Push(104, HIGH_PRIORITY
);
83 EXPECT_EQ(4U, queue
.GetNumberOfJobs());
88 EXPECT_EQ(2U, queue
.GetNumberOfJobs());
92 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
94 EXPECT_TRUE(queue
.PopForRun(LOW_PRIORITY
, &id
));
96 queue
.MarkFinished(102);
97 queue
.MarkFinished(103);
100 EXPECT_EQ(0U, queue
.GetNumberOfJobs());
101 EXPECT_FALSE(queue
.PopForRun(LOW_PRIORITY
, &id
));