OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/file_system_provider/queue.h" | 5 #include "chrome/browser/chromeos/file_system_provider/queue.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/files/file.h" | 9 #include "base/files/file.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 int second_abort_counter = 0; | 63 int second_abort_counter = 0; |
64 queue.Enqueue(second_token, | 64 queue.Enqueue(second_token, |
65 base::Bind(&OnRun, &second_counter, &second_abort_counter)); | 65 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
66 | 66 |
67 base::RunLoop().RunUntilIdle(); | 67 base::RunLoop().RunUntilIdle(); |
68 EXPECT_EQ(1, first_counter); | 68 EXPECT_EQ(1, first_counter); |
69 EXPECT_EQ(0, first_abort_counter); | 69 EXPECT_EQ(0, first_abort_counter); |
70 EXPECT_EQ(0, second_counter); | 70 EXPECT_EQ(0, second_counter); |
71 EXPECT_EQ(0, second_abort_counter); | 71 EXPECT_EQ(0, second_abort_counter); |
72 | 72 |
73 // Complete the first task, which should not run the second one, yet. | 73 // Complete the first task from the queue should run the second task. |
74 queue.Complete(first_token); | 74 queue.Complete(first_token); |
75 base::RunLoop().RunUntilIdle(); | 75 base::RunLoop().RunUntilIdle(); |
76 EXPECT_EQ(1, first_counter); | 76 EXPECT_EQ(1, first_counter); |
77 EXPECT_EQ(0, first_abort_counter); | 77 EXPECT_EQ(0, first_abort_counter); |
78 EXPECT_EQ(0, second_counter); | |
79 EXPECT_EQ(0, second_abort_counter); | |
80 | |
81 // Removing the first task from the queue should run the second task. | |
82 queue.Remove(first_token); | |
83 base::RunLoop().RunUntilIdle(); | |
84 EXPECT_EQ(1, first_counter); | |
85 EXPECT_EQ(0, first_abort_counter); | |
86 EXPECT_EQ(1, second_counter); | 78 EXPECT_EQ(1, second_counter); |
87 EXPECT_EQ(0, second_abort_counter); | 79 EXPECT_EQ(0, second_abort_counter); |
88 | 80 |
89 const size_t third_token = queue.NewToken(); | 81 const size_t third_token = queue.NewToken(); |
90 int third_counter = 0; | 82 int third_counter = 0; |
91 int third_abort_counter = 0; | 83 int third_abort_counter = 0; |
92 queue.Enqueue(third_token, | 84 queue.Enqueue(third_token, |
93 base::Bind(&OnRun, &third_counter, &third_abort_counter)); | 85 base::Bind(&OnRun, &third_counter, &third_abort_counter)); |
94 | 86 |
95 // The second task is still running, so the third one is blocked. | 87 // The second task is still running, so the third one is blocked. |
96 base::RunLoop().RunUntilIdle(); | 88 base::RunLoop().RunUntilIdle(); |
97 EXPECT_EQ(1, first_counter); | 89 EXPECT_EQ(1, first_counter); |
98 EXPECT_EQ(0, first_abort_counter); | 90 EXPECT_EQ(0, first_abort_counter); |
99 EXPECT_EQ(1, second_counter); | 91 EXPECT_EQ(1, second_counter); |
100 EXPECT_EQ(0, second_abort_counter); | 92 EXPECT_EQ(0, second_abort_counter); |
101 EXPECT_EQ(0, third_counter); | 93 EXPECT_EQ(0, third_counter); |
102 EXPECT_EQ(0, third_abort_counter); | 94 EXPECT_EQ(0, third_abort_counter); |
103 | 95 |
104 // After aborting the second task, the third should run. | 96 // After aborting the second task, the third should run. |
105 queue.Abort(second_token); | 97 queue.Abort(second_token); |
106 queue.Remove(second_token); | 98 queue.Complete(second_token); |
107 base::RunLoop().RunUntilIdle(); | 99 base::RunLoop().RunUntilIdle(); |
108 EXPECT_EQ(1, first_counter); | 100 EXPECT_EQ(1, first_counter); |
109 EXPECT_EQ(0, first_abort_counter); | 101 EXPECT_EQ(0, first_abort_counter); |
110 EXPECT_EQ(1, second_counter); | 102 EXPECT_EQ(1, second_counter); |
111 EXPECT_EQ(1, second_abort_counter); | 103 EXPECT_EQ(1, second_abort_counter); |
112 EXPECT_EQ(1, third_counter); | 104 EXPECT_EQ(1, third_counter); |
113 EXPECT_EQ(0, third_abort_counter); | 105 EXPECT_EQ(0, third_abort_counter); |
114 } | 106 } |
115 | 107 |
116 TEST_F(FileSystemProviderQueueTest, Enqueue_WhilePreviousNotRemoved) { | |
117 Queue queue(1); | |
118 const size_t first_token = queue.NewToken(); | |
119 int first_counter = 0; | |
120 int first_abort_counter = 0; | |
121 queue.Enqueue(first_token, | |
122 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
123 | |
124 base::RunLoop().RunUntilIdle(); | |
125 queue.Complete(first_token); | |
126 | |
127 // Enqueuing a new task must not start it, once the queue is filled with a | |
128 // completed task. | |
129 const size_t second_token = queue.NewToken(); | |
130 int second_counter = 0; | |
131 int second_abort_counter = 0; | |
132 queue.Enqueue(second_token, | |
133 base::Bind(&OnRun, &second_counter, &second_abort_counter)); | |
134 | |
135 base::RunLoop().RunUntilIdle(); | |
136 EXPECT_EQ(0, second_counter); | |
137 EXPECT_EQ(0, second_abort_counter); | |
138 } | |
139 | |
140 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { | 108 TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) { |
141 Queue queue(2); | 109 Queue queue(2); |
142 const size_t first_token = queue.NewToken(); | 110 const size_t first_token = queue.NewToken(); |
143 int first_counter = 0; | 111 int first_counter = 0; |
144 int first_abort_counter = 0; | 112 int first_abort_counter = 0; |
145 queue.Enqueue(first_token, | 113 queue.Enqueue(first_token, |
146 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 114 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
147 | 115 |
148 const size_t second_token = queue.NewToken(); | 116 const size_t second_token = queue.NewToken(); |
149 int second_counter = 0; | 117 int second_counter = 0; |
(...skipping 10 matching lines...) Expand all Loading... |
160 base::RunLoop().RunUntilIdle(); | 128 base::RunLoop().RunUntilIdle(); |
161 EXPECT_EQ(1, first_counter); | 129 EXPECT_EQ(1, first_counter); |
162 EXPECT_EQ(0, first_abort_counter); | 130 EXPECT_EQ(0, first_abort_counter); |
163 EXPECT_EQ(1, second_counter); | 131 EXPECT_EQ(1, second_counter); |
164 EXPECT_EQ(0, second_abort_counter); | 132 EXPECT_EQ(0, second_abort_counter); |
165 EXPECT_EQ(0, third_counter); | 133 EXPECT_EQ(0, third_counter); |
166 EXPECT_EQ(0, third_abort_counter); | 134 EXPECT_EQ(0, third_abort_counter); |
167 | 135 |
168 // Completing and removing the second task, should start the last one. | 136 // Completing and removing the second task, should start the last one. |
169 queue.Complete(second_token); | 137 queue.Complete(second_token); |
170 queue.Remove(second_token); | |
171 base::RunLoop().RunUntilIdle(); | 138 base::RunLoop().RunUntilIdle(); |
172 EXPECT_EQ(1, first_counter); | 139 EXPECT_EQ(1, first_counter); |
173 EXPECT_EQ(0, first_abort_counter); | 140 EXPECT_EQ(0, first_abort_counter); |
174 EXPECT_EQ(1, second_counter); | 141 EXPECT_EQ(1, second_counter); |
175 EXPECT_EQ(0, second_abort_counter); | 142 EXPECT_EQ(0, second_abort_counter); |
176 EXPECT_EQ(1, third_counter); | 143 EXPECT_EQ(1, third_counter); |
177 EXPECT_EQ(0, third_abort_counter); | 144 EXPECT_EQ(0, third_abort_counter); |
178 } | 145 } |
179 | 146 |
180 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) | 147 #if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST) |
(...skipping 18 matching lines...) Expand all Loading... |
199 Queue queue(1); | 166 Queue queue(1); |
200 const size_t first_token = queue.NewToken(); | 167 const size_t first_token = queue.NewToken(); |
201 int first_counter = 0; | 168 int first_counter = 0; |
202 int first_abort_counter = 0; | 169 int first_abort_counter = 0; |
203 queue.Enqueue(first_token, | 170 queue.Enqueue(first_token, |
204 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 171 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
205 | 172 |
206 // Completing and removing the first task, which however hasn't started. | 173 // Completing and removing the first task, which however hasn't started. |
207 // That should not invoke the second task. | 174 // That should not invoke the second task. |
208 EXPECT_DEATH(queue.Complete(first_token), ""); | 175 EXPECT_DEATH(queue.Complete(first_token), ""); |
209 EXPECT_DEATH(queue.Remove(first_token), ""); | |
210 } | 176 } |
211 | 177 |
212 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompletedNorAborted) { | 178 TEST_F(FileSystemProviderQueueTest, |
| 179 InvalidUsage_CompleteAfterAbortingNonExecutedTask) { |
213 Queue queue(1); | 180 Queue queue(1); |
214 const size_t first_token = queue.NewToken(); | 181 const size_t first_token = queue.NewToken(); |
215 int first_counter = 0; | 182 int first_counter = 0; |
216 int first_abort_counter = 0; | |
217 queue.Enqueue(first_token, | |
218 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
219 | |
220 base::RunLoop().RunUntilIdle(); | |
221 | |
222 // Remove before completing. | |
223 EXPECT_DEATH(queue.Remove(first_token), ""); | |
224 } | |
225 | |
226 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) { | |
227 Queue queue(1); | |
228 const size_t first_token = queue.NewToken(); | |
229 int first_counter = 0; | |
230 int first_abort_counter = 0; | 183 int first_abort_counter = 0; |
231 queue.Enqueue(first_token, | 184 queue.Enqueue(first_token, |
232 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 185 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
233 | 186 |
234 base::RunLoop().RunUntilIdle(); | |
235 | |
236 // Run, then abort. | |
237 std::vector<base::File::Error> first_abort_callback_log; | 187 std::vector<base::File::Error> first_abort_callback_log; |
238 queue.Abort(first_token); | 188 queue.Abort(first_token); |
239 | 189 |
240 EXPECT_DEATH(queue.Complete(first_token), ""); | 190 EXPECT_DEATH(queue.Complete(first_token), ""); |
241 } | 191 } |
242 | 192 |
243 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterCompleting) { | 193 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterCompleting) { |
244 Queue queue(1); | 194 Queue queue(1); |
245 const size_t first_token = queue.NewToken(); | 195 const size_t first_token = queue.NewToken(); |
246 int first_counter = 0; | 196 int first_counter = 0; |
(...skipping 28 matching lines...) Expand all Loading... |
275 int first_abort_counter = 0; | 225 int first_abort_counter = 0; |
276 queue.Enqueue(first_token, | 226 queue.Enqueue(first_token, |
277 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | 227 base::Bind(&OnRun, &first_counter, &first_abort_counter)); |
278 | 228 |
279 base::RunLoop().RunUntilIdle(); | 229 base::RunLoop().RunUntilIdle(); |
280 | 230 |
281 queue.Abort(first_token); | 231 queue.Abort(first_token); |
282 EXPECT_DEATH(queue.Abort(first_token), ""); | 232 EXPECT_DEATH(queue.Abort(first_token), ""); |
283 } | 233 } |
284 | 234 |
285 TEST_F(FileSystemProviderQueueTest, InvalidUsage_IsAbortedWhileNotInQueue) { | |
286 Queue queue(1); | |
287 EXPECT_DEATH(queue.IsAborted(1234), ""); | |
288 } | |
289 | |
290 TEST_F(FileSystemProviderQueueTest, InvalidUsage_IsAbortedAfterRemoved) { | |
291 Queue queue(1); | |
292 const size_t first_token = queue.NewToken(); | |
293 int first_counter = 0; | |
294 int first_abort_counter = 0; | |
295 queue.Enqueue(first_token, | |
296 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
297 | |
298 base::RunLoop().RunUntilIdle(); | |
299 | |
300 queue.Abort(first_token); | |
301 queue.Remove(first_token); | |
302 EXPECT_DEATH(queue.IsAborted(first_token), ""); | |
303 } | |
304 | |
305 TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) { | |
306 Queue queue(1); | |
307 const size_t first_token = queue.NewToken(); | |
308 int first_counter = 0; | |
309 int first_abort_counter = 0; | |
310 queue.Enqueue(first_token, | |
311 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
312 | |
313 base::RunLoop().RunUntilIdle(); | |
314 | |
315 queue.Complete(first_token); | |
316 queue.Remove(first_token); | |
317 EXPECT_DEATH(queue.Complete(first_token), ""); | |
318 } | |
319 | |
320 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortAfterRemoving) { | |
321 Queue queue(1); | |
322 const size_t first_token = queue.NewToken(); | |
323 int first_counter = 0; | |
324 int first_abort_counter = 0; | |
325 queue.Enqueue(first_token, | |
326 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
327 | |
328 base::RunLoop().RunUntilIdle(); | |
329 | |
330 queue.Complete(first_token); | |
331 queue.Remove(first_token); | |
332 EXPECT_DEATH(queue.Abort(first_token), ""); | |
333 } | |
334 | |
335 TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterRemoving) { | |
336 Queue queue(1); | |
337 const size_t first_token = queue.NewToken(); | |
338 int first_counter = 0; | |
339 int first_abort_counter = 0; | |
340 queue.Enqueue(first_token, | |
341 base::Bind(&OnRun, &first_counter, &first_abort_counter)); | |
342 | |
343 base::RunLoop().RunUntilIdle(); | |
344 | |
345 queue.Complete(first_token); | |
346 queue.Remove(first_token); | |
347 EXPECT_DEATH(queue.Complete(first_token), ""); | |
348 } | |
349 | |
350 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortNonAbortable) { | 235 TEST_F(FileSystemProviderQueueTest, InvalidUsage_AbortNonAbortable) { |
351 Queue queue(1); | 236 Queue queue(1); |
352 const size_t first_token = queue.NewToken(); | 237 const size_t first_token = queue.NewToken(); |
353 int first_counter = 0; | 238 int first_counter = 0; |
354 int first_abort_counter = 0; | 239 int first_abort_counter = 0; |
355 queue.Enqueue(first_token, base::Bind(&OnRunNonAbortable, &first_counter, | 240 queue.Enqueue(first_token, base::Bind(&OnRunNonAbortable, &first_counter, |
356 &first_abort_counter)); | 241 &first_abort_counter)); |
357 | 242 |
358 base::RunLoop().RunUntilIdle(); | 243 base::RunLoop().RunUntilIdle(); |
359 | 244 |
(...skipping 16 matching lines...) Expand all Loading... |
376 queue.Enqueue(second_token, | 261 queue.Enqueue(second_token, |
377 base::Bind(&OnRun, &second_counter, &second_abort_counter)); | 262 base::Bind(&OnRun, &second_counter, &second_abort_counter)); |
378 | 263 |
379 base::RunLoop().RunUntilIdle(); | 264 base::RunLoop().RunUntilIdle(); |
380 EXPECT_EQ(1, first_counter); | 265 EXPECT_EQ(1, first_counter); |
381 EXPECT_EQ(0, first_abort_counter); | 266 EXPECT_EQ(0, first_abort_counter); |
382 EXPECT_EQ(0, second_counter); | 267 EXPECT_EQ(0, second_counter); |
383 EXPECT_EQ(0, second_abort_counter); | 268 EXPECT_EQ(0, second_abort_counter); |
384 | 269 |
385 // Abort the first task while it's being executed. | 270 // Abort the first task while it's being executed. |
386 EXPECT_FALSE(queue.IsAborted(first_token)); | |
387 queue.Abort(first_token); | 271 queue.Abort(first_token); |
388 EXPECT_TRUE(queue.IsAborted(first_token)); | 272 queue.Complete(first_token); |
389 queue.Remove(first_token); | |
390 | 273 |
391 // Abort the second task, before it's started. | 274 // Abort the second task, before it's started. |
392 EXPECT_EQ(0, second_counter); | 275 EXPECT_EQ(0, second_counter); |
393 EXPECT_FALSE(queue.IsAborted(second_token)); | |
394 queue.Abort(second_token); | 276 queue.Abort(second_token); |
395 EXPECT_TRUE(queue.IsAborted(second_token)); | |
396 queue.Remove(second_token); | |
397 | 277 |
398 base::RunLoop().RunUntilIdle(); | 278 base::RunLoop().RunUntilIdle(); |
399 EXPECT_EQ(1, first_counter); | 279 EXPECT_EQ(1, first_counter); |
400 EXPECT_EQ(1, first_abort_counter); | 280 EXPECT_EQ(1, first_abort_counter); |
401 EXPECT_EQ(0, second_counter); | 281 EXPECT_EQ(0, second_counter); |
402 EXPECT_EQ(0, second_abort_counter); | 282 EXPECT_EQ(0, second_abort_counter); |
403 } | 283 } |
404 | 284 |
405 } // namespace file_system_provider | 285 } // namespace file_system_provider |
406 } // namespace chromeos | 286 } // namespace chromeos |
OLD | NEW |