From e2b7f25abf851cca8ce44c82eff152d724524be7 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Mon, 28 Dec 2015 16:43:21 +0000 Subject: Handle resource creation failure --- libadhocutil/resourcePool.impl.h | 16 +++++++++++++-- libadhocutil/unittests/testResourcePool.cpp | 32 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/libadhocutil/resourcePool.impl.h b/libadhocutil/resourcePool.impl.h index b5d7e2f..dfdd72d 100644 --- a/libadhocutil/resourcePool.impl.h +++ b/libadhocutil/resourcePool.impl.h @@ -193,7 +193,13 @@ namespace AdHoc { ResourcePool::get() { poolSize.wait(); - return getOne(); + try { + return getOne(); + } + catch(...) { + poolSize.notify(); + throw; + } } template @@ -203,7 +209,13 @@ namespace AdHoc { if (!poolSize.wait(timeout)) { throw TimeOutOnResourcePoolT(); } - return getOne(); + try { + return getOne(); + } + catch(...) { + poolSize.notify(); + throw; + } } template diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index 3af149b..d7547d4 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -38,6 +38,16 @@ class TRPSmall : public AdHoc::ResourcePool { } }; +class TRPFail : public AdHoc::ResourcePool { + public: + TRPFail() : AdHoc::ResourcePool(3, 1) { } + protected: + MockResource * createResource() const override + { + throw std::exception(); + } +}; + BOOST_AUTO_TEST_CASE ( get ) { { @@ -311,3 +321,25 @@ BOOST_AUTO_TEST_CASE( test ) } } +BOOST_AUTO_TEST_CASE( createFail ) +{ + TRPFail pool; + BOOST_REQUIRE_EQUAL(0, MockResource::count); + BOOST_REQUIRE_EQUAL(0, pool.availableCount()); + BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); + BOOST_REQUIRE_THROW(pool.get(), std::exception); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); + BOOST_REQUIRE_THROW(pool.get(0), std::exception); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); + BOOST_REQUIRE_THROW(pool.get(100), std::exception); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); + BOOST_REQUIRE_EQUAL(0, MockResource::count); + BOOST_REQUIRE_EQUAL(0, pool.availableCount()); + BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); + BOOST_REQUIRE_THROW(pool.get(), std::exception); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); + BOOST_REQUIRE_THROW(pool.get(), std::exception); + BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +} + -- cgit v1.2.3