Split EventBaseThread from ScopedEventBaseThread
[folly.git] / folly / io / async / test / ScopedEventBaseThreadTest.cpp
1 /*
2  * Copyright 2016 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/ScopedEventBaseThread.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 ScopedEventBaseThreadTest : public testing::Test {};
30
31 TEST_F(ScopedEventBaseThreadTest, example) {
32   ScopedEventBaseThread sebt;
33
34   Baton<> done;
35   sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
36   ASSERT_TRUE(done.timed_wait(seconds(1)));
37 }
38
39 TEST_F(ScopedEventBaseThreadTest, default_manager) {
40   auto ebm = EventBaseManager::get();
41   ScopedEventBaseThread sebt;
42   auto sebt_eb = sebt.getEventBase();
43   auto ebm_eb = static_cast<EventBase*>(nullptr);
44   sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm->getEventBase(); });
45   EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
46 }
47
48 TEST_F(ScopedEventBaseThreadTest, custom_manager) {
49   EventBaseManager ebm;
50   ScopedEventBaseThread sebt(&ebm);
51   auto sebt_eb = sebt.getEventBase();
52   auto ebm_eb = static_cast<EventBase*>(nullptr);
53   sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm.getEventBase(); });
54   EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
55 }