proxygen
ScopedEventBaseThreadTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2015-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 #include <string>
21 
22 #include <folly/Optional.h>
27 
28 using namespace std;
29 using namespace std::chrono;
30 using namespace folly;
31 
33 
36 
37  Baton<> done;
38  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
39  ASSERT_TRUE(done.try_wait_for(seconds(1)));
40 }
41 
43  static constexpr StringPiece kThreadName{"named_example"};
44 
45  Optional<std::string> createdThreadName;
46  Baton<> done;
47 
48  ScopedEventBaseThread sebt{kThreadName};
50  createdThreadName = folly::getCurrentThreadName();
51  done.post();
52  });
53 
54  ASSERT_TRUE(done.try_wait_for(seconds(1)));
55  if (createdThreadName) {
56  ASSERT_EQ(kThreadName.toString(), createdThreadName.value());
57  }
58 }
59 
60 TEST_F(ScopedEventBaseThreadTest, default_manager) {
61  auto ebm = EventBaseManager::get();
63  auto sebt_eb = sebt.getEventBase();
64  auto ebm_eb = static_cast<EventBase*>(nullptr);
65  sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm->getEventBase(); });
66  EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
67 }
68 
70  EventBaseManager ebm;
71  ScopedEventBaseThread sebt(&ebm);
72  auto sebt_eb = sebt.getEventBase();
73  auto ebm_eb = static_cast<EventBase*>(nullptr);
74  sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm.getEventBase(); });
75  EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
76 }
77 
78 TEST_F(ScopedEventBaseThreadTest, eb_dtor_in_io_thread) {
80  sebt.emplace();
81  auto const io_thread_id = sebt->getThreadId();
82  EXPECT_NE(this_thread::get_id(), io_thread_id) << "sanity";
83 
84  auto const eb = sebt->getEventBase();
85  thread::id eb_dtor_thread_id;
87  [&] { eb_dtor_thread_id = this_thread::get_id(); }));
88  sebt.clear();
89  EXPECT_EQ(io_thread_id, eb_dtor_thread_id);
90 }
EventBase * getEventBase() const
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
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
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
std::thread::id getThreadId() const
Value & emplace(Args &&...args)
Definition: Optional.h:231
void post() noexcept
Definition: Baton.h:123
TEST_F(ScopedEventBaseThreadTest, example)
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
void runOnDestruction(LoopCallback *callback)
Definition: EventBase.cpp:542
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 clear() noexcept
Definition: Optional.h:251