sequence_local_storage_map_unittest.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright 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 "base/threading/sequence_local_storage_map.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/memory/raw_ptr.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace base {
  10. namespace internal {
  11. namespace {
  12. constexpr int kSlotId = 1;
  13. class SetOnDestroy {
  14. public:
  15. SetOnDestroy(bool* was_destroyed_ptr)
  16. : was_destroyed_ptr_(was_destroyed_ptr) {
  17. DCHECK(was_destroyed_ptr_);
  18. DCHECK(!(*was_destroyed_ptr_));
  19. }
  20. SetOnDestroy(const SetOnDestroy&) = delete;
  21. SetOnDestroy& operator=(const SetOnDestroy&) = delete;
  22. ~SetOnDestroy() {
  23. DCHECK(!(*was_destroyed_ptr_));
  24. *was_destroyed_ptr_ = true;
  25. }
  26. private:
  27. const raw_ptr<bool> was_destroyed_ptr_;
  28. };
  29. template <typename T, typename... Args>
  30. SequenceLocalStorageMap::ValueDestructorPair CreateValueDestructorPair(
  31. Args... args) {
  32. T* value = new T(args...);
  33. SequenceLocalStorageMap::ValueDestructorPair::DestructorFunc* destructor =
  34. [](void* ptr) { std::default_delete<T>()(static_cast<T*>(ptr)); };
  35. SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair{
  36. value, destructor};
  37. return value_destructor_pair;
  38. }
  39. } // namespace
  40. // Verify that setting a value in the SequenceLocalStorageMap, then getting
  41. // it will yield the same value.
  42. TEST(SequenceLocalStorageMapTest, SetGet) {
  43. SequenceLocalStorageMap sequence_local_storage_map;
  44. ScopedSetSequenceLocalStorageMapForCurrentThread
  45. scoped_sequence_local_storage_map(&sequence_local_storage_map);
  46. SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
  47. CreateValueDestructorPair<int>(5);
  48. sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
  49. EXPECT_EQ(*static_cast<int*>(sequence_local_storage_map.Get(kSlotId)), 5);
  50. }
  51. // Verify that the destructor is called on a value stored in the
  52. // SequenceLocalStorageMap when SequenceLocalStorageMap is destroyed.
  53. TEST(SequenceLocalStorageMapTest, Destructor) {
  54. bool set_on_destruction = false;
  55. {
  56. SequenceLocalStorageMap sequence_local_storage_map;
  57. ScopedSetSequenceLocalStorageMapForCurrentThread
  58. scoped_sequence_local_storage_map(&sequence_local_storage_map);
  59. SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
  60. CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction);
  61. sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
  62. }
  63. EXPECT_TRUE(set_on_destruction);
  64. }
  65. // Verify that overwriting a value already in the SequenceLocalStorageMap
  66. // calls value's destructor.
  67. TEST(SequenceLocalStorageMapTest, DestructorCalledOnSetOverwrite) {
  68. bool set_on_destruction = false;
  69. bool set_on_destruction2 = false;
  70. {
  71. SequenceLocalStorageMap sequence_local_storage_map;
  72. ScopedSetSequenceLocalStorageMapForCurrentThread
  73. scoped_sequence_local_storage_map(&sequence_local_storage_map);
  74. SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
  75. CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction);
  76. SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair2 =
  77. CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction2);
  78. sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
  79. ASSERT_FALSE(set_on_destruction);
  80. // Overwrites the old value in the slot.
  81. sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair2));
  82. // Destructor should've been called for the old value in the slot, and not
  83. // yet called for the new value.
  84. EXPECT_TRUE(set_on_destruction);
  85. EXPECT_FALSE(set_on_destruction2);
  86. }
  87. EXPECT_TRUE(set_on_destruction2);
  88. }
  89. } // namespace internal
  90. } // namespace base