1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/file_system_provider/queue.h"
#include <vector>
#include "base/files/file.h"
#include "base/run_loop.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace chromeos {
namespace file_system_provider {
namespace {
void OnAbort(int* abort_counter,
const storage::AsyncFileUtil::StatusCallback& callback) {
(*abort_counter)++;
callback.Run(base::File::FILE_ERROR_FAILED);
}
AbortCallback OnRun(int* run_counter, int* abort_counter) {
(*run_counter)++;
return base::Bind(&OnAbort, abort_counter);
}
void OnAbortCallback(std::vector<base::File::Error>* log,
base::File::Error result) {
log->push_back(result);
}
} // namespace
class FileSystemProviderQueueTest : public testing::Test {
protected:
FileSystemProviderQueueTest() {}
virtual ~FileSystemProviderQueueTest() {}
content::TestBrowserThreadBundle thread_bundle_;
};
TEST_F(FileSystemProviderQueueTest, NewToken) {
Queue queue(1);
EXPECT_EQ(1u, queue.NewToken());
EXPECT_EQ(2u, queue.NewToken());
EXPECT_EQ(3u, queue.NewToken());
}
TEST_F(FileSystemProviderQueueTest, Enqueue_OneAtOnce) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
queue.Enqueue(first_token,
base::Bind(&OnRun, &first_counter, &first_abort_counter));
const size_t second_token = queue.NewToken();
int second_counter = 0;
int second_abort_counter = 0;
const AbortCallback abort_callback = queue.Enqueue(
second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(0, second_counter);
EXPECT_EQ(0, second_abort_counter);
// Complete the first task, which should not run the second one, yet.
queue.Complete(first_token);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(0, second_counter);
EXPECT_EQ(0, second_abort_counter);
// Removing the first task from the queue should run the second task.
queue.Remove(first_token);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(1, second_counter);
EXPECT_EQ(0, second_abort_counter);
const size_t third_token = queue.NewToken();
int third_counter = 0;
int third_abort_counter = 0;
queue.Enqueue(third_token,
base::Bind(&OnRun, &third_counter, &third_abort_counter));
// The second task is still running, so the third one is blocked.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(1, second_counter);
EXPECT_EQ(0, second_abort_counter);
EXPECT_EQ(0, third_counter);
EXPECT_EQ(0, third_abort_counter);
// After aborting the second task, the third should run.
std::vector<base::File::Error> abort_callback_log;
abort_callback.Run(base::Bind(&OnAbortCallback, &abort_callback_log));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(1, second_counter);
EXPECT_EQ(1, second_abort_counter);
ASSERT_EQ(1u, abort_callback_log.size());
EXPECT_EQ(base::File::FILE_ERROR_FAILED, abort_callback_log[0]);
EXPECT_EQ(1, third_counter);
EXPECT_EQ(0, third_abort_counter);
}
TEST_F(FileSystemProviderQueueTest, Enqueue_MultipleAtOnce) {
Queue queue(2);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
queue.Enqueue(first_token,
base::Bind(&OnRun, &first_counter, &first_abort_counter));
const size_t second_token = queue.NewToken();
int second_counter = 0;
int second_abort_counter = 0;
queue.Enqueue(second_token,
base::Bind(&OnRun, &second_counter, &second_abort_counter));
const size_t third_token = queue.NewToken();
int third_counter = 0;
int third_abort_counter = 0;
queue.Enqueue(third_token,
base::Bind(&OnRun, &third_counter, &third_abort_counter));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(1, second_counter);
EXPECT_EQ(0, second_abort_counter);
EXPECT_EQ(0, third_counter);
EXPECT_EQ(0, third_abort_counter);
// Completing and removing the second task, should start the last one.
queue.Complete(second_token);
queue.Remove(second_token);
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(1, second_counter);
EXPECT_EQ(0, second_abort_counter);
EXPECT_EQ(1, third_counter);
EXPECT_EQ(0, third_abort_counter);
}
#if !defined(NDEBUG) && defined(GTEST_HAS_DEATH_TEST)
TEST_F(FileSystemProviderQueueTest, InvalidUsage_DuplicatedTokens) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
queue.Enqueue(first_token,
base::Bind(&OnRun, &first_counter, &first_abort_counter));
// Use the first token on purpose.
int second_counter = 0;
int second_abort_counter = 0;
EXPECT_DEATH(queue.Enqueue(first_token, base::Bind(&OnRun, &second_counter,
&second_abort_counter)),
"");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteNotStarted) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
queue.Enqueue(first_token,
base::Bind(&OnRun, &first_counter, &first_abort_counter));
// Completing and removing the first task, which however hasn't started.
// That should not invoke the second task.
EXPECT_DEATH(queue.Complete(first_token), "");
EXPECT_DEATH(queue.Remove(first_token), "");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveNotCompleted) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
queue.Enqueue(first_token,
base::Bind(&OnRun, &first_counter, &first_abort_counter));
base::RunLoop().RunUntilIdle();
// Remove before completing.
EXPECT_DEATH(queue.Remove(first_token), "");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteAfterAborting) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
AbortCallback first_abort_callback = queue.Enqueue(
first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter));
base::RunLoop().RunUntilIdle();
// Run, then abort.
std::vector<base::File::Error> first_abort_callback_log;
first_abort_callback.Run(
base::Bind(&OnAbortCallback, &first_abort_callback_log));
EXPECT_DEATH(queue.Complete(first_token), "");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveAfterAborting) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
AbortCallback first_abort_callback = queue.Enqueue(
first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter));
base::RunLoop().RunUntilIdle();
// Abort after executing.
std::vector<base::File::Error> first_abort_callback_log;
first_abort_callback.Run(
base::Bind(&OnAbortCallback, &first_abort_callback_log));
base::RunLoop().RunUntilIdle();
// Remove before completing.
EXPECT_DEATH(queue.Remove(first_token), "");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_CompleteTwice) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
AbortCallback first_abort_callback = queue.Enqueue(
first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter));
base::RunLoop().RunUntilIdle();
queue.Complete(first_token);
EXPECT_DEATH(queue.Complete(first_token), "");
}
TEST_F(FileSystemProviderQueueTest, InvalidUsage_RemoveTwice) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
AbortCallback first_abort_callback = queue.Enqueue(
first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter));
base::RunLoop().RunUntilIdle();
queue.Complete(first_token);
queue.Remove(first_token);
EXPECT_DEATH(queue.Complete(first_token), "");
}
#endif
TEST_F(FileSystemProviderQueueTest, Enqueue_Abort) {
Queue queue(1);
const size_t first_token = queue.NewToken();
int first_counter = 0;
int first_abort_counter = 0;
const AbortCallback first_abort_callback = queue.Enqueue(
first_token, base::Bind(&OnRun, &first_counter, &first_abort_counter));
const size_t second_token = queue.NewToken();
int second_counter = 0;
int second_abort_counter = 0;
const AbortCallback second_abort_callback = queue.Enqueue(
second_token, base::Bind(&OnRun, &second_counter, &second_abort_counter));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(0, first_abort_counter);
EXPECT_EQ(0, second_counter);
EXPECT_EQ(0, second_abort_counter);
// Abort the first task while it's being executed.
std::vector<base::File::Error> first_abort_callback_log;
first_abort_callback.Run(
base::Bind(&OnAbortCallback, &first_abort_callback_log));
// Abort the second task, before it's started.
EXPECT_EQ(0, second_counter);
std::vector<base::File::Error> second_abort_callback_log;
second_abort_callback.Run(
base::Bind(&OnAbortCallback, &second_abort_callback_log));
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1, first_counter);
EXPECT_EQ(1, first_abort_counter);
ASSERT_EQ(1u, first_abort_callback_log.size());
EXPECT_EQ(base::File::FILE_ERROR_FAILED, first_abort_callback_log[0]);
EXPECT_EQ(0, second_counter);
EXPECT_EQ(0, second_abort_counter);
ASSERT_EQ(1u, second_abort_callback_log.size());
EXPECT_EQ(base::File::FILE_OK, second_abort_callback_log[0]);
// Aborting again, should result in the FILE_ERROR_INVALID_MODIFICATION error
// code.
second_abort_callback.Run(
base::Bind(&OnAbortCallback, &second_abort_callback_log));
ASSERT_EQ(2u, second_abort_callback_log.size());
EXPECT_EQ(base::File::FILE_ERROR_INVALID_OPERATION,
second_abort_callback_log[1]);
}
} // namespace file_system_provider
} // namespace chromeos
|