huangcm
2025-02-24 69ed55dec4b2116a19e4cca4393cbc014fce5fb2
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
// Copyright 2017 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 "base/threading/sequence_local_storage_map.h"
 
#include <memory>
#include <utility>
 
#include "testing/gtest/include/gtest/gtest.h"
 
namespace base {
namespace internal {
 
namespace {
 
constexpr int kSlotId = 1;
 
class SetOnDestroy {
 public:
  SetOnDestroy(bool* was_destroyed_ptr)
      : was_destroyed_ptr_(was_destroyed_ptr) {
    DCHECK(was_destroyed_ptr_);
    DCHECK(!(*was_destroyed_ptr_));
  }
  ~SetOnDestroy() {
    DCHECK(!(*was_destroyed_ptr_));
    *was_destroyed_ptr_ = true;
  }
 
 private:
  bool* const was_destroyed_ptr_;
 
  DISALLOW_COPY_AND_ASSIGN(SetOnDestroy);
};
 
template <typename T, typename... Args>
SequenceLocalStorageMap::ValueDestructorPair CreateValueDestructorPair(
    Args... args) {
  T* value = new T(args...);
  SequenceLocalStorageMap::ValueDestructorPair::DestructorFunc* destructor =
      [](void* ptr) { std::default_delete<T>()(static_cast<T*>(ptr)); };
 
  SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair{
      value, destructor};
 
  return value_destructor_pair;
}
 
}  // namespace
 
// Verify that setting a value in the SequenceLocalStorageMap, then getting
// it will yield the same value.
TEST(SequenceLocalStorageMapTest, SetGet) {
  SequenceLocalStorageMap sequence_local_storage_map;
  ScopedSetSequenceLocalStorageMapForCurrentThread
      scoped_sequence_local_storage_map(&sequence_local_storage_map);
 
  SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
      CreateValueDestructorPair<int>(5);
 
  sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
 
  EXPECT_EQ(*static_cast<int*>(sequence_local_storage_map.Get(kSlotId)), 5);
}
 
// Verify that the destructor is called on a value stored in the
// SequenceLocalStorageMap when SequenceLocalStorageMap is destroyed.
TEST(SequenceLocalStorageMapTest, Destructor) {
  bool set_on_destruction = false;
 
  {
    SequenceLocalStorageMap sequence_local_storage_map;
    ScopedSetSequenceLocalStorageMapForCurrentThread
        scoped_sequence_local_storage_map(&sequence_local_storage_map);
 
    SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
        CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction);
 
    sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
  }
 
  EXPECT_TRUE(set_on_destruction);
}
 
// Verify that overwriting a value already in the SequenceLocalStorageMap
// calls value's destructor.
TEST(SequenceLocalStorageMapTest, DestructorCalledOnSetOverwrite) {
  bool set_on_destruction = false;
  bool set_on_destruction2 = false;
  {
    SequenceLocalStorageMap sequence_local_storage_map;
    ScopedSetSequenceLocalStorageMapForCurrentThread
        scoped_sequence_local_storage_map(&sequence_local_storage_map);
 
    SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair =
        CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction);
    SequenceLocalStorageMap::ValueDestructorPair value_destructor_pair2 =
        CreateValueDestructorPair<SetOnDestroy>(&set_on_destruction2);
 
    sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair));
 
    ASSERT_FALSE(set_on_destruction);
 
    // Overwrites the old value in the slot.
    sequence_local_storage_map.Set(kSlotId, std::move(value_destructor_pair2));
 
    // Destructor should've been called for the old value in the slot, and not
    // yet called for the new value.
    EXPECT_TRUE(set_on_destruction);
    EXPECT_FALSE(set_on_destruction2);
  }
  EXPECT_TRUE(set_on_destruction2);
}
 
}  // namespace internal
}  // namespace base