EventBaseThreadTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2016-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/EventBaseThread.h>
  17. #include <chrono>
  18. #include <folly/io/async/EventBaseManager.h>
  19. #include <folly/portability/GTest.h>
  20. #include <folly/synchronization/Baton.h>
  21. #include <folly/system/ThreadName.h>
  22. using namespace std;
  23. using namespace std::chrono;
  24. using namespace folly;
  25. class EventBaseThreadTest : public testing::Test {};
  26. TEST_F(EventBaseThreadTest, example) {
  27. EventBaseThread ebt(true, nullptr, "monkey");
  28. Baton<> done;
  29. ebt.getEventBase()->runInEventBaseThread([&] {
  30. EXPECT_EQ(getCurrentThreadName().value(), "monkey");
  31. done.post();
  32. });
  33. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  34. }
  35. TEST_F(EventBaseThreadTest, start_stop) {
  36. EventBaseThread ebt(false);
  37. for (size_t i = 0; i < 4; ++i) {
  38. EXPECT_EQ(nullptr, ebt.getEventBase());
  39. ebt.start();
  40. EXPECT_NE(nullptr, ebt.getEventBase());
  41. Baton<> done;
  42. ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  43. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  44. EXPECT_NE(nullptr, ebt.getEventBase());
  45. ebt.stop();
  46. EXPECT_EQ(nullptr, ebt.getEventBase());
  47. }
  48. }
  49. TEST_F(EventBaseThreadTest, move) {
  50. auto ebt0 = EventBaseThread();
  51. auto ebt1 = std::move(ebt0);
  52. auto ebt2 = std::move(ebt1);
  53. EXPECT_EQ(nullptr, ebt0.getEventBase());
  54. EXPECT_EQ(nullptr, ebt1.getEventBase());
  55. EXPECT_NE(nullptr, ebt2.getEventBase());
  56. Baton<> done;
  57. ebt2.getEventBase()->runInEventBaseThread([&] { done.post(); });
  58. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  59. }
  60. TEST_F(EventBaseThreadTest, self_move) {
  61. EventBaseThread ebt0;
  62. auto ebt = std::move(ebt0);
  63. EXPECT_NE(nullptr, ebt.getEventBase());
  64. Baton<> done;
  65. ebt.getEventBase()->runInEventBaseThread([&] { done.post(); });
  66. ASSERT_TRUE(done.try_wait_for(seconds(1)));
  67. }
  68. TEST_F(EventBaseThreadTest, default_manager) {
  69. auto ebm = EventBaseManager::get();
  70. EventBaseThread ebt;
  71. auto ebt_eb = ebt.getEventBase();
  72. auto ebm_eb = static_cast<EventBase*>(nullptr);
  73. ebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm->getEventBase(); });
  74. EXPECT_EQ(uintptr_t(ebt_eb), uintptr_t(ebm_eb));
  75. }
  76. TEST_F(EventBaseThreadTest, custom_manager) {
  77. EventBaseManager ebm;
  78. EventBaseThread ebt(&ebm);
  79. auto ebt_eb = ebt.getEventBase();
  80. auto ebm_eb = static_cast<EventBase*>(nullptr);
  81. ebt_eb->runInEventBaseThreadAndWait([&] { ebm_eb = ebm.getEventBase(); });
  82. EXPECT_EQ(uintptr_t(ebt_eb), uintptr_t(ebm_eb));
  83. }