proxygen
EventBaseThreadTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-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 
18 
19 #include <chrono>
20 
25 
26 using namespace std;
27 using namespace std::chrono;
28 using namespace folly;
29 
31 
33  EventBaseThread ebt(true, nullptr, "monkey");
34 
35  Baton<> done;
37  EXPECT_EQ(getCurrentThreadName().value(), "monkey");
38  done.post();
39  });
40  ASSERT_TRUE(done.try_wait_for(seconds(1)));
41 }
42 
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.try_wait_for(seconds(1)));
54 
55  EXPECT_NE(nullptr, ebt.getEventBase());
56  ebt.stop();
57  EXPECT_EQ(nullptr, ebt.getEventBase());
58  }
59 }
60 
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.try_wait_for(seconds(1)));
73 }
74 
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.try_wait_for(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 }
EventBase * getEventBase() const
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
Definition: lib.cpp:18
FOLLY_ALWAYS_INLINE bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout, const WaitOptions &opt=wait_options()) noexcept
Definition: Baton.h:206
EventBase * getEventBase() const
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
static const char *const value
Definition: Conv.cpp:50
void post() noexcept
Definition: Baton.h:123
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
TEST_F(EventBaseThreadTest, example)
Optional< std::string > getCurrentThreadName()
Definition: ThreadName.cpp:105
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
void start(folly::StringPiece threadName=folly::StringPiece())