Skip to content

Commit

Permalink
Clean up tests leaking across test boundaries
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Marsden committed Nov 9, 2017
1 parent 7e37c02 commit e4f7346
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/autowiring/CoreContextStateBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ RunCounter::~RunCounter(void) {
outstanding = std::move(stateBlock->m_outstanding);
stateBlock->m_outstanding.reset();
}
outstanding.reset();

// Wake everyone up
stateBlock->m_stateChanged.notify_all();
Expand Down
24 changes: 20 additions & 4 deletions src/autowiring/test/CoreThreadTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ TEST_F(CoreThreadTest, VerifyStartSpam) {
instance->Start(std::shared_ptr<CoreObject>((CoreObject*) 1, [] (CoreObject*) {}));

EXPECT_FALSE(instance->m_multiHit) << "Thread was run more than once unexpectedly";

ctxt->SignalShutdown(true);
}

class InvokesIndefiniteWait:
Expand Down Expand Up @@ -114,6 +116,7 @@ TEST_F(CoreThreadTest, VerifyNestedTermination) {

// Verify that the child thread has stopped:
ASSERT_FALSE(st->IsRunning()) << "Child thread was running even though the enclosing context was terminated";
ctxt->Wait();
}

class ListenThread :
Expand Down Expand Up @@ -202,6 +205,7 @@ TEST_F(CoreThreadTest, VerifyDelayedDispatchQueueSimple) {
std::this_thread::yield();
ASSERT_TRUE(*y) << "A simple ready call was not dispatched within 100ms of being pended";
ASSERT_FALSE(*x) << "An event which should not have been executed for an hour was executed early";
ctxt->SignalShutdown(true);
}

TEST_F(CoreThreadTest, VerifyNoDelayDoubleFree) {
Expand All @@ -221,6 +225,7 @@ TEST_F(CoreThreadTest, VerifyNoDelayDoubleFree) {
// Verify that the shared pointer isn't unique at this point. If it is, it's because our CoreThread deleted
// the event even though it was supposed to have pended it.
ASSERT_FALSE(x.unique()) << "A pended event was freed before it was called, and appears to be present in a dispatch queue";
ctxt->SignalShutdown(true);
}

TEST_F(CoreThreadTest, VerifyDoublePendedDispatchDelay) {
Expand Down Expand Up @@ -255,6 +260,7 @@ TEST_F(CoreThreadTest, VerifyDoublePendedDispatchDelay) {
return;

FAIL() << "An out-of-order delayed dispatch was not executed in time as expected";
ctxt->SignalShutdown(true);
}

TEST_F(CoreThreadTest, VerifyTimedSort) {
Expand All @@ -280,6 +286,7 @@ TEST_F(CoreThreadTest, VerifyTimedSort) {

// Verify that the resulting vector is sorted.
ASSERT_TRUE(std::is_sorted(v.begin(), v.end())) << "A timed sort implementation did not generate a sorted sequence as expected";
ctxt->SignalShutdown(true);
}

TEST_F(CoreThreadTest, VerifyPendByTimePoint) {
Expand Down Expand Up @@ -586,15 +593,23 @@ TEST_F(CoreThreadTest, SubContextHoldsParentContext) {

auto cv = std::make_shared<std::condition_variable>();
auto lock = std::make_shared<std::mutex>();
auto invoked = std::make_shared<bool>(false);
auto proceed = std::make_shared<bool>(false);
*thread += [cv, lock, proceed] {
*thread += [thread, cv, lock, proceed, invoked] {
*invoked = true;
std::unique_lock<std::mutex> lk(*lock);
cv->notify_all();
cv->wait_for(lk, std::chrono::seconds(5), [&] { return *proceed; });
};

parent->Initiate();
child->Initiate();

{ // Wait for lambda to start running before attempting to shutdown
std::unique_lock<std::mutex> lk{ *lock };
ASSERT_TRUE(cv->wait_for(lk, std::chrono::seconds(5), [&] { return *invoked; })) << "Failed to run lambda";
}

// Terminate the parent, verify that the child has exited:
parent->SignalShutdown();
ASSERT_FALSE(parent->Wait(std::chrono::milliseconds(5))) << "Parent context returned before all child threads were done";
Expand All @@ -604,6 +619,7 @@ TEST_F(CoreThreadTest, SubContextHoldsParentContext) {
ASSERT_TRUE(parent->Wait(std::chrono::seconds(5))) << "Signalled thread did not terminate in a timely fashion";
}
ASSERT_EQ(initUses, parent.use_count()) << "Parent had spurious references after all objects should have been destroyed";
parent->Wait();
}

class DoesNothingButQuit:
Expand Down Expand Up @@ -642,12 +658,12 @@ TEST_F(CoreThreadTest, LambdaHoldAfterTermination) {
*mct += [] {};
mct->Barrier();
ASSERT_GE(1U, mct->GetDispatchQueueLength()) << "Dispatch queue had lingering entries after barrier";
child->SignalShutdown();
childWeak = child;
child->Wait();
child->SignalShutdown(true);
}
ASSERT_TRUE(*teardownCalled) << "Teardown listener was not called as expected";
ASSERT_TRUE(childWeak.expired()) << "Child context leaked due to lambda pending in teardown";
ASSERT_TRUE(!childWeak.lock()) << "Child context leaked due to lambda pending in teardown";
ctxt->SignalShutdown(true);
}

TEST_F(CoreThreadTest, CanElevateAnyPriority) {
Expand Down

0 comments on commit e4f7346

Please sign in to comment.