ScopedEventBaseThreadTest.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include <folly/io/async/ScopedEventBaseThread.h>
  17. #include <chrono>
  18. #include <string>
  19. #include <folly/Optional.h>
  20. #include <folly/io/async/EventBaseManager.h>
  21. #include <folly/portability/GTest.h>
  22. #include <folly/synchronization/Baton.h>
  23. #include <folly/system/ThreadName.h>
  24. using namespace std;
  25. using namespace std::chrono;
  26. using namespace folly;
  27. class ScopedEventBaseThreadTest : public testing::Test {};
  28. TEST_F(ScopedEventBaseThreadTest, example) {
  29. ScopedEventBaseThread sebt;
  30. Baton<> done;
  31. sebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  32. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  33. }
  34. TEST_F(ScopedEventBaseThreadTest, named_example) {
  35. static constexpr StringPiece kThreadName{"named_example"};
  36. Optional<std::string> createdThreadName;
  37. Baton<> done;
  38. ScopedEventBaseThread sebt{kThreadName};
  39. sebt.getEventBase()->runInEventBaseThread([&] {
  40. createdThreadName = folly::getCurrentThreadName();
  41. done.post();
  42. });
  43. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  44. if (createdThreadName) {
  45. ASSERT_EQ(kThreadName.toString(), createdThreadName.value());
  46. }
  47. }
  48. TEST_F(ScopedEventBaseThreadTest, default_manager) {
  49. auto ebm = EventBaseManager::get();
  50. ScopedEventBaseThread sebt;
  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. }
  56. TEST_F(ScopedEventBaseThreadTest, custom_manager) {
  57. EventBaseManager ebm;
  58. ScopedEventBaseThread sebt(&ebm);
  59. auto sebt_eb = sebt.getEventBase();
  60. auto ebm_eb = static_cast<EventBase*>(nullptr);
  61. sebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm.getEventBase(); });
  62. EXPECT_EQ(uintptr_t(sebt_eb), uintptr_t(ebm_eb));
  63. }
  64. TEST_F(ScopedEventBaseThreadTest, eb_dtor_in_io_thread) {
  65. Optional<ScopedEventBaseThread> sebt;
  66. sebt.emplace();
  67. auto const io_thread_id = sebt->getThreadId();
  68. EXPECT_NE(this_thread::get_id(), io_thread_id) << "sanity";
  69. auto const eb = sebt->getEventBase();
  70. thread::id eb_dtor_thread_id;
  71. eb->runOnDestruction(new EventBase::FunctionLoopCallback(
  72. [&] { eb_dtor_thread_id = this_thread::get_id(); }));
  73. sebt.clear();
  74. EXPECT_EQ(io_thread_id, eb_dtor_thread_id);
  75. }