summaryrefslogtreecommitdiffstats
path: root/net/base/expiring_cache_unittest.cc
blob: 74b069dd8d826da1ef7f22e2161ac52e45c8e988 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright (c) 2012 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 "net/base/expiring_cache.h"

#include <functional>
#include <string>

#include "base/stl_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

using testing::Pointee;
using testing::StrEq;

namespace net {

namespace {

const int kMaxCacheEntries = 10;
typedef ExpiringCache<std::string, std::string, base::TimeTicks,
                      std::less<base::TimeTicks> > Cache;

struct TestFunctor {
  bool operator()(const std::string& now,
                  const std::string& expiration) const {
    return now != expiration;
  }
};

}  // namespace

TEST(ExpiringCacheTest, Basic) {
  const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);

  Cache cache(kMaxCacheEntries);

  // Start at t=0.
  base::TimeTicks now;
  EXPECT_EQ(0U, cache.size());

  // Add an entry at t=0
  EXPECT_FALSE(cache.Get("entry1", now));
  cache.Put("entry1", "test1", now, now + kTTL);
  EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
  EXPECT_EQ(1U, cache.size());

  // Advance to t=5.
  now += base::TimeDelta::FromSeconds(5);

  // Add an entry at t=5.
  EXPECT_FALSE(cache.Get("entry2", now));
  cache.Put("entry2", "test2", now, now + kTTL);
  EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));
  EXPECT_EQ(2U, cache.size());

  // Advance to t=9.
  now += base::TimeDelta::FromSeconds(4);

  // Verify that the entries added are still retrievable and usable.
  EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
  EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));

  // Advance to t=10; entry1 is now expired.
  now += base::TimeDelta::FromSeconds(1);

  EXPECT_FALSE(cache.Get("entry1", now));
  EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));

  // The expired element should no longer be in the cache.
  EXPECT_EQ(1U, cache.size());

  // Update entry1 so it is no longer expired.
  cache.Put("entry1", "test1", now, now + kTTL);

  // Both entries should be retrievable and usable.
  EXPECT_EQ(2U, cache.size());
  EXPECT_THAT(cache.Get("entry1", now), Pointee(StrEq("test1")));
  EXPECT_THAT(cache.Get("entry2", now), Pointee(StrEq("test2")));

  // Advance to t=20; both entries are now expired.
  now += base::TimeDelta::FromSeconds(10);

  EXPECT_FALSE(cache.Get("entry1", now));
  EXPECT_FALSE(cache.Get("entry2", now));
}

TEST(ExpiringCacheTest, Compact) {
  const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);

  Cache cache(kMaxCacheEntries);

  // Start at t=0.
  base::TimeTicks now;
  EXPECT_EQ(0U, cache.size());

  // Add five valid entries at t=10 that expire at t=20.
  base::TimeTicks t10 = now + kTTL;
  for (int i = 0; i < 5; ++i) {
    std::string name = base::StringPrintf("valid%d", i);
    cache.Put(name, "I'm valid!", t10, t10 + kTTL);
  }
  EXPECT_EQ(5U, cache.size());

  // Add three entries at t=0 that expire at t=10.
  for (int i = 0; i < 3; ++i) {
    std::string name = base::StringPrintf("expired%d", i);
    cache.Put(name, "I'm expired.", now, t10);
  }
  EXPECT_EQ(8U, cache.size());

  // Add two negative (instantly expired) entries at t=0 that expire at t=0.
  for (int i = 0; i < 2; ++i) {
    std::string name = base::StringPrintf("negative%d", i);
    cache.Put(name, "I was never valid.", now, now);
  }
  EXPECT_EQ(10U, cache.size());

  EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "expired0"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "expired1"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "expired2"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "negative0"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "negative1"));

  // Shrink the new max constraints bound and compact. The "negative" and
  // "expired" entries should be dropped.
  cache.max_entries_ = 6;
  cache.Compact(now);
  EXPECT_EQ(5U, cache.size());

  EXPECT_TRUE(ContainsKey(cache.entries_, "valid0"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid1"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid2"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid3"));
  EXPECT_TRUE(ContainsKey(cache.entries_, "valid4"));
  EXPECT_FALSE(ContainsKey(cache.entries_, "expired0"));
  EXPECT_FALSE(ContainsKey(cache.entries_, "expired1"));
  EXPECT_FALSE(ContainsKey(cache.entries_, "expired2"));
  EXPECT_FALSE(ContainsKey(cache.entries_, "negative0"));
  EXPECT_FALSE(ContainsKey(cache.entries_, "negative1"));

  // Shrink further -- this time the compact will start dropping valid entries
  // to make space.
  cache.max_entries_ = 4;
  cache.Compact(now);
  EXPECT_EQ(3U, cache.size());
}

// Add entries while the cache is at capacity, causing evictions.
TEST(ExpiringCacheTest, SetWithCompact) {
  const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);

  Cache cache(3);

  // t=10
  base::TimeTicks now = base::TimeTicks() + kTTL;

  cache.Put("test1", "test1", now, now + kTTL);
  cache.Put("test2", "test2", now, now + kTTL);
  cache.Put("expired", "expired", now, now);

  EXPECT_EQ(3U, cache.size());

  // Should all be retrievable except "expired".
  EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
  EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
  EXPECT_FALSE(cache.Get("expired", now));

  // Adding the fourth entry will cause "expired" to be evicted.
  cache.Put("test3", "test3", now, now + kTTL);
  EXPECT_EQ(3U, cache.size());

  EXPECT_FALSE(cache.Get("expired", now));
  EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("test1")));
  EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("test2")));
  EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("test3")));

  // Add two more entries. Something should be evicted, however "test5"
  // should definitely be in there (since it was last inserted).
  cache.Put("test4", "test4", now, now + kTTL);
  EXPECT_EQ(3U, cache.size());
  cache.Put("test5", "test5", now, now + kTTL);
  EXPECT_EQ(3U, cache.size());
  EXPECT_THAT(cache.Get("test5", now), Pointee(StrEq("test5")));
}

TEST(ExpiringCacheTest, Clear) {
  const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);

  Cache cache(kMaxCacheEntries);

  // Start at t=0.
  base::TimeTicks now;
  EXPECT_EQ(0U, cache.size());

  // Add three entries.
  cache.Put("test1", "foo", now, now + kTTL);
  cache.Put("test2", "foo", now, now + kTTL);
  cache.Put("test3", "foo", now, now + kTTL);
  EXPECT_EQ(3U, cache.size());

  cache.Clear();

  EXPECT_EQ(0U, cache.size());
}

TEST(ExpiringCacheTest, GetTruncatesExpiredEntries) {
  const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);

  Cache cache(kMaxCacheEntries);

  // Start at t=0.
  base::TimeTicks now;
  EXPECT_EQ(0U, cache.size());

  // Add three entries at t=0.
  cache.Put("test1", "foo1", now, now + kTTL);
  cache.Put("test2", "foo2", now, now + kTTL);
  cache.Put("test3", "foo3", now, now + kTTL);
  EXPECT_EQ(3U, cache.size());

  // Ensure the entries were added.
  EXPECT_THAT(cache.Get("test1", now), Pointee(StrEq("foo1")));
  EXPECT_THAT(cache.Get("test2", now), Pointee(StrEq("foo2")));
  EXPECT_THAT(cache.Get("test3", now), Pointee(StrEq("foo3")));

  // Add five entries at t=10.
  now += kTTL;
  for (int i = 0; i < 5; ++i) {
    std::string name = base::StringPrintf("valid%d", i);
    cache.Put(name, name, now, now + kTTL);  // Expire at t=20.
  }
  EXPECT_EQ(8U, cache.size());

  // Now access two expired entries and ensure the cache size goes down.
  EXPECT_FALSE(cache.Get("test1", now));
  EXPECT_FALSE(cache.Get("test2", now));
  EXPECT_EQ(6U, cache.size());

  // Accessing non-expired entries should return entries and not adjust the
  // cache size.
  for (int i = 0; i < 5; ++i) {
    std::string name = base::StringPrintf("valid%d", i);
    EXPECT_THAT(cache.Get(name, now), Pointee(StrEq(name)));
  }
  EXPECT_EQ(6U, cache.size());
}

TEST(ExpiringCacheTest, CustomFunctor) {
  ExpiringCache<std::string, std::string, std::string, TestFunctor> cache(5);

  const std::string kNow("Now");
  const std::string kLater("A little bit later");
  const std::string kMuchLater("Much later");
  const std::string kHeatDeath("The heat death of the universe");

  EXPECT_EQ(0u, cache.size());

  // Add three entries at t=kNow that expire at kLater.
  cache.Put("test1", "foo1", kNow, kLater);
  cache.Put("test2", "foo2", kNow, kLater);
  cache.Put("test3", "foo3", kNow, kLater);
  EXPECT_EQ(3U, cache.size());

  // Add two entries at t=kNow that expire at kMuchLater
  cache.Put("test4", "foo4", kNow, kMuchLater);
  cache.Put("test5", "foo5", kNow, kMuchLater);
  EXPECT_EQ(5U, cache.size());

  // Ensure the entries were added.
  EXPECT_THAT(cache.Get("test1", kNow), Pointee(StrEq("foo1")));
  EXPECT_THAT(cache.Get("test2", kNow), Pointee(StrEq("foo2")));
  EXPECT_THAT(cache.Get("test3", kNow), Pointee(StrEq("foo3")));
  EXPECT_THAT(cache.Get("test4", kNow), Pointee(StrEq("foo4")));
  EXPECT_THAT(cache.Get("test5", kNow), Pointee(StrEq("foo5")));

  // Add one entry at t=kLater that expires at kHeatDeath, which will expire
  // one of test1-3.
  cache.Put("test6", "foo6", kLater, kHeatDeath);
  EXPECT_THAT(cache.Get("test6", kLater), Pointee(StrEq("foo6")));
  EXPECT_EQ(3U, cache.size());

  // Now compact at kMuchLater, which should remove all but "test6".
  cache.max_entries_ = 2;
  cache.Compact(kMuchLater);

  EXPECT_EQ(1U, cache.size());
  EXPECT_THAT(cache.Get("test6", kMuchLater), Pointee(StrEq("foo6")));

  // Finally, "test6" should not be valid at the end of the universe.
  EXPECT_FALSE(cache.Get("test6", kHeatDeath));

  // Because comparison is based on equality, not strict weak ordering, we
  // should be able to add something at kHeatDeath that expires at kMuchLater.
  cache.Put("test7", "foo7", kHeatDeath, kMuchLater);
  EXPECT_EQ(1U, cache.size());
  EXPECT_THAT(cache.Get("test7", kNow), Pointee(StrEq("foo7")));
  EXPECT_THAT(cache.Get("test7", kLater), Pointee(StrEq("foo7")));
  EXPECT_THAT(cache.Get("test7", kHeatDeath), Pointee(StrEq("foo7")));
  EXPECT_FALSE(cache.Get("test7", kMuchLater));
}

}  // namespace net