Fix copyright lines
[folly.git] / folly / executors / test / FiberIOExecutorTest.cpp
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
19 #include <folly/executors/FiberIOExecutor.h>
20 #include <folly/executors/IOThreadPoolExecutor.h>
21
22 #include <folly/portability/GTest.h>
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);
31   folly::FiberIOExecutor e(tpe);
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);
39   folly::FiberIOExecutor e(tpe);
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 }