summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2015-12-28 16:43:21 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2015-12-28 16:43:21 +0000
commite2b7f25abf851cca8ce44c82eff152d724524be7 (patch)
tree93fefd9f3547a47b0755e3d6078cdf34d15030e9
parentAdd support for querying resource pool free count (diff)
downloadlibadhocutil-e2b7f25abf851cca8ce44c82eff152d724524be7.tar.bz2
libadhocutil-e2b7f25abf851cca8ce44c82eff152d724524be7.tar.xz
libadhocutil-e2b7f25abf851cca8ce44c82eff152d724524be7.zip
Handle resource creation failure
-rw-r--r--libadhocutil/resourcePool.impl.h16
-rw-r--r--libadhocutil/unittests/testResourcePool.cpp32
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<R>::get()
{
poolSize.wait();
- return getOne();
+ try {
+ return getOne();
+ }
+ catch(...) {
+ poolSize.notify();
+ throw;
+ }
}
template <typename R>
@@ -203,7 +209,13 @@ namespace AdHoc {
if (!poolSize.wait(timeout)) {
throw TimeOutOnResourcePoolT<R>();
}
- return getOne();
+ try {
+ return getOne();
+ }
+ catch(...) {
+ poolSize.notify();
+ throw;
+ }
}
template <typename R>
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<MockResource> {
}
};
+class TRPFail : public AdHoc::ResourcePool<MockResource> {
+ public:
+ TRPFail() : AdHoc::ResourcePool<MockResource>(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());
+}
+