diff --git a/Foundation/testsuite/src/ExpireCacheTest.cpp b/Foundation/testsuite/src/ExpireCacheTest.cpp index 7b11b1678..f25f9ae01 100644 --- a/Foundation/testsuite/src/ExpireCacheTest.cpp +++ b/Foundation/testsuite/src/ExpireCacheTest.cpp @@ -1,7 +1,7 @@ // // ExpireCacheTest.cpp // -// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.cpp#9 $ +// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.cpp#10 $ // // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. // and Contributors. @@ -35,6 +35,7 @@ #include "CppUnit/TestSuite.h" #include "Poco/Exception.h" #include "Poco/ExpireCache.h" +#include "Poco/AccessExpireCache.h" #include "Poco/Bugcheck.h" #include "Poco/Thread.h" @@ -155,6 +156,46 @@ void ExpireCacheTest::testDuplicateAdd() } + +void ExpireCacheTest::testAccessExpireN() +{ + // 3-1 represents the cache sorted by age, elements get replaced at the end of the list + // 3-1|5 -> 5 gets removed + AccessExpireCache aCache(DURSLEEP); + aCache.add(1, 2); // 1 + assert (aCache.has(1)); + SharedPtr tmp = aCache.get(1); + assert (!tmp.isNull()); + assert (*tmp == 2); + assert (aCache.size() == 1); + Thread::sleep(DURWAIT); + assert (aCache.size() == 0); + assert (!aCache.has(1)); + + // tmp must still be valid, access it + assert (*tmp == 2); + tmp = aCache.get(1); + assert (!tmp); + + aCache.add(1, 2); // 1 + Thread::sleep(DURHALFSLEEP); + aCache.add(3, 4); // 3-1 + assert (aCache.has(1)); + assert (aCache.has(3)); + + Thread::sleep(DURHALFSLEEP+50); //3|1 + assert (!aCache.has(1)); + assert (*aCache.get(3) == 4); + Thread::sleep(DURHALFSLEEP+25); //3|1 + assert (*aCache.get(3) == 4); + // removing illegal entries should work too + aCache.remove(666); + + aCache.clear(); + assert (!aCache.has(5)); + assert (!aCache.has(3)); +} + void ExpireCacheTest::setUp() { } @@ -173,6 +214,7 @@ CppUnit::Test* ExpireCacheTest::suite() CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0); CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN); CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd); + CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN); return pSuite; } diff --git a/Foundation/testsuite/src/ExpireCacheTest.h b/Foundation/testsuite/src/ExpireCacheTest.h index d2ba5055a..298f21f36 100644 --- a/Foundation/testsuite/src/ExpireCacheTest.h +++ b/Foundation/testsuite/src/ExpireCacheTest.h @@ -1,7 +1,7 @@ // // ExpireCacheTest.h // -// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.h#5 $ +// $Id: //poco/Main/Foundation/testsuite/src/ExpireCacheTest.h#6 $ // // Tests for ExpireCache // @@ -49,6 +49,7 @@ public: void testDuplicateAdd(); void testExpire0(); void testExpireN(); + void testAccessExpireN(); void setUp(); diff --git a/Foundation/testsuite/src/ExpireLRUCacheTest.cpp b/Foundation/testsuite/src/ExpireLRUCacheTest.cpp index 18fbf80c9..229a898c0 100644 --- a/Foundation/testsuite/src/ExpireLRUCacheTest.cpp +++ b/Foundation/testsuite/src/ExpireLRUCacheTest.cpp @@ -1,7 +1,7 @@ // // ExpireLRUCacheTest.cpp // -// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.cpp#8 $ +// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.cpp#9 $ // // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. // and Contributors. @@ -35,6 +35,7 @@ #include "CppUnit/TestSuite.h" #include "Poco/Exception.h" #include "Poco/ExpireLRUCache.h" +#include "Poco/AccessExpireLRUCache.h" #include "Poco/Bugcheck.h" #include "Poco/Thread.h" @@ -141,6 +142,46 @@ void ExpireLRUCacheTest::testExpireN() } +void ExpireLRUCacheTest::testAccessExpireN() +{ + // 3-1 represents the cache sorted by age, elements get replaced at the end of the list + // 3-1|5 -> 5 gets removed + AccessExpireLRUCache aCache(3, DURSLEEP); + aCache.add(1, 2); // 1 + assert (aCache.has(1)); + SharedPtr tmp = aCache.get(1); + assert (!tmp.isNull()); + assert (*tmp == 2); + assert (aCache.size() == 1); + Thread::sleep(DURWAIT); + assert (aCache.size() == 0); + assert (!aCache.has(1)); + + // tmp must still be valid, access it + assert (*tmp == 2); + tmp = aCache.get(1); + assert (!tmp); + + aCache.add(1, 2); // 1 + Thread::sleep(DURHALFSLEEP); + aCache.add(3, 4); // 3-1 + assert (aCache.has(1)); + assert (aCache.has(3)); + + Thread::sleep(DURHALFSLEEP+50); //3|1 + assert (!aCache.has(1)); + assert (*aCache.get(3) == 4); + Thread::sleep(DURHALFSLEEP+25); //3|1 + assert (*aCache.get(3) == 4); + // removing illegal entries should work too + aCache.remove(666); + + aCache.clear(); + assert (!aCache.has(5)); + assert (!aCache.has(3)); +} + + void ExpireLRUCacheTest::testCacheSize0() { // cache size 0 is illegal @@ -301,6 +342,7 @@ CppUnit::Test* ExpireLRUCacheTest::suite() CppUnit_addTest(pSuite, ExpireLRUCacheTest, testClear); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testExpire0); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testExpireN); + CppUnit_addTest(pSuite, ExpireLRUCacheTest, testAccessExpireN); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize0); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize1); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize2); diff --git a/Foundation/testsuite/src/ExpireLRUCacheTest.h b/Foundation/testsuite/src/ExpireLRUCacheTest.h index ae0ce2229..14c31d893 100644 --- a/Foundation/testsuite/src/ExpireLRUCacheTest.h +++ b/Foundation/testsuite/src/ExpireLRUCacheTest.h @@ -1,7 +1,7 @@ // // ExpireLRUCacheTest.h // -// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.h#5 $ +// $Id: //poco/Main/Foundation/testsuite/src/ExpireLRUCacheTest.h#6 $ // // Tests for ExpireLRUCache // @@ -48,6 +48,7 @@ public: void testClear(); void testExpire0(); void testExpireN(); + void testAccessExpireN(); void testCacheSize0(); void testCacheSize1(); void testCacheSize2();