proxygen
ScopedEventBaseThreadTest.cpp File Reference

Go to the source code of this file.

Classes

class  ScopedEventBaseThreadTest
 

Functions

 TEST_F (ScopedEventBaseThreadTest, example)
 
 TEST_F (ScopedEventBaseThreadTest, named_example)
 
 TEST_F (ScopedEventBaseThreadTest, default_manager)
 
 TEST_F (ScopedEventBaseThreadTest, custom_manager)
 
 TEST_F (ScopedEventBaseThreadTest, eb_dtor_in_io_thread)
 

Function Documentation

TEST_F ( ScopedEventBaseThreadTest  ,
example   
)

Definition at line 34 of file ScopedEventBaseThreadTest.cpp.

References ASSERT_TRUE, folly::ScopedEventBaseThread::getEventBase(), folly::Baton< MayBlock, Atom >::post(), folly::EventBase::runInEventBaseThread(), and folly::Baton< MayBlock, Atom >::try_wait_for().

34  {
36 
37  Baton<> done;
38  sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
39  ASSERT_TRUE(done.try_wait_for(seconds(1)));
40 }
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
void post() noexcept
Definition: Baton.h:123
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( ScopedEventBaseThreadTest  ,
named_example   
)

Definition at line 42 of file ScopedEventBaseThreadTest.cpp.

References ASSERT_EQ, ASSERT_TRUE, folly::getCurrentThreadName(), folly::ScopedEventBaseThread::getEventBase(), folly::Baton< MayBlock, Atom >::post(), folly::EventBase::runInEventBaseThread(), folly::Baton< MayBlock, Atom >::try_wait_for(), and folly::Optional< Value >::value().

42  {
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 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
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
void post() noexcept
Definition: Baton.h:123
FOLLY_CPP14_CONSTEXPR const Value & value() const &
Definition: Optional.h:268
Optional< std::string > getCurrentThreadName()
Definition: ThreadName.cpp:105
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
TEST_F ( ScopedEventBaseThreadTest  ,
default_manager   
)

Definition at line 60 of file ScopedEventBaseThreadTest.cpp.

References EXPECT_EQ, folly::pushmi::operators::get, folly::ScopedEventBaseThread::getEventBase(), and folly::EventBase::runInEventBaseThreadAndWait().

60  {
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 }
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
PUSHMI_INLINE_VAR constexpr detail::get_fn< T > get
Definition: submit.h:391
TEST_F ( ScopedEventBaseThreadTest  ,
custom_manager   
)

Definition at line 69 of file ScopedEventBaseThreadTest.cpp.

References EXPECT_EQ, folly::ScopedEventBaseThread::getEventBase(), folly::EventBaseManager::getEventBase(), and folly::EventBase::runInEventBaseThreadAndWait().

69  {
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 }
EventBase * getEventBase() const
bool runInEventBaseThreadAndWait(void(*fn)(T *), T *arg)
Definition: EventBase.h:799
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST_F ( ScopedEventBaseThreadTest  ,
eb_dtor_in_io_thread   
)

Definition at line 78 of file ScopedEventBaseThreadTest.cpp.

References folly::Optional< Value >::clear(), folly::Optional< Value >::emplace(), EXPECT_EQ, EXPECT_NE, folly::ScopedEventBaseThread::getEventBase(), folly::ScopedEventBaseThread::getThreadId(), and folly::EventBase::runOnDestruction().

78  {
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 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
std::thread::id getThreadId() const
Value & emplace(Args &&...args)
Definition: Optional.h:231
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void runOnDestruction(LoopCallback *callback)
Definition: EventBase.cpp:542
void clear() noexcept
Definition: Optional.h:251