From 12897d3d63e8324ef7d25a18bc5de0f5bd317b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Mon, 1 Jul 2019 17:51:38 +0200 Subject: [PATCH] fixed GH #2738: Poco::AccessExpireStrategy::onGet() must not extend expiration time after expiration --- Foundation/include/Poco/AccessExpireStrategy.h | 15 +++++++++------ Foundation/testsuite/src/ExpireCacheTest.cpp | 18 +++++++++++++++++- Foundation/testsuite/src/ExpireCacheTest.h | 2 +- .../testsuite/src/ExpireLRUCacheTest.cpp | 15 +++++++++++++++ Foundation/testsuite/src/ExpireLRUCacheTest.h | 3 ++- 5 files changed, 44 insertions(+), 9 deletions(-) diff --git a/Foundation/include/Poco/AccessExpireStrategy.h b/Foundation/include/Poco/AccessExpireStrategy.h index 764c89f56..3bfa45c21 100644 --- a/Foundation/include/Poco/AccessExpireStrategy.h +++ b/Foundation/include/Poco/AccessExpireStrategy.h @@ -31,7 +31,7 @@ namespace Poco { -template < +template < class TKey, class TValue > @@ -55,11 +55,14 @@ public: typename ExpireStrategy::Iterator it = this->_keys.find(key); if (it != this->_keys.end()) { - this->_keyIndex.erase(it->second); - Timestamp now; - typename ExpireStrategy::IndexIterator itIdx = - this->_keyIndex.insert(typename ExpireStrategy::TimeIndex::value_type(now, key)); - it->second = itIdx; + if (!it->second->first.isElapsed(this->_expireTime)) // don't extend if already expired + { + this->_keyIndex.erase(it->second); + Timestamp now; + typename ExpireStrategy::IndexIterator itIdx = + this->_keyIndex.insert(typename ExpireStrategy::TimeIndex::value_type(now, key)); + it->second = itIdx; + } } } }; diff --git a/Foundation/testsuite/src/ExpireCacheTest.cpp b/Foundation/testsuite/src/ExpireCacheTest.cpp index ea3653d2a..4bf21b3c0 100644 --- a/Foundation/testsuite/src/ExpireCacheTest.cpp +++ b/Foundation/testsuite/src/ExpireCacheTest.cpp @@ -183,7 +183,22 @@ void ExpireCacheTest::testExpireWithHas() aCache.add(1, 2); // 1 assertTrue (aCache.has(1)); Thread::sleep(DURWAIT); - assertTrue (!aCache.has(1)); + assert (!aCache.has(1)); +} + + +void ExpireCacheTest::testAccessExpireGet() +{ + AccessExpireCache aCache(DURSLEEP); + aCache.add(1, 2); // 1 + assertTrue (aCache.has(1)); + SharedPtr tmp = aCache.get(1); + assertTrue (!tmp.isNull()); + assertTrue (*tmp == 2); + assertTrue (aCache.size() == 1); + Thread::sleep(DURWAIT); + tmp = aCache.get(1); + assertTrue (tmp.isNull()); } @@ -207,6 +222,7 @@ CppUnit::Test* ExpireCacheTest::suite() CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd); CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN); CppUnit_addTest(pSuite, ExpireCacheTest, testExpireWithHas); + CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireGet); return pSuite; } diff --git a/Foundation/testsuite/src/ExpireCacheTest.h b/Foundation/testsuite/src/ExpireCacheTest.h index 8ce6ba0b0..1147c3a8d 100644 --- a/Foundation/testsuite/src/ExpireCacheTest.h +++ b/Foundation/testsuite/src/ExpireCacheTest.h @@ -29,8 +29,8 @@ public: void testExpireN(); void testAccessExpireN(); void testExpireWithHas(); + void testAccessExpireGet(); - void setUp(); void tearDown(); static CppUnit::Test* suite(); diff --git a/Foundation/testsuite/src/ExpireLRUCacheTest.cpp b/Foundation/testsuite/src/ExpireLRUCacheTest.cpp index 523e9faf6..91e3a01f2 100644 --- a/Foundation/testsuite/src/ExpireLRUCacheTest.cpp +++ b/Foundation/testsuite/src/ExpireLRUCacheTest.cpp @@ -303,6 +303,20 @@ void ExpireLRUCacheTest::testDuplicateAdd() } +void ExpireLRUCacheTest::testAccessExpireGet() +{ + ExpireLRUCache aCache(3, DURSLEEP); + aCache.add(1, 2); // 1 + assertTrue (aCache.has(1)); + SharedPtr tmp = aCache.get(1); + assertTrue (!tmp.isNull()); + assertTrue (*tmp == 2); + Thread::sleep(DURWAIT); + tmp = aCache.get(1); + assertTrue (tmp.isNull()); +} + + void ExpireLRUCacheTest::setUp() { } @@ -326,6 +340,7 @@ CppUnit::Test* ExpireLRUCacheTest::suite() CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSize2); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testCacheSizeN); CppUnit_addTest(pSuite, ExpireLRUCacheTest, testDuplicateAdd); + CppUnit_addTest(pSuite, ExpireLRUCacheTest, testAccessExpireGet); return pSuite; } diff --git a/Foundation/testsuite/src/ExpireLRUCacheTest.h b/Foundation/testsuite/src/ExpireLRUCacheTest.h index 7832de945..40a4eea45 100644 --- a/Foundation/testsuite/src/ExpireLRUCacheTest.h +++ b/Foundation/testsuite/src/ExpireLRUCacheTest.h @@ -32,7 +32,8 @@ public: void testCacheSize2(); void testCacheSizeN(); void testDuplicateAdd(); - + void testAccessExpireGet(); + void setUp(); void tearDown(); static CppUnit::Test* suite();