From 8a4dadd30ad8d6554a23259bb251b5c41e2e92e1 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 28 Dec 2015 19:40:01 +0000 Subject: Support testing a resource validity on return to the pool --- libadhocutil/resourcePool.h | 4 +++- libadhocutil/resourcePool.impl.h | 18 +++++++++++++++--- libadhocutil/unittests/testResourcePool.cpp | 28 ++++++++++++++++++++++++---- 3 files changed, 42 insertions(+), 8 deletions(-) diff --git a/libadhocutil/resourcePool.h b/libadhocutil/resourcePool.h index 9d8c625..26517bf 100644 --- a/libadhocutil/resourcePool.h +++ b/libadhocutil/resourcePool.h @@ -83,8 +83,10 @@ namespace AdHoc { virtual Resource * createResource() const = 0; /// Destroy an existing resource (defaults to delete). virtual void destroyResource(Resource *) const throw(); - /// Test a cached resource is still suitable for use (defaults to no-op). + /// Test a cached resource is still suitable for use before re-use (defaults to no-op). virtual void testResource(const Resource *) const; + /// Test a cached resource is still suitable for use on return (defaults to no-op). + virtual void returnTestResource(const Resource *) const; private: typedef std::list Available; diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index dfdd72d..f49a548 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -145,6 +145,12 @@ namespace AdHoc { { } + template + void + ResourcePool::returnTestResource(const R *) const + { + } + template unsigned int ResourcePool::inUseCount() const @@ -248,10 +254,16 @@ namespace AdHoc { { Lock(lock); removeFrom(r, inUse); - if (available.size() < keep) { - available.push_back(r); + try { + returnTestResource(r); + if (available.size() < keep) { + available.push_back(r); + } + else { + destroyResource(r); + } } - else { + catch (...) { destroyResource(r); } poolSize.notify(); diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index d7547d4..2b3389f 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -38,9 +38,7 @@ class TRPSmall : public AdHoc::ResourcePool { } }; -class TRPFail : public AdHoc::ResourcePool { - public: - TRPFail() : AdHoc::ResourcePool(3, 1) { } +class TRPCreateFail : public TRPSmall { protected: MockResource * createResource() const override { @@ -48,6 +46,14 @@ class TRPFail : public AdHoc::ResourcePool { } }; +class TRPReturnFail : public TRPSmall { + protected: + void returnTestResource(const MockResource *) const override + { + throw std::exception(); + } +}; + BOOST_AUTO_TEST_CASE ( get ) { { @@ -323,7 +329,7 @@ BOOST_AUTO_TEST_CASE( test ) BOOST_AUTO_TEST_CASE( createFail ) { - TRPFail pool; + TRPCreateFail pool; BOOST_REQUIRE_EQUAL(0, MockResource::count); BOOST_REQUIRE_EQUAL(0, pool.availableCount()); BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); @@ -343,3 +349,17 @@ BOOST_AUTO_TEST_CASE( createFail ) BOOST_REQUIRE_EQUAL(3, pool.freeCount()); } +BOOST_AUTO_TEST_CASE( returnFail ) +{ + TRPReturnFail pool; + { + auto rh = pool.get(); + BOOST_REQUIRE_EQUAL(0, pool.availableCount()); + BOOST_REQUIRE_EQUAL(1, pool.inUseCount()); + BOOST_REQUIRE_EQUAL(2, pool.freeCount()); + } + BOOST_REQUIRE_EQUAL(0, pool.availableCount()); + BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +} + -- cgit v1.2.3