TEST(Coro, CurrentExecutor) { ScopedEventBaseThread evbThread; auto future = via(evbThread.getEventBase(), taskYield(evbThread.getEventBase())); future.wait(); EXPECT_EQ(42, future.get()); }
TEST(Coro, NestedThreads) { ScopedEventBaseThread evbThread; auto future = via(evbThread.getEventBase(), taskThread()); future.wait(); EXPECT_EQ(42, future.get()); }
TEST(Coro, LargeStack) { ScopedEventBaseThread evbThread; auto future = via(evbThread.getEventBase(), taskRecursion(5000)); future.wait(); EXPECT_EQ(5000, future.get()); }
TEST_F(ScopedEventBaseThreadTest, example) { ScopedEventBaseThread sebt; Baton<> done; sebt.getEventBase()->runInEventBaseThread([&] { done.post(); }); ASSERT_TRUE(done.timed_wait(seconds(1))); }
TEST_F(ScopedEventBaseThreadTest, default_manager) { auto ebm = EventBaseManager::get(); ScopedEventBaseThread sebt; auto sebt_eb = sebt.getEventBase(); auto ebm_eb = static_cast<EventBase*>(nullptr); sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm->getEventBase(); }); EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb)); }
TEST(Coro, Sleep) { ScopedEventBaseThread evbThread; auto startTime = std::chrono::steady_clock::now(); auto future = via(evbThread.getEventBase(), taskSleep()); EXPECT_FALSE(future.await_ready()); future.wait(); // The total time should be roughly 1 second. Some builds, especially // optimized ones, may result in slightly less than 1 second, so we perform // rounding here. auto totalTime = std::chrono::steady_clock::now() - startTime; EXPECT_GE( chrono::round<std::chrono::seconds>(totalTime), std::chrono::seconds{1}); EXPECT_TRUE(future.await_ready()); }
int main(int argc, char* argv[]) { FLAGS_logtostderr = true; FLAGS_minloglevel = 0; #ifdef OSS google::ParseCommandLineFlags(&argc, &argv, true); #else gflags::ParseCommandLineFlags(&argc, &argv, true); #endif google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); ScopedEventBaseThread eventBaseThread; std::unique_ptr<StandardReactiveSocket> reactiveSocket; Callback callback; auto stats = std::make_shared<StatsPrinter>(); eventBaseThread.getEventBase()->runInEventBaseThreadAndWait( [&callback, &reactiveSocket, &eventBaseThread, stats]() { folly::AsyncSocket::UniquePtr socket( new folly::AsyncSocket(eventBaseThread.getEventBase())); folly::SocketAddress addr(FLAGS_host, FLAGS_port, true); socket->connect(&callback, addr); std::cout << "attempting connection to " << addr.describe() << std::endl; std::unique_ptr<DuplexConnection> connection = std::make_unique<TcpDuplexConnection>( std::move(socket), inlineExecutor(), stats); std::unique_ptr<DuplexConnection> framedConnection = std::make_unique<FramedDuplexConnection>( std::move(connection), inlineExecutor()); std::unique_ptr<RequestHandler> requestHandler = std::make_unique<ClientRequestHandler>(); reactiveSocket = StandardReactiveSocket::fromClientConnection( *eventBaseThread.getEventBase(), std::move(framedConnection), std::move(requestHandler), ConnectionSetupPayload( "text/plain", "text/plain", Payload("meta", "data")), stats, std::make_unique<FollyKeepaliveTimer>( *eventBaseThread.getEventBase(), std::chrono::milliseconds(5000))); // reactiveSocket->requestSubscription( // Payload("from client"), std::make_shared<PrintSubscriber>()); }); std::string name; std::getline(std::cin, name); eventBaseThread.getEventBase()->runInEventBaseThreadAndWait( [&reactiveSocket]() { reactiveSocket.reset(nullptr); }); return 0; }
int main(int argc, char* argv[]) { FLAGS_logtostderr = true; FLAGS_minloglevel = 0; google::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]); google::InstallFailureSignalHandler(); ScopedEventBaseThread eventBaseThread; std::unique_ptr<StandardReactiveSocket> reactiveSocket; Callback callback; StatsPrinter stats; auto token = ResumeIdentificationToken::generateNew(); eventBaseThread.getEventBase()->runInEventBaseThreadAndWait([&]() { folly::SocketAddress addr(FLAGS_host, FLAGS_port, true); folly::AsyncSocket::UniquePtr socket( new folly::AsyncSocket(eventBaseThread.getEventBase())); socket->connect(&callback, addr); LOG(INFO) << "attempting connection to " << addr.describe(); std::unique_ptr<DuplexConnection> connection = folly::make_unique<TcpDuplexConnection>( std::move(socket), inlineExecutor(), stats); std::unique_ptr<DuplexConnection> framedConnection = folly::make_unique<FramedDuplexConnection>( std::move(connection), *eventBaseThread.getEventBase()); std::unique_ptr<RequestHandler> requestHandler = folly::make_unique<ClientRequestHandler>(); reactiveSocket = StandardReactiveSocket::disconnectedClient( *eventBaseThread.getEventBase(), std::move(requestHandler), stats, folly::make_unique<FollyKeepaliveTimer>( *eventBaseThread.getEventBase(), std::chrono::seconds(10))); reactiveSocket->onConnected([](ReactiveSocket& socket) { LOG(INFO) << "socket connected " << &socket; }); reactiveSocket->onDisconnected([](ReactiveSocket& socket) { LOG(INFO) << "socket disconnect " << &socket; }); reactiveSocket->onClosed([](ReactiveSocket& socket) { LOG(INFO) << "socket closed " << &socket; }); LOG(INFO) << "requestStream:"; reactiveSocket->requestStream( Payload("from client"), std::make_shared<PrintSubscriber>()); LOG(INFO) << "connecting RS ..."; reactiveSocket->clientConnect( std::make_shared<FrameTransport>(std::move(framedConnection)), ConnectionSetupPayload( "text/plain", "text/plain", Payload("meta", "data"), true, token)); }); std::string input; std::getline(std::cin, input); eventBaseThread.getEventBase()->runInEventBaseThreadAndWait([&]() { LOG(INFO) << "disconnecting RS ..."; reactiveSocket->disconnect(); LOG(INFO) << "requestStream:"; reactiveSocket->requestStream( Payload("from client2"), std::make_shared<PrintSubscriber>()); }); std::getline(std::cin, input); eventBaseThread.getEventBase()->runInEventBaseThreadAndWait([&]() { folly::SocketAddress addr(FLAGS_host, FLAGS_port, true); LOG(INFO) << "new TCP connection ..."; folly::AsyncSocket::UniquePtr socketResume( new folly::AsyncSocket(eventBaseThread.getEventBase())); socketResume->connect(&callback, addr); std::unique_ptr<DuplexConnection> connectionResume = folly::make_unique<TcpDuplexConnection>( std::move(socketResume), inlineExecutor(), stats); std::unique_ptr<DuplexConnection> framedConnectionResume = folly::make_unique<FramedDuplexConnection>( std::move(connectionResume), inlineExecutor()); LOG(INFO) << "try resume ..."; reactiveSocket->tryClientResume( token, std::make_shared<FrameTransport>(std::move(framedConnectionResume)), folly::make_unique<ResumeCallback>()); }); std::getline(std::cin, input); // TODO why need to shutdown in eventbase? eventBaseThread.getEventBase()->runInEventBaseThreadAndWait( [&reactiveSocket]() { LOG(INFO) << "releasing RS"; reactiveSocket.reset(nullptr); }); return 0; }