Skip to content

Commit

Permalink
Clean-up AutoFilterTest.VerifyDescendentAwareness.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Hare committed Aug 16, 2014
1 parent 098c876 commit c884f24
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/autowiring/test/AutoFilterTest.cpp
Expand Up @@ -22,13 +22,13 @@ class AutoFilterTest:
TEST_F(AutoFilterTest, VerifyDescendentAwareness) {
// Create a packet while the factory has no subscribers:
AutoRequired<AutoPacketFactory> parentFactory;
auto packet1 = parentFactory->NewPacket();
std::shared_ptr<AutoPacket> firstPacket = parentFactory->NewPacket();

// Verify subscription-free status:
EXPECT_FALSE(packet1->HasSubscribers<Decoration<0>>()) << "Subscription exists where one should not have existed";
EXPECT_FALSE(firstPacket->HasSubscribers<Decoration<0>>()) << "Subscription exists where one should not have existed";

std::shared_ptr<AutoPacket> packet2;
std::weak_ptr<AutoPacket> packet3;
std::shared_ptr<AutoPacket> strongPacket;
std::weak_ptr<AutoPacket> weakPacket;
std::weak_ptr<FilterA> filterChecker;

// Create a subcontext
Expand All @@ -41,36 +41,39 @@ TEST_F(AutoFilterTest, VerifyDescendentAwareness) {
AutoRequired<FilterA> subFilter;
filterChecker = subFilter;
}
EXPECT_FALSE(firstPacket->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";

//Create a packet where a subscriber exists only in a subcontext
packet2 = parentFactory->NewPacket();
auto strongPacket3 = parentFactory->NewPacket();
packet3 = strongPacket3;
EXPECT_TRUE(packet2->HasSubscribers<Decoration<0>>()) << "Packet lacked expected subscription from subcontext";
EXPECT_TRUE(packet3.lock()->HasSubscribers<Decoration<0>>()) << "Packet lacked expected subscription from subcontext";
strongPacket = parentFactory->NewPacket();
std::shared_ptr<AutoPacket> holdPacket = parentFactory->NewPacket();
weakPacket = holdPacket;
EXPECT_TRUE(strongPacket->HasSubscribers<Decoration<0>>()) << "Packet lacked expected subscription from subcontext";
EXPECT_TRUE(weakPacket.lock()->HasSubscribers<Decoration<0>>()) << "Packet lacked expected subscription from subcontext";
}
EXPECT_TRUE(packet3.expired()) << "Packet was not destroyed when it's subscribers were removed";
EXPECT_TRUE(weakPacket.expired()) << "Packet was not destroyed when it's subscribers were removed";
EXPECT_FALSE(filterChecker.expired()) << "Packet keeping subcontext member alive";

// Create a packet after the subcontext has been destroyed
auto packet4 = parentFactory->NewPacket();
EXPECT_FALSE(packet4->HasSubscribers<Decoration<0>>()) << "Subscription exists where one should not have existed";

// Verify the first packet still does not have subscriptions:
EXPECT_FALSE(packet1->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";

packet2->Decorate(Decoration<0>());

// Verify the second packet will no longer have subscriptions -
// normally removing a subscriber would mean the packet still has the subscriber, but
// in this case, the subscriber was actually destroyed so the packet has lost a subscriber.
EXPECT_TRUE(packet2->HasSubscribers<Decoration<0>>()) << "Packet lacked an expected subscription";
EXPECT_TRUE(strongPacket->HasSubscribers<Decoration<0>>()) << "Missing subscriber from destroyed subcontext";

// Verify the third one does not:
EXPECT_FALSE(packet4->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";
// Call the subscriber... this will either succeed or segfault.
strongPacket->Decorate(Decoration<0>());
strongPacket->Decorate(Decoration<1>());
EXPECT_TRUE(strongPacket->HasSubscribers<Decoration<0>>()) << "Calling a subscriber should not remove it";
{
std::shared_ptr<FilterA> holdFilter = filterChecker.lock();
ASSERT_EQ(1, holdFilter->m_called) << "Subcontext filter was not called";
}

// Create a packet after the subcontext has been destroyed
auto lastPacket = parentFactory->NewPacket();
EXPECT_FALSE(lastPacket->HasSubscribers<Decoration<0>>()) << "Subscription was incorrectly, retroactively added to a packet";

packet2.reset();
EXPECT_TRUE(filterChecker.expired()) << "Subscriber didn't expire after packet was reset.";
// Verify that strongPacket was responsible for keeping subFilter alive
strongPacket.reset();
EXPECT_TRUE(filterChecker.expired()) << "Subscriber from destroyed subcontext didn't expire after packet was reset.";
}

TEST_F(AutoFilterTest, VerifySimpleFilter) {
Expand Down

0 comments on commit c884f24

Please sign in to comment.