diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-28 19:40:01 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2015-12-28 19:40:01 +0000 |
commit | 8a4dadd30ad8d6554a23259bb251b5c41e2e92e1 (patch) | |
tree | ca2fefbdfe939c4f29404bb1748cb7756ac2268c | |
parent | Handle resource creation failure (diff) | |
download | libadhocutil-0.3.2.tar.bz2 libadhocutil-0.3.2.tar.xz libadhocutil-0.3.2.zip |
Support testing a resource validity on return to the poollibadhocutil-0.3.2
-rw-r--r-- | libadhocutil/resourcePool.h | 4 | ||||
-rw-r--r-- | libadhocutil/resourcePool.impl.h | 18 | ||||
-rw-r--r-- | 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<Resource *> 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 @@ -146,6 +146,12 @@ namespace AdHoc { } template <typename R> + void + ResourcePool<R>::returnTestResource(const R *) const + { + } + + template <typename R> unsigned int ResourcePool<R>::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<MockResource> { } }; -class TRPFail : public AdHoc::ResourcePool<MockResource> { - public: - TRPFail() : AdHoc::ResourcePool<MockResource>(3, 1) { } +class TRPCreateFail : public TRPSmall { protected: MockResource * createResource() const override { @@ -48,6 +46,14 @@ class TRPFail : public AdHoc::ResourcePool<MockResource> { } }; +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()); +} + |