EventBaseLocalTest.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/EventBaseLocal.h>
  17. #include <folly/portability/GTest.h>
  18. struct Foo {
  19. Foo(int n_, std::function<void()> dtorFn_)
  20. : n(n_), dtorFn(std::move(dtorFn_)) {}
  21. ~Foo() {
  22. dtorFn();
  23. }
  24. int n;
  25. std::function<void()> dtorFn;
  26. };
  27. TEST(EventBaseLocalTest, Basic) {
  28. int dtorCnt = 0;
  29. folly::EventBase evb1;
  30. {
  31. folly::EventBaseLocal<Foo> foo;
  32. EXPECT_EQ(foo.get(evb1), nullptr);
  33. foo.emplace(evb1, new Foo(5, [&]() { ++dtorCnt; }));
  34. EXPECT_EQ(foo.get(evb1)->n, 5);
  35. {
  36. folly::EventBase evb2;
  37. foo.emplace(evb2, new Foo(6, [&]() { ++dtorCnt; }));
  38. EXPECT_EQ(foo.get(evb2)->n, 6);
  39. foo.erase(evb2);
  40. EXPECT_EQ(dtorCnt, 1); // should dtor a Foo when we erase
  41. EXPECT_EQ(foo.get(evb2), nullptr);
  42. foo.emplace(evb2, 7, [&]() { ++dtorCnt; });
  43. EXPECT_EQ(foo.get(evb2)->n, 7);
  44. }
  45. EXPECT_EQ(dtorCnt, 2); // should dtor a Foo when evb2 destructs
  46. }
  47. EXPECT_EQ(dtorCnt, 2); // should schedule Foo destructor, when foo destructs
  48. evb1.loop();
  49. EXPECT_EQ(dtorCnt, 3); // Foo will be destroyed in EventBase loop
  50. }
  51. TEST(EventBaseLocalTest, getOrCreate) {
  52. folly::EventBase evb1;
  53. folly::EventBaseLocal<int> ints;
  54. EXPECT_EQ(ints.getOrCreate(evb1), 0);
  55. EXPECT_EQ(ints.getOrCreate(evb1, 5), 0);
  56. folly::EventBase evb2;
  57. EXPECT_EQ(ints.getOrCreate(evb2, 5), 5);
  58. ints.erase(evb2);
  59. auto creator = []() { return new int(4); };
  60. EXPECT_EQ(ints.getOrCreateFn(evb2, creator), 4);
  61. }
  62. using IntPtr = std::unique_ptr<int>;
  63. TEST(EventBaseLocalTest, getOrCreateNoncopyable) {
  64. folly::EventBase evb1;
  65. folly::EventBaseLocal<IntPtr> ints;
  66. EXPECT_EQ(ints.getOrCreate(evb1), IntPtr());
  67. EXPECT_EQ(ints.getOrCreate(evb1, std::make_unique<int>(5)), IntPtr());
  68. folly::EventBase evb2;
  69. EXPECT_EQ(*ints.getOrCreate(evb2, std::make_unique<int>(5)), 5);
  70. }
  71. TEST(EventBaseLocalTest, emplaceNoncopyable) {
  72. folly::EventBase evb;
  73. folly::EventBaseLocal<IntPtr> ints;
  74. ints.emplace(evb, std::make_unique<int>(42));
  75. EXPECT_EQ(42, **ints.get(evb));
  76. }