void run () { beast::Journal const j; beast::manual_clock <std::chrono::steady_clock> clock; clock.set (0); typedef int Key; typedef std::string Value; typedef TaggedCache <Key, Value> Cache; Cache c ("test", 1, 1, clock, j); // Insert an item, retrieve it, and age it so it gets purged. { expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 0); expect (! c.insert (1, "one")); expect (c.getCacheSize() == 1); expect (c.getTrackSize() == 1); { std::string s; expect (c.retrieve (1, s)); expect (s == "one"); } ++clock; c.sweep (); expect (c.getCacheSize () == 0); expect (c.getTrackSize () == 0); } // Insert an item, maintain a strong pointer, age it, and // verify that the entry still exists. { expect (! c.insert (2, "two")); expect (c.getCacheSize() == 1); expect (c.getTrackSize() == 1); { Cache::mapped_ptr p (c.fetch (2)); expect (p != nullptr); ++clock; c.sweep (); expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 1); } // Make sure its gone now that our reference is gone ++clock; c.sweep (); expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 0); } // Insert the same key/value pair and make sure we get the same result { expect (! c.insert (3, "three")); { Cache::mapped_ptr const p1 (c.fetch (3)); Cache::mapped_ptr p2 (std::make_shared <Value> ("three")); c.canonicalize (3, p2); expect (p1.get() == p2.get()); } ++clock; c.sweep (); expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 0); } // Put an object in but keep a strong pointer to it, advance the clock a lot, // then canonicalize a new object with the same key, make sure you get the // original object. { // Put an object in expect (! c.insert (4, "four")); expect (c.getCacheSize() == 1); expect (c.getTrackSize() == 1); { // Keep a strong pointer to it Cache::mapped_ptr p1 (c.fetch (4)); expect (p1 != nullptr); expect (c.getCacheSize() == 1); expect (c.getTrackSize() == 1); // Advance the clock a lot ++clock; c.sweep (); expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 1); // Canonicalize a new object with the same key Cache::mapped_ptr p2 (std::make_shared <std::string> ("four")); expect (c.canonicalize (4, p2, false)); expect (c.getCacheSize() == 1); expect (c.getTrackSize() == 1); // Make sure we get the original object expect (p1.get() == p2.get()); } ++clock; c.sweep (); expect (c.getCacheSize() == 0); expect (c.getTrackSize() == 0); } }
void run () { beast::manual_clock <std::chrono::seconds> clock; clock.set (0); typedef std::string Key; typedef KeyCache <Key> Cache; // Insert an item, retrieve it, and age it so it gets purged. { Cache c ("test", clock, 1, 2); expect (c.size () == 0); expect (c.insert ("one")); expect (! c.insert ("one")); expect (c.size () == 1); expect (c.exists ("one")); expect (c.touch_if_exists ("one")); ++clock; c.sweep (); expect (c.size () == 1); expect (c.exists ("one")); ++clock; c.sweep (); expect (c.size () == 0); expect (! c.exists ("one")); expect (! c.touch_if_exists ("one")); } // Insert two items, have one expire { Cache c ("test", clock, 2, 2); expect (c.insert ("one")); expect (c.size () == 1); expect (c.insert ("two")); expect (c.size () == 2); ++clock; c.sweep (); expect (c.size () == 2); expect (c.touch_if_exists ("two")); ++clock; c.sweep (); expect (c.size () == 1); expect (c.exists ("two")); } // Insert three items (1 over limit), sweep { Cache c ("test", clock, 2, 3); expect (c.insert ("one")); ++clock; expect (c.insert ("two")); ++clock; expect (c.insert ("three")); ++clock; expect (c.size () == 3); c.sweep (); expect (c.size () < 3); } }
void run () { TestStopwatch clock; clock.set (0); using Key = std::string; using Cache = KeyCache <Key>; // Insert an item, retrieve it, and age it so it gets purged. { Cache c ("test", clock, 1, 2); BEAST_EXPECT(c.size () == 0); BEAST_EXPECT(c.insert ("one")); BEAST_EXPECT(! c.insert ("one")); BEAST_EXPECT(c.size () == 1); BEAST_EXPECT(c.exists ("one")); BEAST_EXPECT(c.touch_if_exists ("one")); ++clock; c.sweep (); BEAST_EXPECT(c.size () == 1); BEAST_EXPECT(c.exists ("one")); ++clock; c.sweep (); BEAST_EXPECT(c.size () == 0); BEAST_EXPECT(! c.exists ("one")); BEAST_EXPECT(! c.touch_if_exists ("one")); } // Insert two items, have one expire { Cache c ("test", clock, 2, 2); BEAST_EXPECT(c.insert ("one")); BEAST_EXPECT(c.size () == 1); BEAST_EXPECT(c.insert ("two")); BEAST_EXPECT(c.size () == 2); ++clock; c.sweep (); BEAST_EXPECT(c.size () == 2); BEAST_EXPECT(c.touch_if_exists ("two")); ++clock; c.sweep (); BEAST_EXPECT(c.size () == 1); BEAST_EXPECT(c.exists ("two")); } // Insert three items (1 over limit), sweep { Cache c ("test", clock, 2, 3); BEAST_EXPECT(c.insert ("one")); ++clock; BEAST_EXPECT(c.insert ("two")); ++clock; BEAST_EXPECT(c.insert ("three")); ++clock; BEAST_EXPECT(c.size () == 3); c.sweep (); BEAST_EXPECT(c.size () < 3); } }