1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2013 Cloudwatt <libre.licensing@cloudwatt.com>
8 * Author: Loic Dachary <loic@dachary.org>
9 * Cheng Cheng <ccheng.leo@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library Public License for more details.
25 #include "gtest/gtest.h"
26 #include "common/Thread.h"
27 #include "common/shared_cache.hpp"
29 class SharedLRUTest : public SharedLRU<unsigned int, int> {
31 Mutex &get_lock() { return lock; }
32 Cond &get_cond() { return cond; }
33 map<unsigned int, pair< ceph::weak_ptr<int>, int* > > &get_weak_refs() {
38 class SharedLRU_all : public ::testing::Test {
41 class Thread_wait : public Thread {
46 ceph::shared_ptr<int> ptr;
47 enum in_method_t { LOOKUP, LOWER_BOUND } in_method;
49 Thread_wait(SharedLRUTest& _cache, unsigned int _key,
50 int _value, in_method_t _in_method) :
54 in_method(_in_method) { }
56 void * entry() override {
59 ptr = cache.lower_bound(key);
62 ptr = ceph::shared_ptr<int>(new int);
64 ptr = cache.lookup(key);
71 static const useconds_t DELAY_MAX = 20 * 1000 * 1000;
72 static useconds_t delay;
74 bool wait_for(SharedLRUTest &cache, int waitting) {
77 // the delay variable is supposed to be initialized to zero. It would be fine
78 // to usleep(0) but we take this opportunity to test the loop. It will try
79 // again and therefore show that the logic ( increasing the delay ) actually
85 Mutex::Locker l(cache.get_lock());
86 if (cache.waiting == waitting) {
91 cout << "delay " << delay << "us, is not long enough, try again\n";
93 } while ((delay = delay * 2 + 1) < DELAY_MAX);
94 return delay < DELAY_MAX;
98 useconds_t SharedLRU_all::delay = 0;
100 TEST_F(SharedLRU_all, add) {
102 unsigned int key = 1;
104 bool existed = false;
106 ceph::shared_ptr<int> ptr = cache.add(key, new int(value1), &existed);
107 ASSERT_EQ(value1, *ptr);
108 ASSERT_FALSE(existed);
112 ceph::shared_ptr<int> ptr = cache.add(key, new int(value2), &existed);
113 ASSERT_EQ(value1, *ptr);
114 ASSERT_TRUE(existed);
117 TEST_F(SharedLRU_all, empty) {
119 unsigned int key = 1;
120 bool existed = false;
122 ASSERT_TRUE(cache.empty());
125 ceph::shared_ptr<int> ptr = cache.add(key, new int(value1), &existed);
126 ASSERT_EQ(value1, *ptr);
127 ASSERT_FALSE(existed);
129 ASSERT_FALSE(cache.empty());
132 ASSERT_TRUE(cache.empty());
135 TEST_F(SharedLRU_all, lookup) {
137 unsigned int key = 1;
140 ASSERT_TRUE(cache.add(key, new int(value)).get());
141 ASSERT_TRUE(cache.lookup(key).get());
142 ASSERT_EQ(value, *cache.lookup(key));
144 ASSERT_TRUE(cache.lookup(key).get());
146 TEST_F(SharedLRU_all, lookup_or_create) {
150 unsigned int key = 1;
151 ASSERT_TRUE(cache.add(key, new int(value)).get());
152 ASSERT_TRUE(cache.lookup_or_create(key).get());
153 ASSERT_EQ(value, *cache.lookup(key));
156 unsigned int key = 2;
157 ASSERT_TRUE(cache.lookup_or_create(key).get());
158 ASSERT_EQ(0, *cache.lookup(key));
160 ASSERT_TRUE(cache.lookup(1).get());
161 ASSERT_TRUE(cache.lookup(2).get());
164 TEST_F(SharedLRU_all, wait_lookup) {
166 unsigned int key = 1;
170 ceph::shared_ptr<int> ptr(new int);
171 cache.get_weak_refs()[key] = make_pair(ptr, &*ptr);
173 EXPECT_FALSE(cache.get_weak_refs()[key].first.lock());
175 Thread_wait t(cache, key, value, Thread_wait::LOOKUP);
176 t.create("wait_lookup_1");
177 ASSERT_TRUE(wait_for(cache, 1));
178 EXPECT_EQ(value, *t.ptr);
179 // waiting on a key does not block lookups on other keys
180 EXPECT_FALSE(cache.lookup(key + 12345));
182 Mutex::Locker l(cache.get_lock());
183 cache.get_weak_refs().erase(key);
184 cache.get_cond().Signal();
186 ASSERT_TRUE(wait_for(cache, 0));
190 TEST_F(SharedLRU_all, wait_lookup_or_create) {
192 unsigned int key = 1;
196 ceph::shared_ptr<int> ptr(new int);
197 cache.get_weak_refs()[key] = make_pair(ptr, &*ptr);
199 EXPECT_FALSE(cache.get_weak_refs()[key].first.lock());
201 Thread_wait t(cache, key, value, Thread_wait::LOOKUP);
202 t.create("wait_lookup_2");
203 ASSERT_TRUE(wait_for(cache, 1));
204 EXPECT_EQ(value, *t.ptr);
205 // waiting on a key does not block lookups on other keys
206 EXPECT_TRUE(cache.lookup_or_create(key + 12345).get());
208 Mutex::Locker l(cache.get_lock());
209 cache.get_weak_refs().erase(key);
210 cache.get_cond().Signal();
212 ASSERT_TRUE(wait_for(cache, 0));
217 TEST_F(SharedLRU_all, lower_bound) {
221 unsigned int key = 1;
222 ASSERT_FALSE(cache.lower_bound(key));
225 ASSERT_TRUE(cache.add(key, new int(value)).get());
226 ASSERT_TRUE(cache.lower_bound(key).get());
227 EXPECT_EQ(value, *cache.lower_bound(key));
231 TEST_F(SharedLRU_all, wait_lower_bound) {
233 unsigned int key = 1;
235 unsigned int other_key = key + 1;
236 int other_value = value + 1;
238 ASSERT_TRUE(cache.add(other_key, new int(other_value)).get());
241 ceph::shared_ptr<int> ptr(new int);
242 cache.get_weak_refs()[key] = make_pair(ptr, &*ptr);
244 EXPECT_FALSE(cache.get_weak_refs()[key].first.lock());
246 Thread_wait t(cache, key, value, Thread_wait::LOWER_BOUND);
247 t.create("wait_lower_bnd");
248 ASSERT_TRUE(wait_for(cache, 1));
250 // waiting on a key does not block getting lower_bound on other keys
251 EXPECT_TRUE(cache.lower_bound(other_key).get());
253 Mutex::Locker l(cache.get_lock());
254 cache.get_weak_refs().erase(key);
255 cache.get_cond().Signal();
257 ASSERT_TRUE(wait_for(cache, 0));
259 EXPECT_TRUE(t.ptr.get());
261 TEST_F(SharedLRU_all, get_next) {
265 const unsigned int key = 0;
266 pair<unsigned int, int> i;
267 EXPECT_FALSE(cache.get_next(key, &i));
272 const unsigned int key2 = 333;
273 ceph::shared_ptr<int> ptr2 = cache.lookup_or_create(key2);
274 const int value2 = *ptr2 = 400;
276 // entries with expired pointers are silently ignored
277 const unsigned int key_gone = 222;
278 cache.get_weak_refs()[key_gone] = make_pair(ceph::shared_ptr<int>(), (int*)0);
280 const unsigned int key1 = 111;
281 ceph::shared_ptr<int> ptr1 = cache.lookup_or_create(key1);
282 const int value1 = *ptr1 = 800;
284 pair<unsigned int, int> i;
285 EXPECT_TRUE(cache.get_next(0, &i));
286 EXPECT_EQ(key1, i.first);
287 EXPECT_EQ(value1, i.second);
289 EXPECT_TRUE(cache.get_next(i.first, &i));
290 EXPECT_EQ(key2, i.first);
291 EXPECT_EQ(value2, i.second);
293 EXPECT_FALSE(cache.get_next(i.first, &i));
295 cache.get_weak_refs().clear();
299 const unsigned int key1 = 111;
300 ceph::shared_ptr<int> *ptr1 = new shared_ptr<int>(cache.lookup_or_create(key1));
301 const unsigned int key2 = 222;
302 ceph::shared_ptr<int> ptr2 = cache.lookup_or_create(key2);
304 pair<unsigned int, ceph::shared_ptr<int> > i;
305 EXPECT_TRUE(cache.get_next(i.first, &i));
306 EXPECT_EQ(key1, i.first);
308 EXPECT_TRUE(cache.get_next(i.first, &i));
309 EXPECT_EQ(key2, i.first);
313 TEST_F(SharedLRU_all, clear) {
315 unsigned int key = 1;
318 ceph::shared_ptr<int> ptr = cache.add(key, new int(value));
319 ASSERT_EQ(value, *cache.lookup(key));
321 ASSERT_TRUE(cache.lookup(key).get());
323 ASSERT_FALSE(cache.lookup(key));
326 ceph::shared_ptr<int> ptr = cache.add(key, new int(value));
328 ASSERT_TRUE(cache.lookup(key).get());
330 ASSERT_FALSE(cache.lookup(key));
332 TEST_F(SharedLRU_all, clear_all) {
334 unsigned int key = 1;
337 ceph::shared_ptr<int> ptr = cache.add(key, new int(value));
338 ASSERT_EQ(value, *cache.lookup(key));
340 ASSERT_TRUE(cache.lookup(key).get());
342 ASSERT_FALSE(cache.lookup(key));
344 ceph::shared_ptr<int> ptr2 = cache.add(key, new int(value));
345 ASSERT_TRUE(cache.lookup(key).get());
347 ASSERT_TRUE(cache.lookup(key).get());
348 ASSERT_FALSE(cache.empty());
351 TEST(SharedCache_all, add) {
352 SharedLRU<int, int> cache;
353 unsigned int key = 1;
355 ceph::shared_ptr<int> ptr = cache.add(key, new int(value));
356 ASSERT_EQ(ptr, cache.lookup(key));
357 ASSERT_EQ(value, *cache.lookup(key));
360 TEST(SharedCache_all, lru) {
361 const size_t SIZE = 5;
362 SharedLRU<int, int> cache(NULL, SIZE);
364 bool existed = false;
365 ceph::shared_ptr<int> ptr = cache.add(0, new int(0), &existed);
366 ASSERT_FALSE(existed);
368 int *tmpint = new int(0);
369 ceph::shared_ptr<int> ptr2 = cache.add(0, tmpint, &existed);
370 ASSERT_TRUE(existed);
373 for (size_t i = 1; i < 2*SIZE; ++i) {
374 cache.add(i, new int(i), &existed);
375 ASSERT_FALSE(existed);
378 ASSERT_TRUE(cache.lookup(0).get());
379 ASSERT_EQ(0, *cache.lookup(0));
381 ASSERT_FALSE(cache.lookup(SIZE-1));
382 ASSERT_FALSE(cache.lookup(SIZE));
383 ASSERT_TRUE(cache.lookup(SIZE+1).get());
384 ASSERT_EQ((int)SIZE+1, *cache.lookup(SIZE+1));
387 ASSERT_FALSE(cache.lookup(0));
388 ceph::shared_ptr<int> ptr2 = cache.add(0, new int(0), &existed);
389 ASSERT_FALSE(ptr == ptr2);
390 ptr = ceph::shared_ptr<int>();
391 ASSERT_TRUE(cache.lookup(0).get());
395 // compile-command: "cd ../.. ; make unittest_shared_cache && ./unittest_shared_cache # --gtest_filter=*.* --log-to-stderr=true"