proxygen
FiberIOExecutorTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <memory>
18 
21 
23 
24 namespace {
25 
26 class FiberIOExecutorTest : public testing::Test {};
27 } // namespace
28 
29 TEST_F(FiberIOExecutorTest, event_base) {
30  auto tpe = std::make_shared<folly::IOThreadPoolExecutor>(1);
32 
33  ASSERT_NE(e.getEventBase(), nullptr);
34  ASSERT_EQ(e.getEventBase(), tpe->getEventBase());
35 }
36 
37 TEST_F(FiberIOExecutorTest, basic_execution) {
38  auto tpe = std::make_shared<folly::IOThreadPoolExecutor>(1);
40 
41  // FiberIOExecutor should add tasks using the FiberManager mapped to the
42  // IOThreadPoolExecutor's event base.
43  folly::Baton<> baton;
44  bool inContext = false;
45 
46  e.add([&]() {
47  inContext = folly::fibers::onFiber();
48  auto& lc = dynamic_cast<folly::fibers::EventBaseLoopController&>(
49  folly::fibers::getFiberManager(*e.getEventBase()).loopController());
50  auto& eb = lc.getEventBase()->getEventBase();
51  inContext =
52  inContext && &eb == folly::EventBaseManager::get()->getEventBase();
53  baton.post();
54  });
55  baton.wait();
56 
57  ASSERT_TRUE(inContext);
58 }
EventBase * getEventBase() const
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
TEST_F(TestInfoTest, Names)
An IOExecutor that executes funcs under mapped fiber context.
static EventBaseManager * get()
FOLLY_ALWAYS_INLINE void wait(const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:170
void post() noexcept
Definition: Baton.h:123
#define ASSERT_NE(val1, val2)
Definition: gtest.h:1960
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
FiberManager & getFiberManager(EventBase &evb, const FiberManager::Options &opts)