rippled
KeyCache_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/TaggedCache.h>
21 #include <ripple/basics/chrono.h>
22 #include <ripple/beast/clock/manual_clock.h>
23 #include <ripple/beast/unit_test.h>
24 #include <ripple/beast/utility/Journal.h>
25 #include <ripple/protocol/Protocol.h>
26 #include <test/unit_test/SuiteJournal.h>
27 
28 namespace ripple {
29 
30 class KeyCache_test : public beast::unit_test::suite
31 {
32 public:
33  void
34  run() override
35  {
36  using namespace std::chrono_literals;
37  TestStopwatch clock;
38  clock.set(0);
39 
40  using Key = std::string;
41  using Cache = TaggedCache<Key, int, true>;
42 
43  test::SuiteJournal j("KeyCacheTest", *this);
44 
45  // Insert an item, retrieve it, and age it so it gets purged.
46  {
47  Cache c("test", LedgerIndex(1), 2s, clock, j);
48 
49  BEAST_EXPECT(c.size() == 0);
50  BEAST_EXPECT(c.insert("one"));
51  BEAST_EXPECT(!c.insert("one"));
52  BEAST_EXPECT(c.size() == 1);
53  BEAST_EXPECT(c.touch_if_exists("one"));
54  ++clock;
55  c.sweep();
56  BEAST_EXPECT(c.size() == 1);
57  ++clock;
58  c.sweep();
59  BEAST_EXPECT(c.size() == 0);
60  BEAST_EXPECT(!c.touch_if_exists("one"));
61  }
62 
63  // Insert two items, have one expire
64  {
65  Cache c("test", LedgerIndex(2), 2s, clock, j);
66 
67  BEAST_EXPECT(c.insert("one"));
68  BEAST_EXPECT(c.size() == 1);
69  BEAST_EXPECT(c.insert("two"));
70  BEAST_EXPECT(c.size() == 2);
71  ++clock;
72  c.sweep();
73  BEAST_EXPECT(c.size() == 2);
74  BEAST_EXPECT(c.touch_if_exists("two"));
75  ++clock;
76  c.sweep();
77  BEAST_EXPECT(c.size() == 1);
78  }
79 
80  // Insert three items (1 over limit), sweep
81  {
82  Cache c("test", LedgerIndex(2), 3s, clock, j);
83 
84  BEAST_EXPECT(c.insert("one"));
85  ++clock;
86  BEAST_EXPECT(c.insert("two"));
87  ++clock;
88  BEAST_EXPECT(c.insert("three"));
89  ++clock;
90  BEAST_EXPECT(c.size() == 3);
91  c.sweep();
92  BEAST_EXPECT(c.size() < 3);
93  }
94  }
95 };
96 
98 
99 } // namespace ripple
ripple::LedgerIndex
std::uint32_t LedgerIndex
A ledger index.
Definition: Protocol.h:90
std::string
STL class.
ripple::TaggedCache
Map/cache combination.
Definition: Application.h:64
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::KeyCache
TaggedCache< uint256, int, true > KeyCache
Definition: KeyCache.h:28
ripple::KeyCache_test::run
void run() override
Definition: KeyCache_test.cpp:34
ripple::test::SuiteJournal
Definition: SuiteJournal.h:88
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
beast::manual_clock< std::chrono::steady_clock >
ripple::KeyCache_test
Definition: KeyCache_test.cpp:30