MCPcopy Create free account
hub / github.com/apache/trafficserver / testRefcounting

Function testRefcounting

src/iocore/hostdb/test_RefCountCache.cc:131–178  ·  view source on GitHub ↗

TODO: check that the memory was actually free-d better

Source from the content-addressed store, hash-verified

129
130// TODO: check that the memory was actually free-d better
131int
132testRefcounting()
133{
134 int ret = 0;
135
136 RefCountCache<ExampleStruct> *cache = new RefCountCache<ExampleStruct>(4);
137
138 // Create and then immediately delete an item
139 ExampleStruct *to_delete = ExampleStruct::alloc();
140 ret |= to_delete->refcount() != 0;
141 cache->put(1, to_delete);
142 ret |= to_delete->refcount() != 1;
143 cache->erase(1);
144 ret |= to_delete->refcount() != 0;
145 ret |= to_delete->idx != -1;
146
147 // Set an item in the cache
148 ExampleStruct *tmp = ExampleStruct::alloc();
149 ret |= tmp->refcount() != 0;
150 printf("ret=%d ref=%d\n", ret, tmp->refcount());
151 cache->put(static_cast<uint64_t>(1), tmp);
152 ret |= tmp->refcount() != 1;
153 printf("ret=%d ref=%d\n", ret, tmp->refcount());
154 tmp->idx = 1;
155
156 // Grab a pointer to item 1
157 Ptr<ExampleStruct> ccitem = cache->get(static_cast<uint64_t>(1));
158 ret |= tmp->refcount() != 2;
159 printf("ret=%d ref=%d\n", ret, tmp->refcount());
160
161 Ptr<ExampleStruct> tmpAfter = cache->get(static_cast<uint64_t>(1));
162 ret |= tmp->refcount() != 3;
163 printf("ret=%d ref=%d\n", ret, tmp->refcount());
164
165 // Delete a single item
166 cache->erase(1);
167 ret |= tmp->refcount() != 2;
168 printf("ret=%d ref=%d\n", ret, tmp->refcount());
169 // verify that it still isn't in there
170 ret |= cache->get(1).get() != nullptr;
171 printf("ret=%d ref=%d\n", ret, tmp->refcount());
172 ret |= tmpAfter.get()->idx != 1;
173 printf("ret=%d ref=%d\n", ret, tmp->refcount());
174
175 delete cache;
176
177 return ret;
178}
179
180int
181testclear()

Callers 1

testFunction · 0.85

Calls 4

refcountMethod · 0.80
putMethod · 0.45
eraseMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected