backend_cleanup_tracker_unittest.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright (c) 2017 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. #include "net/disk_cache/backend_cleanup_tracker.h"
  5. #include "base/bind.h"
  6. #include "base/callback.h"
  7. #include "base/files/scoped_temp_dir.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "net/test/test_with_task_environment.h"
  10. #include "testing/gmock/include/gmock/gmock.h"
  11. #include "testing/gtest/include/gtest/gtest.h"
  12. namespace disk_cache {
  13. namespace {
  14. using testing::UnorderedElementsAre;
  15. using testing::IsEmpty;
  16. class BackendCleanupTrackerTest : public net::TestWithTaskEnvironment {
  17. protected:
  18. BackendCleanupTrackerTest() = default;
  19. void SetUp() override {
  20. testing::Test::SetUp();
  21. ASSERT_TRUE(tmp_dir_.CreateUniqueTempDir());
  22. // Create two unique paths.
  23. path1_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("a"));
  24. path2_ = tmp_dir_.GetPath().Append(FILE_PATH_LITERAL("b"));
  25. }
  26. void RecordCall(int val) { called_.push_back(val); }
  27. base::OnceClosure RecordCallClosure(int val) {
  28. return base::BindOnce(&BackendCleanupTrackerTest::RecordCall,
  29. base::Unretained(this), val);
  30. }
  31. base::ScopedTempDir tmp_dir_;
  32. base::FilePath path1_;
  33. base::FilePath path2_;
  34. std::vector<int> called_;
  35. };
  36. TEST_F(BackendCleanupTrackerTest, DistinctPath) {
  37. scoped_refptr<BackendCleanupTracker> t1 =
  38. BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(1));
  39. scoped_refptr<BackendCleanupTracker> t2 =
  40. BackendCleanupTracker::TryCreate(path2_, RecordCallClosure(2));
  41. // Both should be created immediately (since the paths are distinct), none of
  42. // the callbacks should be invoked.
  43. ASSERT_TRUE(t1 != nullptr);
  44. ASSERT_TRUE(t2 != nullptr);
  45. RunUntilIdle();
  46. EXPECT_TRUE(called_.empty());
  47. t1->AddPostCleanupCallback(RecordCallClosure(3));
  48. t2->AddPostCleanupCallback(RecordCallClosure(4));
  49. t2->AddPostCleanupCallback(RecordCallClosure(5));
  50. // Just adding callbacks doesn't run them, nor just an event loop.
  51. EXPECT_TRUE(called_.empty());
  52. RunUntilIdle();
  53. EXPECT_TRUE(called_.empty());
  54. t1 = nullptr;
  55. // Callbacks are not invoked immediately.
  56. EXPECT_TRUE(called_.empty());
  57. // ... but via the event loop.
  58. RunUntilIdle();
  59. EXPECT_THAT(called_, UnorderedElementsAre(3));
  60. // Now cleanup t2.
  61. t2 = nullptr;
  62. EXPECT_THAT(called_, UnorderedElementsAre(3));
  63. RunUntilIdle();
  64. EXPECT_THAT(called_, UnorderedElementsAre(3, 4, 5));
  65. }
  66. TEST_F(BackendCleanupTrackerTest, SamePath) {
  67. scoped_refptr<BackendCleanupTracker> t1 =
  68. BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(1));
  69. scoped_refptr<BackendCleanupTracker> t2 =
  70. BackendCleanupTracker::TryCreate(path1_, RecordCallClosure(2));
  71. // Since path is the same, only first call succeeds. No callback yet,
  72. // since t1 controls the path.
  73. ASSERT_TRUE(t1 != nullptr);
  74. EXPECT_TRUE(t2 == nullptr);
  75. RunUntilIdle();
  76. EXPECT_TRUE(called_.empty());
  77. t1->AddPostCleanupCallback(RecordCallClosure(3));
  78. t1->AddPostCleanupCallback(RecordCallClosure(4));
  79. // Create an alias denoting work in progress.
  80. scoped_refptr<BackendCleanupTracker> alias = t1;
  81. t1 = nullptr;
  82. EXPECT_TRUE(called_.empty());
  83. RunUntilIdle();
  84. EXPECT_TRUE(called_.empty());
  85. alias = nullptr;
  86. EXPECT_TRUE(called_.empty());
  87. RunUntilIdle();
  88. // Both the callback passed to the TryCreate that failed and ones passed to
  89. // AddPostCleanupCallback are called.
  90. EXPECT_THAT(called_, UnorderedElementsAre(2, 3, 4));
  91. }
  92. } // namespace
  93. } // namespace disk_cache