From 750586e0f3fc85aa6dec08ecd54a95fca1b4b7ca Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Mon, 17 Aug 2015 00:51:43 -0700 Subject: [PATCH] Extract folly/io/async/test/RequestContextTest.cpp (from Thrift). Summary: [Folly] Extract folly/io/async/test/RequestContextTest.cpp (from Thrift). Reviewed By: @haijunz Differential Revision: D2350908 --- folly/io/async/test/RequestContextTest.cpp | 82 ++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 folly/io/async/test/RequestContextTest.cpp diff --git a/folly/io/async/test/RequestContextTest.cpp b/folly/io/async/test/RequestContextTest.cpp new file mode 100644 index 00000000..529a15af --- /dev/null +++ b/folly/io/async/test/RequestContextTest.cpp @@ -0,0 +1,82 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +#include +#include + +#include +#include + +using namespace folly; + +class TestData : public RequestData { + public: + explicit TestData(int data) : data_(data) {} + ~TestData() override {} + int data_; +}; + +TEST(RequestContext, SimpleTest) { + EventBase base; + + + // There should always be a default context with get() + EXPECT_TRUE(RequestContext::get() != nullptr); + + + // but not with saveContext() + EXPECT_EQ(RequestContext::saveContext(), nullptr); + RequestContext::create(); + EXPECT_NE(RequestContext::saveContext(), nullptr); + RequestContext::create(); + EXPECT_NE(RequestContext::saveContext(), nullptr); + + EXPECT_EQ(nullptr, RequestContext::get()->getContextData("test")); + + RequestContext::get()->setContextData( + "test", + std::unique_ptr(new TestData(10))); + base.runInEventBaseThread([&](){ + EXPECT_TRUE(RequestContext::get() != nullptr); + auto data = dynamic_cast( + RequestContext::get()->getContextData("test"))->data_; + EXPECT_EQ(10, data); + base.terminateLoopSoon(); + }); + auto th = std::thread([&](){ + base.loopForever(); + }); + th.join(); + EXPECT_TRUE(RequestContext::get() != nullptr); + auto a = dynamic_cast( + RequestContext::get()->getContextData("test")); + auto data = a->data_; + EXPECT_EQ(10, data); + + RequestContext::setContext(std::shared_ptr()); + // There should always be a default context + EXPECT_TRUE(nullptr != RequestContext::get()); +} + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + google::InitGoogleLogging(argv[0]); + google::ParseCommandLineFlags(&argc, &argv, true); + + return RUN_ALL_TESTS(); +} -- 2.34.1