AsyncTimeoutTest.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/AsyncTimeout.h>
  17. #include <folly/io/async/EventBase.h>
  18. #include <folly/portability/GTest.h>
  19. namespace folly {
  20. TEST(AsyncTimeout, make) {
  21. int value = 0;
  22. int const expected = 10;
  23. EventBase manager;
  24. auto observer =
  25. AsyncTimeout::make(manager, [&]() noexcept { value = expected; });
  26. observer->scheduleTimeout(std::chrono::milliseconds(100));
  27. manager.loop();
  28. EXPECT_EQ(expected, value);
  29. }
  30. TEST(AsyncTimeout, schedule) {
  31. int value = 0;
  32. int const expected = 10;
  33. EventBase manager;
  34. auto observer = AsyncTimeout::schedule(
  35. std::chrono::milliseconds(100), manager, [&]() noexcept {
  36. value = expected;
  37. });
  38. manager.loop();
  39. EXPECT_EQ(expected, value);
  40. }
  41. TEST(AsyncTimeout, schedule_immediate) {
  42. int value = 0;
  43. int const expected = 10;
  44. EventBase manager;
  45. auto observer = AsyncTimeout::schedule(
  46. std::chrono::milliseconds(0), manager, [&]() noexcept {
  47. value = expected;
  48. });
  49. manager.loop();
  50. EXPECT_EQ(expected, value);
  51. }
  52. TEST(AsyncTimeout, cancel_make) {
  53. int value = 0;
  54. int const expected = 10;
  55. EventBase manager;
  56. auto observer =
  57. AsyncTimeout::make(manager, [&]() noexcept { value = expected; });
  58. std::weak_ptr<RequestContext> rctx_weak_ptr;
  59. {
  60. RequestContextScopeGuard rctx_guard;
  61. rctx_weak_ptr = RequestContext::saveContext();
  62. observer->scheduleTimeout(std::chrono::milliseconds(100));
  63. observer->cancelTimeout();
  64. }
  65. // Ensure that RequestContext created for the scope has been released and
  66. // deleted.
  67. EXPECT_EQ(rctx_weak_ptr.expired(), true);
  68. manager.loop();
  69. EXPECT_NE(expected, value);
  70. }
  71. TEST(AsyncTimeout, cancel_schedule) {
  72. int value = 0;
  73. int const expected = 10;
  74. EventBase manager;
  75. std::unique_ptr<AsyncTimeout> observer;
  76. std::weak_ptr<RequestContext> rctx_weak_ptr;
  77. {
  78. RequestContextScopeGuard rctx_guard;
  79. rctx_weak_ptr = RequestContext::saveContext();
  80. observer = AsyncTimeout::schedule(
  81. std::chrono::milliseconds(100), manager, [&]() noexcept {
  82. value = expected;
  83. });
  84. observer->cancelTimeout();
  85. }
  86. // Ensure that RequestContext created for the scope has been released and
  87. // deleted.
  88. EXPECT_EQ(rctx_weak_ptr.expired(), true);
  89. manager.loop();
  90. EXPECT_NE(expected, value);
  91. }
  92. } // namespace folly