From 23aebd2d969f0b5113de4b3759cf7df11e00f52d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnter=20Obiltschnig?= Date: Mon, 1 Jul 2019 17:59:29 +0200 Subject: [PATCH] fixed GH #2738: Poco::AccessExpireStrategy::onGet() must not extend expiration time after expiration --- Foundation/include/Poco/AccessExpireStrategy.h | 13 ++++++++----- Foundation/testsuite/src/ExpireCacheTest.cpp | 16 ++++++++++++++++ Foundation/testsuite/src/ExpireCacheTest.h | 2 +- Foundation/testsuite/src/ExpireLRUCacheTest.cpp | 15 +++++++++++++++ Foundation/testsuite/src/ExpireLRUCacheTest.h | 3 ++- 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/Foundation/include/Poco/AccessExpireStrategy.h b/Foundation/include/Poco/AccessExpireStrategy.h index bdae932f9..3bfa45c21 100644 --- a/Foundation/include/Poco/AccessExpireStrategy.h +++ b/Foundation/include/Poco/AccessExpireStrategy.h @@ -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 210d84be7..6dedfd35c 100644 --- a/Foundation/testsuite/src/ExpireCacheTest.cpp +++ b/Foundation/testsuite/src/ExpireCacheTest.cpp @@ -187,6 +187,21 @@ void ExpireCacheTest::testExpireWithHas() } +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()); +} + + void ExpireCacheTest::setUp() { } @@ -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 911b256bb..e8286a95f 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 cca954ec5..4693ecba5 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 2e8073a4f..c3c822e55 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();