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/chrono.h>
21 #include <ripple/basics/KeyCache.h>
22 #include <ripple/beast/unit_test.h>
23 #include <ripple/beast/clock/manual_clock.h>
24 
25 namespace ripple {
26 
27 class KeyCache_test : public beast::unit_test::suite
28 {
29 public:
30  void run () override
31  {
32  using namespace std::chrono_literals;
33  TestStopwatch clock;
34  clock.set (0);
35 
36  using Key = std::string;
37  using Cache = KeyCache <Key>;
38 
39  // Insert an item, retrieve it, and age it so it gets purged.
40  {
41  Cache c ("test", clock, 1, 2s);
42 
43  BEAST_EXPECT(c.size () == 0);
44  BEAST_EXPECT(c.insert ("one"));
45  BEAST_EXPECT(! c.insert ("one"));
46  BEAST_EXPECT(c.size () == 1);
47  BEAST_EXPECT(c.exists ("one"));
48  BEAST_EXPECT(c.touch_if_exists ("one"));
49  ++clock;
50  c.sweep ();
51  BEAST_EXPECT(c.size () == 1);
52  BEAST_EXPECT(c.exists ("one"));
53  ++clock;
54  c.sweep ();
55  BEAST_EXPECT(c.size () == 0);
56  BEAST_EXPECT(! c.exists ("one"));
57  BEAST_EXPECT(! c.touch_if_exists ("one"));
58  }
59 
60  // Insert two items, have one expire
61  {
62  Cache c ("test", clock, 2, 2s);
63 
64  BEAST_EXPECT(c.insert ("one"));
65  BEAST_EXPECT(c.size () == 1);
66  BEAST_EXPECT(c.insert ("two"));
67  BEAST_EXPECT(c.size () == 2);
68  ++clock;
69  c.sweep ();
70  BEAST_EXPECT(c.size () == 2);
71  BEAST_EXPECT(c.touch_if_exists ("two"));
72  ++clock;
73  c.sweep ();
74  BEAST_EXPECT(c.size () == 1);
75  BEAST_EXPECT(c.exists ("two"));
76  }
77 
78  // Insert three items (1 over limit), sweep
79  {
80  Cache c ("test", clock, 2, 3s);
81 
82  BEAST_EXPECT(c.insert ("one"));
83  ++clock;
84  BEAST_EXPECT(c.insert ("two"));
85  ++clock;
86  BEAST_EXPECT(c.insert ("three"));
87  ++clock;
88  BEAST_EXPECT(c.size () == 3);
89  c.sweep ();
90  BEAST_EXPECT(c.size () < 3);
91  }
92  }
93 };
94 
95 BEAST_DEFINE_TESTSUITE(KeyCache,common,ripple);
96 
97 }
std::string
STL class.
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
ripple::KeyCache_test::run
void run() override
Definition: KeyCache_test.cpp:30
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< Key >
ripple::KeyCache_test
Definition: KeyCache_test.cpp:27