TEST(AutoResetEvent, TimedWait) { AutoResetEvent event; EXPECT_FALSE(event.TimedWait(1)); event.Set(); EXPECT_TRUE(event.TimedWait(1)); EXPECT_FALSE(event.TryWait()); }
//! Making sure all internal processing has stopped in an object using a //! callQueue: //! First, ensure that all users of the object have been shut down or detached. //! Then make sure that internal processing has been shut down in the object. //! Then ensure that all enqueued dispatch calls have been flushed //! by calling WaitForPendingCalls. void WaitForPendingCalls(bool bInvokedOnDispatchThread) { if(bInvokedOnDispatchThread) { // CallQueue is running on the same thread as the Wait call. bool bCompleted; Enqueue(bind(&Impl::SetToTrue, ref(bCompleted))); while(!bCompleted) { const bool bBlock = false; Dispatch(bBlock); } } else { // CallQueue is running on a different thread than the Wait call. AutoResetEvent completedEvent; Enqueue(bind(&AutoResetEvent::Set, &completedEvent)); completedEvent.Wait(); } }
void PerfTest::WaitForResult(FileWriter & fw) { Invariant(allReceived_.WaitOne(testTimeout_)); auto elapsedMilliseconds = stopwatch_.ElapsedMilliseconds; console.WriteLine(">>> time elapsed: {0} ms", elapsedMilliseconds); auto totalReceivedBytes = recvBytes_.load(); auto recvRate = (totalReceivedBytes * 8.0) / elapsedMilliseconds / 1000.0; console.WriteLine(">>> received: {0} bytes", totalReceivedBytes); console.WriteLine(">>> receive rate: {0} mbps\n\n", recvRate); fw.Write(",{0},{1}", elapsedMilliseconds, recvRate); listener_->Stop(); listener_.reset(); }
void PerfTest::StartListener() { wstring localFqdn; auto error = TcpTransportUtility::GetLocalFqdn(localFqdn); Invariant(error.IsSuccess()); for (USHORT port = 22000; port < 23000; ++port) { listener_ = TcpDatagramTransport::Create(wformatString("{0}:{1}", localFqdn, port)); SecuritySettings securitySettings = TTestUtil::CreateTestSecuritySettings(securityProvider_, protectionLevel); Invariant(listener_->SetSecurity(securitySettings).IsSuccess()); auto root = make_shared<TestRoot>(); // make it real! listener_->SetMessageHandler([this, root](MessageUPtr & receivedMsg, ISendTarget::SPtr const & st) { auto after = ++recvCount_; recvBytes_ += receivedMsg->SerializedSize(); if (after == 1) { console.WriteLine("send test parameters to {0}:{1}", st->Address(), testParameters_); auto msg = make_unique<Message>(testParameters_); listener_->SendOneWay(st, move(msg)); return; } if (after == 2) { stopwatch_.Start(); if (after < (testParameters_.MessageCount() + 1)) { return; } } if (after == (testParameters_.MessageCount() + 1)) { stopwatch_.Stop(); allReceived_.Set(); // stop sending side auto msg = make_unique<Message>(); listener_->SendOneWay(st, move(msg)); return; } if (shouldQueueReceivedMessage_) { AcquireWriteLock grab(receiveQueueLock); receiveQueue.push(receivedMsg->Clone()); if (receiveQueue.size() > 8) { receiveQueue.pop(); } } }); error = listener_->Start(); if (!error.IsSuccess()) { Invariant(error.IsError(ErrorCodeValue::AddressAlreadyInUse)); console.WriteLine("!!! address conflict, will retry !!!"); continue; } console.WriteLine("listen address: {0}", listener_->ListenAddress()); break; } }