-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathbackend_cleanup_tracker_unittest.cc
115 lines (93 loc) · 3.5 KB
/
backend_cleanup_tracker_unittest.cc
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/disk_cache/backend_cleanup_tracker.h"
#include "base/files/scoped_temp_dir.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/ref_counted.h"
#include "net/test/test_with_task_environment.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace disk_cache {
namespace {
using testing::UnorderedElementsAre;
using testing::IsEmpty;
class BackendCleanupTrackerTest : public net::TestWithTaskEnvironment {
protected:
BackendCleanupTrackerTest() = default;
void SetUp() override {
testing::Test::SetUp();
ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
// Create two unique paths.
path1_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("a"));
path2_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("b"));
}
void RecordCall(int val) { called_.push_back(val); }
base::OnceClosure RecordCallClosure(int val) {
return base::BindOnce(&BackendCleanupTrackerTest::RecordCall,
base::Unretained(this), val);
}
base::ScopedTempDir tmp_dir_;
base::FilePath path1_;
base::FilePath path2_;
std::vector<int> called_;
};
TEST_F(BackendCleanupTrackerTest, DistinctPath) {
scoped_refptr<BackendCleanupTracker> t1 =
BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(1));
scoped_refptr<BackendCleanupTracker> t2 =
BackendCleanupTracker::TryCreate(path2_, RecordCallClosure(2));
// Both should be created immediately (since the paths are distinct), none of
// the callbacks should be invoked.
ASSERT_TRUE(t1 != nullptr);
ASSERT_TRUE(t2 != nullptr);
RunUntilIdle();
EXPECT_TRUE(called_.empty());
t1->AddPostCleanupCallback(RecordCallClosure(3));
t2->AddPostCleanupCallback(RecordCallClosure(4));
t2->AddPostCleanupCallback(RecordCallClosure(5));
// Just adding callbacks doesn't run them, nor just an event loop.
EXPECT_TRUE(called_.empty());
RunUntilIdle();
EXPECT_TRUE(called_.empty());
t1 = nullptr;
// Callbacks are not invoked immediately.
EXPECT_TRUE(called_.empty());
// ... but via the event loop.
RunUntilIdle();
EXPECT_THAT(called_, UnorderedElementsAre(3));
// Now cleanup t2.
t2 = nullptr;
EXPECT_THAT(called_, UnorderedElementsAre(3));
RunUntilIdle();
EXPECT_THAT(called_, UnorderedElementsAre(3, 4, 5));
}
TEST_F(BackendCleanupTrackerTest, SamePath) {
scoped_refptr<BackendCleanupTracker> t1 =
BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(1));
scoped_refptr<BackendCleanupTracker> t2 =
BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(2));
// Since path is the same, only first call succeeds. No callback yet,
// since t1 controls the path.
ASSERT_TRUE(t1 != nullptr);
EXPECT_TRUE(t2 == nullptr);
RunUntilIdle();
EXPECT_TRUE(called_.empty());
t1->AddPostCleanupCallback(RecordCallClosure(3));
t1->AddPostCleanupCallback(RecordCallClosure(4));
// Create an alias denoting work in progress.
scoped_refptr<BackendCleanupTracker> alias = t1;
t1 = nullptr;
EXPECT_TRUE(called_.empty());
RunUntilIdle();
EXPECT_TRUE(called_.empty());
alias = nullptr;
EXPECT_TRUE(called_.empty());
RunUntilIdle();
// Both the callback passed to the TryCreate that failed and ones passed to
// AddPostCleanupCallback are called.
EXPECT_THAT(called_, UnorderedElementsAre(2, 3, 4));
}
} // namespace
} // namespace disk_cache