2017
[folly.git] / folly / io / async / test / EventBaseThreadTest.cpp
1 /*
2  * Copyright 2017 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 <folly/io/async/EventBaseThread.h>
18
19 #include <chrono>
20
21 #include <folly/Baton.h>
22 #include <folly/io/async/EventBaseManager.h>
23 #include <folly/portability/GTest.h>
24
25 using namespace std;
26 using namespace std::chrono;
27 using namespace folly;
28
29 class EventBaseThreadTest : public testing::Test {};
30
31 TEST_F(EventBaseThreadTest, example) {
32   EventBaseThread ebt;
33
34   Baton<> done;
35   ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
36   ASSERT_TRUE(done.timed_wait(seconds(1)));
37 }
38
39 TEST_F(EventBaseThreadTest, start_stop) {
40   EventBaseThread ebt(false);
41
42   for (size_t i = 0; i < 4; ++i) {
43     EXPECT_EQ(nullptr, ebt.getEventBase());
44     ebt.start();
45     EXPECT_NE(nullptr, ebt.getEventBase());
46
47     Baton<> done;
48     ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
49     ASSERT_TRUE(done.timed_wait(seconds(1)));
50
51     EXPECT_NE(nullptr, ebt.getEventBase());
52     ebt.stop();
53     EXPECT_EQ(nullptr, ebt.getEventBase());
54   }
55 }
56
57 TEST_F(EventBaseThreadTest, move) {
58   auto ebt0 = EventBaseThread();
59   auto ebt1 = std::move(ebt0);
60   auto ebt2 = std::move(ebt1);
61
62   EXPECT_EQ(nullptr, ebt0.getEventBase());
63   EXPECT_EQ(nullptr, ebt1.getEventBase());
64   EXPECT_NE(nullptr, ebt2.getEventBase());
65
66   Baton<> done;
67   ebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
68   ASSERT_TRUE(done.timed_wait(seconds(1)));
69 }
70
71 TEST_F(EventBaseThreadTest, self_move) {
72   EventBaseThread ebt0;
73   auto ebt = std::move(ebt0);
74
75   EXPECT_NE(nullptr, ebt.getEventBase());
76
77   Baton<> done;
78   ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
79   ASSERT_TRUE(done.timed_wait(seconds(1)));
80 }
81
82 TEST_F(EventBaseThreadTest, default_manager) {
83   auto ebm = EventBaseManager::get();
84   EventBaseThread ebt;
85   auto ebt_eb = ebt.getEventBase();
86   auto ebm_eb = static_cast<EventBase*>(nullptr);
87   ebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm->getEventBase(); });
88   EXPECT_EQ(uintptr_t(ebt_eb), uintptr_t(ebm_eb));
89 }
90
91 TEST_F(EventBaseThreadTest, custom_manager) {
92   EventBaseManager ebm;
93   EventBaseThread ebt(&ebm);
94   auto ebt_eb = ebt.getEventBase();
95   auto ebm_eb = static_cast<EventBase*>(nullptr);
96   ebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm.getEventBase(); });
97   EXPECT_EQ(uintptr_t(ebt_eb), uintptr_t(ebm_eb));
98 }