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.
4
5#include "chrome/browser/chromeos/drive/job_queue.h"
6
7#include "testing/gtest/include/gtest/gtest.h"
8
9namespace drive {
10
11TEST(JobQueueTest, BasicJobQueueOperations) {
12  const int kNumMaxConcurrentJobs = 3;
13  const int kNumPriorityLevels = 2;
14  enum {HIGH_PRIORITY, LOW_PRIORITY};
15
16  // Create a queue. Number of jobs are initially zero.
17  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels);
18  EXPECT_EQ(0U, queue.GetNumberOfJobs());
19
20  // Push 4 jobs.
21  queue.Push(101, LOW_PRIORITY);
22  queue.Push(102, HIGH_PRIORITY);
23  queue.Push(103, LOW_PRIORITY);
24  queue.Push(104, HIGH_PRIORITY);
25
26  // High priority jobs should be popped first.
27  JobID id;
28  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
29  EXPECT_EQ(102, id);
30  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
31  EXPECT_EQ(104, id);
32
33  // Then low priority jobs follow.
34  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
35  EXPECT_EQ(101, id);
36
37  // The queue allows at most 3 parallel runs. So returns false here.
38  EXPECT_FALSE(queue.PopForRun(LOW_PRIORITY, &id));
39
40  // No jobs finished yet, so the job count is four.
41  EXPECT_EQ(4U, queue.GetNumberOfJobs());
42
43  // Mark one job as finished.
44  queue.MarkFinished(104);
45  EXPECT_EQ(3U, queue.GetNumberOfJobs());
46
47  // Then the next jobs can be popped.
48  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
49  EXPECT_EQ(103, id);
50
51  // Push 1 more job.
52  queue.Push(105, LOW_PRIORITY);
53
54  // Finish all 3 running jobs.
55  queue.MarkFinished(101);
56  queue.MarkFinished(102);
57  queue.MarkFinished(103);
58  EXPECT_EQ(1U, queue.GetNumberOfJobs());
59
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));
63
64  // Under the low priority context, it is fine.
65  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
66  EXPECT_EQ(105, id);
67}
68
69TEST(JobQueueTest, JobQueueRemove) {
70  const int kNumMaxConcurrentJobs = 3;
71  const int kNumPriorityLevels = 2;
72  enum {HIGH_PRIORITY, LOW_PRIORITY};
73
74  // Create a queue. Number of jobs are initially zero.
75  JobQueue queue(kNumMaxConcurrentJobs, kNumPriorityLevels);
76  EXPECT_EQ(0U, queue.GetNumberOfJobs());
77
78  // Push 4 jobs.
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());
84
85  // Remove 2.
86  queue.Remove(101);
87  queue.Remove(104);
88  EXPECT_EQ(2U, queue.GetNumberOfJobs());
89
90  // Pop the 2 jobs.
91  JobID id;
92  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
93  EXPECT_EQ(102, id);
94  EXPECT_TRUE(queue.PopForRun(LOW_PRIORITY, &id));
95  EXPECT_EQ(103, id);
96  queue.MarkFinished(102);
97  queue.MarkFinished(103);
98
99  // 0 job left.
100  EXPECT_EQ(0U, queue.GetNumberOfJobs());
101  EXPECT_FALSE(queue.PopForRun(LOW_PRIORITY, &id));
102}
103
104}  // namespace drive
105