TEST_F(WebsocketTest, CleanShutdown) { AutoRequired<WebsocketExceptionFilter>(); // Try starting and stopping server multiple times { AutoCreateContext ctxt; CurrentContextPusher pshr(ctxt); AutoRequired<AutoNetServer>(); ctxt->Initiate(); ctxt->Wait(std::chrono::milliseconds(200)); ctxt->SignalShutdown(true); } { AutoCreateContext ctxt; CurrentContextPusher pshr(ctxt); AutoRequired<AutoNetServer>(); ctxt->Initiate(); ctxt->Wait(std::chrono::milliseconds(200)); ctxt->SignalShutdown(true); } { AutoCreateContext ctxt; CurrentContextPusher pshr(ctxt); AutoRequired<AutoNetServer>(); ctxt->Initiate(); ctxt->Wait(std::chrono::milliseconds(200)); ctxt->SignalShutdown(true); } }
TEST_F(CoreContextTest, AppropriateShutdownInterleave) { // Need both an outer and an inner context AutoCurrentContext ctxtOuter; AutoCreateContext ctxtInner; // Need to inject types at both scopes AutoRequired<ExplicitlyHoldsOutstandingCount> outer(ctxtOuter); AutoRequired<ExplicitlyHoldsOutstandingCount> inner(ctxtInner); // Start both contexts up ctxtOuter->Initiate(); ctxtInner->Initiate(); // Now shut down the outer context. Hand off to an async, we want this to block. std::thread holder{ [ctxtOuter] { ctxtOuter->SignalShutdown(true); } }; auto holderClean = MakeAtExit([&holder] { holder.join(); }); // Need to ensure that both outstanding counters are reset at some point: { auto cleanup = MakeAtExit([&] { outer->Proceed(); inner->Proceed(); }); // Outer entry should have called "stop": auto future = outer->calledStop.get_future(); ASSERT_EQ( std::future_status::ready, future.wait_for(std::chrono::seconds(5)) ) << "Outer scope's OnStop method was incorrectly blocked by a child context member taking a long time to shut down"; } // Both contexts should be stopped now: ASSERT_TRUE(ctxtOuter->Wait(std::chrono::seconds(5))) << "Outer context did not tear down in a timely fashion"; ASSERT_TRUE(ctxtOuter->IsQuiescent()) << "Quiescence not achieved by outer context after shutdown"; ASSERT_TRUE(ctxtInner->Wait(std::chrono::seconds(5))) << "Inner context did not tear down in a timely fashion"; }