Use the GTest portability headers
[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 #include <folly/Baton.h>
21 #include <folly/portability/GTest.h>
22
23 using namespace std;
24 using namespace std::chrono;
25 using namespace folly;
26
27 class ScopedEventBaseThreadTest : public testing::Test {};
28
29 TEST_F(ScopedEventBaseThreadTest, example) {
30   ScopedEventBaseThread sebt;
31
32   Baton<> done;
33   sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
34   done.timed_wait(steady_clock::now() + milliseconds(100));
35 }
36
37 TEST_F(ScopedEventBaseThreadTest, start_stop) {
38   ScopedEventBaseThread sebt(false);
39
40   for (size_t i = 0; i < 4; ++i) {
41     EXPECT_EQ(nullptr, sebt.getEventBase());
42     sebt.start();
43     EXPECT_NE(nullptr, sebt.getEventBase());
44
45     Baton<> done;
46     sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
47     done.timed_wait(steady_clock::now() + milliseconds(100));
48
49     EXPECT_NE(nullptr, sebt.getEventBase());
50     sebt.stop();
51     EXPECT_EQ(nullptr, sebt.getEventBase());
52   }
53 }
54
55 TEST_F(ScopedEventBaseThreadTest, move) {
56   auto sebt0 = ScopedEventBaseThread();
57   auto sebt1 = std::move(sebt0);
58   auto sebt2 = std::move(sebt1);
59
60   EXPECT_EQ(nullptr, sebt0.getEventBase());
61   EXPECT_EQ(nullptr, sebt1.getEventBase());
62   EXPECT_NE(nullptr, sebt2.getEventBase());
63
64   Baton<> done;
65   sebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
66   done.timed_wait(steady_clock::now() + milliseconds(100));
67 }
68
69 TEST_F(ScopedEventBaseThreadTest, self_move) {
70   ScopedEventBaseThread sebt0;
71   auto sebt = std::move(sebt0);
72
73   EXPECT_NE(nullptr, sebt.getEventBase());
74
75   Baton<> done;
76   sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
77   done.timed_wait(steady_clock::now() + milliseconds(100));
78 }
79
80 TEST_F(ScopedEventBaseThreadTest, manager) {
81   EventBaseManager ebm;
82   ScopedEventBaseThread sebt(&ebm);
83   auto sebt_eb = sebt.getEventBase();
84   auto ebm_eb = (EventBase*)nullptr;
85   sebt_eb->runInEventBaseThreadAndWait([&] {
86       ebm_eb = ebm.getEventBase();
87   });
88   EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
89 }