Skip to content

Commit

Permalink
Strengthen test guarantee in DispatchQueueTest.BarrierWithAbort
Browse files Browse the repository at this point in the history
  • Loading branch information
codemercenary committed May 19, 2015
1 parent 34be59a commit cabbd11
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions src/autowiring/test/DispatchQueueTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ struct BarrierMonitor {
// Standard continuation behavior:
std::mutex lock;
std::condition_variable cv;
bool done;
size_t nReady = 0;
bool done = false;
};

TEST_F(DispatchQueueTest, BarrierWithAbort) {
Expand All @@ -113,28 +114,34 @@ TEST_F(DispatchQueueTest, BarrierWithAbort) {

// This dispatch entry will delay until we're ready for it to continue:
*ct += [b] {
std::lock_guard<std::mutex> lk(b->lock);
std::unique_lock<std::mutex> lk(b->lock);
b->nReady++;
b->cv.notify_all();
b->cv.wait(lk, [&] { return b->done; });
};

// Delay for long enough for the barrier to be reached:
std::this_thread::sleep_for(std::chrono::milliseconds(1));

// Launch something that will barrier:
auto f = std::async(
std::launch::async,
[=] {
try {
ct->Barrier(std::chrono::seconds(5));
}
catch (autowiring_error&) {
return false;
{
std::unique_lock<std::mutex> lk(b->lock);
b->nReady++;
b->cv.notify_all();
}
return true;
return ct->Barrier(std::chrono::seconds(5));
}
);

// Now abandon the queue, this should cause the async thread to quit:
// Wait for all threads to hit the barrier, then signal anyone interested that they can back out
b->cv.wait(lk, [&] { return b->nReady == 2; });
b->done = true;
b->cv.notify_all();

// Abandon the queue before relinquishing the lock:
ct->Abort();

ASSERT_EQ(std::future_status::ready, f.wait_for(std::chrono::seconds(5))) << "Barrier did not abort fast enough";
ASSERT_FALSE(f.get()) << "Exception should have been thrown inside the Barrier call";
bool rs;
ASSERT_ANY_THROW(rs = f.get()) << "Barrier call returned " << std::boolalpha << rs << " instead of throwing an exception";
}

0 comments on commit cabbd11

Please sign in to comment.