2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/io/async/EventBaseThread.h>
21 #include <folly/Baton.h>
22 #include <folly/ThreadName.h>
23 #include <folly/io/async/EventBaseManager.h>
24 #include <folly/portability/GTest.h>
27 using namespace std::chrono;
28 using namespace folly;
30 class EventBaseThreadTest : public testing::Test {};
32 TEST_F(EventBaseThreadTest, example) {
33 EventBaseThread ebt(true, nullptr, "monkey");
36 ebt.getEventBase()->runInEventBaseThread([&] {
37 EXPECT_EQ(getCurrentThreadName().value(), "monkey");
40 ASSERT_TRUE(done.timed_wait(seconds(1)));
43 TEST_F(EventBaseThreadTest, start_stop) {
44 EventBaseThread ebt(false);
46 for (size_t i = 0; i < 4; ++i) {
47 EXPECT_EQ(nullptr, ebt.getEventBase());
49 EXPECT_NE(nullptr, ebt.getEventBase());
52 ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
53 ASSERT_TRUE(done.timed_wait(seconds(1)));
55 EXPECT_NE(nullptr, ebt.getEventBase());
57 EXPECT_EQ(nullptr, ebt.getEventBase());
61 TEST_F(EventBaseThreadTest, move) {
62 auto ebt0 = EventBaseThread();
63 auto ebt1 = std::move(ebt0);
64 auto ebt2 = std::move(ebt1);
66 EXPECT_EQ(nullptr, ebt0.getEventBase());
67 EXPECT_EQ(nullptr, ebt1.getEventBase());
68 EXPECT_NE(nullptr, ebt2.getEventBase());
71 ebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
72 ASSERT_TRUE(done.timed_wait(seconds(1)));
75 TEST_F(EventBaseThreadTest, self_move) {
77 auto ebt = std::move(ebt0);
79 EXPECT_NE(nullptr, ebt.getEventBase());
82 ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
83 ASSERT_TRUE(done.timed_wait(seconds(1)));
86 TEST_F(EventBaseThreadTest, default_manager) {
87 auto ebm = EventBaseManager::get();
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));
95 TEST_F(EventBaseThreadTest, custom_manager) {
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));