]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab ft=cpp | |
3 | ||
4 | /* | |
5 | * Ceph - scalable distributed file system | |
6 | * | |
7 | * Copyright (C) 2020 Red Hat, Inc | |
8 | * | |
9 | * This is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License version 2.1, as published by the Free Software | |
12 | * Foundation. See file COPYING. | |
13 | */ | |
14 | ||
15 | #include "rgw/rgw_bucket_sync_cache.h" | |
16 | #include <gtest/gtest.h> | |
17 | ||
18 | using namespace rgw::bucket_sync; | |
19 | ||
20 | // helper function to construct rgw_bucket_shard | |
21 | static rgw_bucket_shard make_key(const std::string& tenant, | |
22 | const std::string& bucket, int shard) | |
23 | { | |
24 | auto key = rgw_bucket_key{tenant, bucket}; | |
25 | return rgw_bucket_shard{key, shard}; | |
26 | } | |
27 | ||
28 | TEST(BucketSyncCache, ReturnCachedPinned) | |
29 | { | |
30 | auto cache = Cache::create(0); | |
31 | const auto key = make_key("", "1", 0); | |
32 | auto h1 = cache->get(key); // pin | |
33 | h1->counter = 1; | |
34 | auto h2 = cache->get(key); | |
35 | EXPECT_EQ(1, h2->counter); | |
36 | } | |
37 | ||
38 | TEST(BucketSyncCache, ReturnNewUnpinned) | |
39 | { | |
40 | auto cache = Cache::create(0); | |
41 | const auto key = make_key("", "1", 0); | |
42 | cache->get(key)->counter = 1; // pin+unpin | |
43 | EXPECT_EQ(0, cache->get(key)->counter); | |
44 | } | |
45 | ||
46 | TEST(BucketSyncCache, DistinctTenant) | |
47 | { | |
48 | auto cache = Cache::create(2); | |
49 | const auto key1 = make_key("a", "bucket", 0); | |
50 | const auto key2 = make_key("b", "bucket", 0); | |
51 | cache->get(key1)->counter = 1; | |
52 | EXPECT_EQ(0, cache->get(key2)->counter); | |
53 | } | |
54 | ||
55 | TEST(BucketSyncCache, DistinctShards) | |
56 | { | |
57 | auto cache = Cache::create(2); | |
58 | const auto key1 = make_key("", "bucket", 0); | |
59 | const auto key2 = make_key("", "bucket", 1); | |
60 | cache->get(key1)->counter = 1; | |
61 | EXPECT_EQ(0, cache->get(key2)->counter); | |
62 | } | |
63 | ||
64 | TEST(BucketSyncCache, DontEvictPinned) | |
65 | { | |
66 | auto cache = Cache::create(0); | |
67 | ||
68 | const auto key1 = make_key("", "1", 0); | |
69 | const auto key2 = make_key("", "2", 0); | |
70 | ||
71 | auto h1 = cache->get(key1); | |
72 | EXPECT_EQ(key1, h1->key); | |
73 | auto h2 = cache->get(key2); | |
74 | EXPECT_EQ(key2, h2->key); | |
75 | EXPECT_EQ(key1, h1->key); // h1 unchanged | |
76 | } | |
77 | ||
78 | TEST(BucketSyncCache, HandleLifetime) | |
79 | { | |
80 | const auto key = make_key("", "1", 0); | |
81 | ||
82 | Handle h; // test that handles keep the cache referenced | |
83 | { | |
84 | auto cache = Cache::create(0); | |
85 | h = cache->get(key); | |
86 | } | |
87 | EXPECT_EQ(key, h->key); | |
88 | } | |
89 | ||
90 | TEST(BucketSyncCache, TargetSize) | |
91 | { | |
92 | auto cache = Cache::create(2); | |
93 | ||
94 | const auto key1 = make_key("", "1", 0); | |
95 | const auto key2 = make_key("", "2", 0); | |
96 | const auto key3 = make_key("", "3", 0); | |
97 | ||
98 | // fill cache up to target_size=2 | |
99 | cache->get(key1)->counter = 1; | |
100 | cache->get(key2)->counter = 2; | |
101 | // test that each unpinned entry is still cached | |
102 | EXPECT_EQ(1, cache->get(key1)->counter); | |
103 | EXPECT_EQ(2, cache->get(key2)->counter); | |
104 | // overflow the cache and recycle key1 | |
105 | cache->get(key3)->counter = 3; | |
106 | // test that the oldest entry was recycled | |
107 | EXPECT_EQ(0, cache->get(key1)->counter); | |
108 | } | |
109 | ||
110 | TEST(BucketSyncCache, HandleMoveAssignEmpty) | |
111 | { | |
112 | auto cache = Cache::create(0); | |
113 | ||
114 | const auto key1 = make_key("", "1", 0); | |
115 | const auto key2 = make_key("", "2", 0); | |
116 | ||
117 | Handle j1; | |
118 | { | |
119 | auto h1 = cache->get(key1); | |
120 | j1 = std::move(h1); // assign over empty handle | |
121 | EXPECT_EQ(key1, j1->key); | |
122 | } | |
123 | auto h2 = cache->get(key2); | |
124 | EXPECT_EQ(key1, j1->key); // j1 stays pinned | |
125 | } | |
126 | ||
127 | TEST(BucketSyncCache, HandleMoveAssignExisting) | |
128 | { | |
129 | const auto key1 = make_key("", "1", 0); | |
130 | const auto key2 = make_key("", "2", 0); | |
131 | ||
132 | Handle h1; | |
133 | { | |
134 | auto cache1 = Cache::create(0); | |
135 | h1 = cache1->get(key1); | |
136 | } // j1 has the last ref to cache1 | |
137 | { | |
138 | auto cache2 = Cache::create(0); | |
139 | auto h2 = cache2->get(key2); | |
140 | h1 = std::move(h2); // assign over existing handle | |
141 | } | |
142 | EXPECT_EQ(key2, h1->key); | |
143 | } | |
144 | ||
145 | TEST(BucketSyncCache, HandleCopyAssignEmpty) | |
146 | { | |
147 | auto cache = Cache::create(0); | |
148 | ||
149 | const auto key1 = make_key("", "1", 0); | |
150 | const auto key2 = make_key("", "2", 0); | |
151 | ||
152 | Handle j1; | |
153 | { | |
154 | auto h1 = cache->get(key1); | |
155 | j1 = h1; // assign over empty handle | |
156 | EXPECT_EQ(&*h1, &*j1); | |
157 | } | |
158 | auto h2 = cache->get(key2); | |
159 | EXPECT_EQ(key1, j1->key); // j1 stays pinned | |
160 | } | |
161 | ||
162 | TEST(BucketSyncCache, HandleCopyAssignExisting) | |
163 | { | |
164 | const auto key1 = make_key("", "1", 0); | |
165 | const auto key2 = make_key("", "2", 0); | |
166 | ||
167 | Handle h1; | |
168 | { | |
169 | auto cache1 = Cache::create(0); | |
170 | h1 = cache1->get(key1); | |
171 | } // j1 has the last ref to cache1 | |
172 | { | |
173 | auto cache2 = Cache::create(0); | |
174 | auto h2 = cache2->get(key2); | |
175 | h1 = h2; // assign over existing handle | |
176 | EXPECT_EQ(&*h1, &*h2); | |
177 | } | |
178 | EXPECT_EQ(key2, h1->key); | |
179 | } |