TEST_F(ContextMapTest, VerifyWithThreadsPathological) { ContextMap<size_t> mp; // Context collection and exit race threads: vector<std::shared_ptr<CoreContext>> contexts; // Exit race controller: AutoRequired<ExitRaceSignal> signal; // Create a number of dependent contexts: for(size_t i = 0; i < 100; i++) { AutoCreateContext context; contexts.push_back(context); // Store a shared pointer mp.Add(i, context); // Start the context context->Initiate(); } // Set the signal: signal->Signal(); // Verify that the map empties once our zero-count is hit: for(size_t i = 0; i < contexts.size(); i++) { contexts[i]->SignalShutdown(true); } // Clear the context collection: contexts.clear(); EXPECT_EQ(0UL, mp.size()) << "Context map did not empty as expected"; }
TEST_F(ContextCreatorTest, ValidateMultipleEviction) { AutoCurrentContext()->Initiate(); // Number of dependent contexts to be created const size_t count = 100; // Teardown lock, counter, and condition: std::mutex lock; std::condition_variable cond; int counter = count; // Obtain creator pointer: AutoRequired<Creator> creator; // Set up a signal manager at global context scope: AutoRequired<GlobalSignal> signal; { // Array of objects to test destruction on, and corresponding collection of contexts: std::shared_ptr<WaitMember> members[count]; // Create a few contexts: for(int i = count; i--;) { AutoCreateContext ctxt; CurrentContextPusher pshr(ctxt); // Trivial validation that the newly created context is an empty context: ASSERT_EQ(static_cast<size_t>(0), ctxt->GetMemberCount()) << "A created context was not empty"; // Add in an object to test asynchronous destruction: AutoRequired<WaitMember> obj; members[i] = obj; ASSERT_EQ(signal.get(), obj->m_signal.get()) << "Dependent context wiring did not correctly match to the enclosing scope"; // Add a notifier to signal a continue condition when we have everything we need: ctxt->AddTeardownListener([&lock, &cond, &counter] { (std::lock_guard<std::mutex>)lock, counter--, cond.notify_all(); }); // Kick off the context: ctxt->Initiate(); } // Signal all members and then release everything: signal->Signal(); } // Wait for all contexts to be destroyed std::unique_lock<std::mutex> lk(lock); bool wait_status = cond.wait_for(lk, std::chrono::seconds(1), [&counter] {return counter == 0;}); ASSERT_TRUE(wait_status) << "All teardown listeners didn't trigger, counter still at " << counter; // Validate that everything expires: ASSERT_EQ(static_cast<size_t>(0), creator->GetSize()) << "Not all contexts were evicted as expected"; }