From fd91e1b9f19664574e08a1fc8bf23b95a68193af Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Wed, 18 Nov 2015 00:20:53 +0000 Subject: Prevent test failed due to race condition --- libadhocutil/unittests/testResourcePool.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libadhocutil/unittests/testResourcePool.cpp b/libadhocutil/unittests/testResourcePool.cpp index 8b4161e..6e62730 100644 --- a/libadhocutil/unittests/testResourcePool.cpp +++ b/libadhocutil/unittests/testResourcePool.cpp @@ -213,19 +213,24 @@ BOOST_AUTO_TEST_CASE( threading1 ) static void -acquireAndKeepFor1Second(TRPSmall * pool) +acquireAndKeepFor1Second(TRPSmall * pool, AdHoc::Semaphore & s) { auto r = pool->get(); + s.notify(); sleep(1); } BOOST_AUTO_TEST_CASE( threading2 ) { TRPSmall pool; - std::thread t1([&pool]() { acquireAndKeepFor1Second(&pool); }); - std::thread t2([&pool]() { acquireAndKeepFor1Second(&pool); }); - std::thread t3([&pool]() { acquireAndKeepFor1Second(&pool); }); - + AdHoc::Semaphore s; + std::thread t1([&pool, &s]() { acquireAndKeepFor1Second(&pool, s); }); + std::thread t2([&pool, &s]() { acquireAndKeepFor1Second(&pool, s); }); + std::thread t3([&pool, &s]() { acquireAndKeepFor1Second(&pool, s); }); + + s.wait(); + s.wait(); + s.wait(); BOOST_REQUIRE_THROW(pool.get(100), AdHoc::TimeOutOnResourcePoolT); BOOST_REQUIRE_EQUAL(3, pool.inUseCount()); -- cgit v1.2.3