diff options
Diffstat (limited to 'libadhocutil/unittests/testResourcePool.cpp')
-rw-r--r-- | libadhocutil/unittests/testResourcePool.cpp | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index baa965a..a5f06a2 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -4,7 +4,11 @@ #include "c++11Helpers.h" #include "lockHelpers.h" #include "resourcePool.impl.h" -#include "semaphore.h" +#ifdef __cpp_lib_semaphore +# include <semaphore> +#else +# include "polyfill-semaphore.h" +#endif #include <atomic> #include <list> #include <map> @@ -330,21 +334,21 @@ BOOST_AUTO_TEST_CASE(threading1, *boost::unit_test::timeout(10)) } static void -acquireAndKeepFor1Second(TRPSmall * pool, AdHoc::Semaphore & s) +acquireAndKeepFor1Second(TRPSmall * pool, AdHoc::SemaphoreType & s) { auto r = pool->get(); static std::mutex m; ScopeLock(m) { BOOST_CHECK(r); } - s.notify(); + s.release(); sleep(1); } BOOST_AUTO_TEST_CASE(threading2) { TRPSmall pool; - AdHoc::Semaphore s; + AdHoc::SemaphoreType s {0}; std::thread t1([&pool, &s]() { acquireAndKeepFor1Second(&pool, s); }); @@ -355,9 +359,9 @@ BOOST_AUTO_TEST_CASE(threading2) acquireAndKeepFor1Second(&pool, s); }); - s.wait(); - s.wait(); - s.wait(); + s.acquire(); + s.acquire(); + s.acquire(); BOOST_REQUIRE_THROW(pool.get(100), AdHoc::TimeOutOnResourcePoolT<MockResource>); BOOST_REQUIRE_EQUAL(3, pool.inUseCount()); @@ -416,20 +420,32 @@ BOOST_AUTO_TEST_CASE(createFail) BOOST_REQUIRE_EQUAL(0, MockResource::count); BOOST_REQUIRE_EQUAL(0, pool.availableCount()); BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif BOOST_REQUIRE_THROW(pool.get(), std::exception); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif BOOST_REQUIRE_THROW(pool.get(0), std::exception); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif BOOST_REQUIRE_THROW(pool.get(100), std::exception); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif 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); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif BOOST_REQUIRE_THROW(pool.get(), std::exception); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif } BOOST_AUTO_TEST_CASE(returnFail) @@ -440,11 +456,15 @@ BOOST_AUTO_TEST_CASE(returnFail) BOOST_CHECK(rh); BOOST_REQUIRE_EQUAL(0, pool.availableCount()); BOOST_REQUIRE_EQUAL(1, pool.inUseCount()); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(2, pool.freeCount()); +#endif } BOOST_REQUIRE_EQUAL(0, pool.availableCount()); BOOST_REQUIRE_EQUAL(0, pool.inUseCount()); +#ifndef __cpp_lib_semaphore BOOST_REQUIRE_EQUAL(3, pool.freeCount()); +#endif } BOOST_AUTO_TEST_CASE(exception_msgs) |