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); } }