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