summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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());
+}
+