Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: sync/internal_api/attachments/task_queue_unittest.cc

Issue 554743004: Update AttachmentServiceImpl to retry attachment uploads. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check ShouldDispatch in Dispatch. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 <vector>
8
9 #include "base/bind.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/timer/mock_timer.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 using base::TimeDelta;
17
18 namespace syncer {
19
20 namespace {
21
22 const TimeDelta kZero;
23
24 } // namespace
25
26 class TaskQueueTest : public testing::Test {
27 protected:
28 TaskQueueTest() : weak_ptr_factory_(this) {
29 queue_.reset(new TaskQueue<int>(
30 base::Bind(&TaskQueueTest::Process, weak_ptr_factory_.GetWeakPtr()),
31 TimeDelta::FromMinutes(1),
32 TimeDelta::FromMinutes(8)));
33 }
34
35 void RunLoop() {
36 base::RunLoop run_loop;
37 run_loop.RunUntilIdle();
38 }
39
40 void Process(const int& task) { dispatched_.push_back(task); }
41
42 base::MessageLoop message_loop_;
43 scoped_ptr<TaskQueue<int> > queue_;
44 std::vector<int> dispatched_;
45 base::WeakPtrFactory<TaskQueueTest> weak_ptr_factory_;
46 };
47
48 // See that at most one task is dispatched at a time.
49 TEST_F(TaskQueueTest, AddToQueue_NoConcurrentTasks) {
50 queue_->AddToQueue(1);
51 queue_->AddToQueue(2);
52 RunLoop();
53
54 // Only one has been dispatched.
55 ASSERT_EQ(1U, dispatched_.size());
56 EXPECT_EQ(1, dispatched_.front());
57 RunLoop();
58
59 // Still only one.
60 ASSERT_EQ(1U, dispatched_.size());
61 EXPECT_EQ(1, dispatched_.front());
62 dispatched_.clear();
63 queue_->MarkAsSucceeded(1);
64 RunLoop();
65
66 ASSERT_EQ(1U, dispatched_.size());
67 EXPECT_EQ(2, dispatched_.front());
68 dispatched_.clear();
69 queue_->MarkAsSucceeded(2);
70 RunLoop();
71
72 ASSERT_TRUE(dispatched_.empty());
73 }
74
75 // See that that the queue ignores duplicate adds.
76 TEST_F(TaskQueueTest, AddToQueue_NoDuplicates) {
77 queue_->AddToQueue(1);
78 queue_->AddToQueue(1);
79 queue_->AddToQueue(2);
80 queue_->AddToQueue(1);
81 ASSERT_TRUE(dispatched_.empty());
82 RunLoop();
83
84 ASSERT_EQ(1U, dispatched_.size());
85 EXPECT_EQ(1, dispatched_.front());
86 dispatched_.clear();
87 queue_->MarkAsSucceeded(1);
88 RunLoop();
89
90 ASSERT_EQ(1U, dispatched_.size());
91 EXPECT_EQ(2, dispatched_.front());
92 dispatched_.clear();
93 queue_->MarkAsSucceeded(2);
94 RunLoop();
95
96 ASSERT_TRUE(dispatched_.empty());
97 }
98
99 // See that Retry works as expected.
100 TEST_F(TaskQueueTest, Retry) {
101 scoped_ptr<base::MockTimer> timer_to_pass(new base::MockTimer(false, false));
102 base::MockTimer* mock_timer = timer_to_pass.get();
103 queue_->SetTimerForTest(timer_to_pass.PassAs<base::Timer>());
104
105 // 1st attempt.
106 queue_->AddToQueue(1);
107 ASSERT_TRUE(mock_timer->IsRunning());
108 ASSERT_EQ(TimeDelta(), mock_timer->GetCurrentDelay());
109 TimeDelta last_delay = mock_timer->GetCurrentDelay();
110 mock_timer->Fire();
111 RunLoop();
112
113 // 2nd attempt.
114 ASSERT_FALSE(mock_timer->IsRunning());
115 ASSERT_EQ(1U, dispatched_.size());
116 EXPECT_EQ(1, dispatched_.front());
117 dispatched_.clear();
118 queue_->Retry(1);
119 ASSERT_TRUE(mock_timer->IsRunning());
120 EXPECT_GT(mock_timer->GetCurrentDelay(), last_delay);
121 EXPECT_LE(mock_timer->GetCurrentDelay(), TimeDelta::FromMinutes(1));
122 last_delay = mock_timer->GetCurrentDelay();
123 mock_timer->Fire();
124 RunLoop();
125
126 // 3rd attempt.
127 ASSERT_FALSE(mock_timer->IsRunning());
128 ASSERT_EQ(1U, dispatched_.size());
129 EXPECT_EQ(1, dispatched_.front());
130 dispatched_.clear();
131 queue_->Retry(1);
132 ASSERT_TRUE(mock_timer->IsRunning());
133 EXPECT_GT(mock_timer->GetCurrentDelay(), last_delay);
134 last_delay = mock_timer->GetCurrentDelay();
135 mock_timer->Fire();
136 RunLoop();
137
138 // Give up.
139 ASSERT_FALSE(mock_timer->IsRunning());
140 ASSERT_EQ(1U, dispatched_.size());
141 EXPECT_EQ(1, dispatched_.front());
142 dispatched_.clear();
143 queue_->Cancel(1);
144 ASSERT_FALSE(mock_timer->IsRunning());
145
146 // Try a different task. See the timer remains unchanged because the previous
147 // task was marked as failed.
148 ASSERT_TRUE(dispatched_.empty());
149 queue_->AddToQueue(2);
150 ASSERT_TRUE(mock_timer->IsRunning());
151 EXPECT_GE(last_delay, mock_timer->GetCurrentDelay());
152 last_delay = mock_timer->GetCurrentDelay();
153 mock_timer->Fire();
154 RunLoop();
155
156 // Mark this one as succeeding, which will clear the backoff delay.
157 ASSERT_FALSE(mock_timer->IsRunning());
158 ASSERT_EQ(1U, dispatched_.size());
159 EXPECT_EQ(2, dispatched_.front());
160 dispatched_.clear();
161 queue_->MarkAsSucceeded(2);
162 ASSERT_FALSE(mock_timer->IsRunning());
163
164 // Add one last task and see that it's dispatched without delay because the
165 // previous one succeeded.
166 ASSERT_TRUE(dispatched_.empty());
167 queue_->AddToQueue(3);
168 ASSERT_TRUE(mock_timer->IsRunning());
169 EXPECT_LT(mock_timer->GetCurrentDelay(), last_delay);
170 last_delay = mock_timer->GetCurrentDelay();
171 mock_timer->Fire();
172 RunLoop();
173
174 // Clean up.
175 ASSERT_EQ(1U, dispatched_.size());
176 EXPECT_EQ(3, dispatched_.front());
177 dispatched_.clear();
178 queue_->MarkAsSucceeded(3);
179 ASSERT_FALSE(mock_timer->IsRunning());
180 }
181
182 // See that a task marked as canceled is not retried.
183 TEST_F(TaskQueueTest, Cancel_NoRetry) {
184 queue_->AddToQueue(1);
185 RunLoop();
186
187 ASSERT_EQ(1U, dispatched_.size());
188 EXPECT_EQ(1, dispatched_.front());
189 dispatched_.clear();
190 queue_->Cancel(1);
191 RunLoop();
192
193 ASSERT_TRUE(dispatched_.empty());
194 }
195
196 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698