| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 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 "sync/internal_api/public/attachments/task_queue.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/message_loop/message_loop.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/timer/mock_timer.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 | |
| 17 using base::TimeDelta; | |
| 18 | |
| 19 namespace syncer { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 const TimeDelta kZero; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 class TaskQueueTest : public testing::Test { | |
| 28 protected: | |
| 29 TaskQueueTest() : weak_ptr_factory_(this) { | |
| 30 queue_.reset(new TaskQueue<int>( | |
| 31 base::Bind(&TaskQueueTest::Process, weak_ptr_factory_.GetWeakPtr()), | |
| 32 TimeDelta::FromMinutes(1), | |
| 33 TimeDelta::FromMinutes(8))); | |
| 34 } | |
| 35 | |
| 36 void RunLoop() { | |
| 37 base::RunLoop run_loop; | |
| 38 run_loop.RunUntilIdle(); | |
| 39 } | |
| 40 | |
| 41 void Process(const int& task) { dispatched_.push_back(task); } | |
| 42 | |
| 43 base::MessageLoop message_loop_; | |
| 44 std::unique_ptr<TaskQueue<int>> queue_; | |
| 45 std::vector<int> dispatched_; | |
| 46 base::WeakPtrFactory<TaskQueueTest> weak_ptr_factory_; | |
| 47 }; | |
| 48 | |
| 49 // See that at most one task is dispatched at a time. | |
| 50 TEST_F(TaskQueueTest, AddToQueue_NoConcurrentTasks) { | |
| 51 queue_->AddToQueue(1); | |
| 52 queue_->AddToQueue(2); | |
| 53 RunLoop(); | |
| 54 | |
| 55 // Only one has been dispatched. | |
| 56 ASSERT_EQ(1U, dispatched_.size()); | |
| 57 EXPECT_EQ(1, dispatched_.front()); | |
| 58 RunLoop(); | |
| 59 | |
| 60 // Still only one. | |
| 61 ASSERT_EQ(1U, dispatched_.size()); | |
| 62 EXPECT_EQ(1, dispatched_.front()); | |
| 63 dispatched_.clear(); | |
| 64 queue_->MarkAsSucceeded(1); | |
| 65 RunLoop(); | |
| 66 | |
| 67 ASSERT_EQ(1U, dispatched_.size()); | |
| 68 EXPECT_EQ(2, dispatched_.front()); | |
| 69 dispatched_.clear(); | |
| 70 queue_->MarkAsSucceeded(2); | |
| 71 RunLoop(); | |
| 72 | |
| 73 ASSERT_TRUE(dispatched_.empty()); | |
| 74 } | |
| 75 | |
| 76 // See that that the queue ignores duplicate adds. | |
| 77 TEST_F(TaskQueueTest, AddToQueue_NoDuplicates) { | |
| 78 queue_->AddToQueue(1); | |
| 79 queue_->AddToQueue(1); | |
| 80 queue_->AddToQueue(2); | |
| 81 queue_->AddToQueue(1); | |
| 82 ASSERT_TRUE(dispatched_.empty()); | |
| 83 RunLoop(); | |
| 84 | |
| 85 ASSERT_EQ(1U, dispatched_.size()); | |
| 86 EXPECT_EQ(1, dispatched_.front()); | |
| 87 dispatched_.clear(); | |
| 88 queue_->MarkAsSucceeded(1); | |
| 89 RunLoop(); | |
| 90 | |
| 91 ASSERT_EQ(1U, dispatched_.size()); | |
| 92 EXPECT_EQ(2, dispatched_.front()); | |
| 93 dispatched_.clear(); | |
| 94 queue_->MarkAsSucceeded(2); | |
| 95 RunLoop(); | |
| 96 | |
| 97 ASSERT_TRUE(dispatched_.empty()); | |
| 98 } | |
| 99 | |
| 100 // See that Retry works as expected. | |
| 101 TEST_F(TaskQueueTest, Retry) { | |
| 102 std::unique_ptr<base::MockTimer> timer_to_pass( | |
| 103 new base::MockTimer(false, false)); | |
| 104 base::MockTimer* mock_timer = timer_to_pass.get(); | |
| 105 queue_->SetTimerForTest(std::move(timer_to_pass)); | |
| 106 | |
| 107 // 1st attempt. | |
| 108 queue_->AddToQueue(1); | |
| 109 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 110 ASSERT_EQ(kZero, mock_timer->GetCurrentDelay()); | |
| 111 TimeDelta last_delay = mock_timer->GetCurrentDelay(); | |
| 112 mock_timer->Fire(); | |
| 113 RunLoop(); | |
| 114 | |
| 115 // 2nd attempt. | |
| 116 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 117 ASSERT_EQ(1U, dispatched_.size()); | |
| 118 EXPECT_EQ(1, dispatched_.front()); | |
| 119 dispatched_.clear(); | |
| 120 queue_->MarkAsFailed(1); | |
| 121 queue_->AddToQueue(1); | |
| 122 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 123 EXPECT_GT(mock_timer->GetCurrentDelay(), last_delay); | |
| 124 EXPECT_LE(mock_timer->GetCurrentDelay(), TimeDelta::FromMinutes(1)); | |
| 125 last_delay = mock_timer->GetCurrentDelay(); | |
| 126 mock_timer->Fire(); | |
| 127 RunLoop(); | |
| 128 | |
| 129 // 3rd attempt. | |
| 130 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 131 ASSERT_EQ(1U, dispatched_.size()); | |
| 132 EXPECT_EQ(1, dispatched_.front()); | |
| 133 dispatched_.clear(); | |
| 134 queue_->MarkAsFailed(1); | |
| 135 queue_->AddToQueue(1); | |
| 136 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 137 EXPECT_GT(mock_timer->GetCurrentDelay(), last_delay); | |
| 138 last_delay = mock_timer->GetCurrentDelay(); | |
| 139 mock_timer->Fire(); | |
| 140 RunLoop(); | |
| 141 | |
| 142 // Give up. | |
| 143 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 144 ASSERT_EQ(1U, dispatched_.size()); | |
| 145 EXPECT_EQ(1, dispatched_.front()); | |
| 146 dispatched_.clear(); | |
| 147 queue_->Cancel(1); | |
| 148 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 149 | |
| 150 // Try a different task. See the timer remains unchanged because the previous | |
| 151 // task was cancelled. | |
| 152 ASSERT_TRUE(dispatched_.empty()); | |
| 153 queue_->AddToQueue(2); | |
| 154 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 155 EXPECT_GE(last_delay, mock_timer->GetCurrentDelay()); | |
| 156 last_delay = mock_timer->GetCurrentDelay(); | |
| 157 mock_timer->Fire(); | |
| 158 RunLoop(); | |
| 159 | |
| 160 // Mark this one as succeeding, which will clear the backoff delay. | |
| 161 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 162 ASSERT_EQ(1U, dispatched_.size()); | |
| 163 EXPECT_EQ(2, dispatched_.front()); | |
| 164 dispatched_.clear(); | |
| 165 queue_->MarkAsSucceeded(2); | |
| 166 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 167 | |
| 168 // Add one last task and see that it's dispatched without delay because the | |
| 169 // previous one succeeded. | |
| 170 ASSERT_TRUE(dispatched_.empty()); | |
| 171 queue_->AddToQueue(3); | |
| 172 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 173 EXPECT_LT(mock_timer->GetCurrentDelay(), last_delay); | |
| 174 last_delay = mock_timer->GetCurrentDelay(); | |
| 175 mock_timer->Fire(); | |
| 176 RunLoop(); | |
| 177 | |
| 178 // Clean up. | |
| 179 ASSERT_EQ(1U, dispatched_.size()); | |
| 180 EXPECT_EQ(3, dispatched_.front()); | |
| 181 dispatched_.clear(); | |
| 182 queue_->MarkAsSucceeded(3); | |
| 183 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 184 } | |
| 185 | |
| 186 TEST_F(TaskQueueTest, Cancel) { | |
| 187 queue_->AddToQueue(1); | |
| 188 RunLoop(); | |
| 189 | |
| 190 ASSERT_EQ(1U, dispatched_.size()); | |
| 191 EXPECT_EQ(1, dispatched_.front()); | |
| 192 dispatched_.clear(); | |
| 193 queue_->Cancel(1); | |
| 194 RunLoop(); | |
| 195 | |
| 196 ASSERT_TRUE(dispatched_.empty()); | |
| 197 } | |
| 198 | |
| 199 // See that ResetBackoff resets the backoff delay. | |
| 200 TEST_F(TaskQueueTest, ResetBackoff) { | |
| 201 std::unique_ptr<base::MockTimer> timer_to_pass( | |
| 202 new base::MockTimer(false, false)); | |
| 203 base::MockTimer* mock_timer = timer_to_pass.get(); | |
| 204 queue_->SetTimerForTest(std::move(timer_to_pass)); | |
| 205 | |
| 206 // Add an item, mark it as failed, re-add it and see that we now have a | |
| 207 // backoff delay. | |
| 208 queue_->AddToQueue(1); | |
| 209 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 210 ASSERT_EQ(kZero, mock_timer->GetCurrentDelay()); | |
| 211 mock_timer->Fire(); | |
| 212 RunLoop(); | |
| 213 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 214 ASSERT_EQ(1U, dispatched_.size()); | |
| 215 EXPECT_EQ(1, dispatched_.front()); | |
| 216 dispatched_.clear(); | |
| 217 queue_->MarkAsFailed(1); | |
| 218 queue_->AddToQueue(1); | |
| 219 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 220 EXPECT_GT(mock_timer->GetCurrentDelay(), kZero); | |
| 221 EXPECT_LE(mock_timer->GetCurrentDelay(), TimeDelta::FromMinutes(1)); | |
| 222 | |
| 223 // Call ResetBackoff and see that there is no longer a delay. | |
| 224 queue_->ResetBackoff(); | |
| 225 ASSERT_TRUE(mock_timer->IsRunning()); | |
| 226 ASSERT_EQ(kZero, mock_timer->GetCurrentDelay()); | |
| 227 mock_timer->Fire(); | |
| 228 RunLoop(); | |
| 229 ASSERT_FALSE(mock_timer->IsRunning()); | |
| 230 ASSERT_EQ(1U, dispatched_.size()); | |
| 231 EXPECT_EQ(1, dispatched_.front()); | |
| 232 dispatched_.clear(); | |
| 233 queue_->MarkAsSucceeded(1); | |
| 234 } | |
| 235 | |
| 236 } // namespace syncer | |
| OLD | NEW |