proxygen
AsyncTimeoutTest.cpp
Go to the documentation of this file.
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 
20 
21 namespace folly {
22 
24  int value = 0;
25  int const expected = 10;
26  EventBase manager;
27 
28  auto observer =
29  AsyncTimeout::make(manager, [&]() noexcept { value = expected; });
30 
31  observer->scheduleTimeout(std::chrono::milliseconds(100));
32 
33  manager.loop();
34 
35  EXPECT_EQ(expected, value);
36 }
37 
38 TEST(AsyncTimeout, schedule) {
39  int value = 0;
40  int const expected = 10;
41  EventBase manager;
42 
43  auto observer = AsyncTimeout::schedule(
44  std::chrono::milliseconds(100), manager, [&]() noexcept {
45  value = expected;
46  });
47 
48  manager.loop();
49 
50  EXPECT_EQ(expected, value);
51 }
52 
53 TEST(AsyncTimeout, schedule_immediate) {
54  int value = 0;
55  int const expected = 10;
56  EventBase manager;
57 
58  auto observer = AsyncTimeout::schedule(
59  std::chrono::milliseconds(0), manager, [&]() noexcept {
60  value = expected;
61  });
62 
63  manager.loop();
64  EXPECT_EQ(expected, value);
65 }
66 
67 TEST(AsyncTimeout, cancel_make) {
68  int value = 0;
69  int const expected = 10;
70  EventBase manager;
71 
72  auto observer =
73  AsyncTimeout::make(manager, [&]() noexcept { value = expected; });
74 
75  std::weak_ptr<RequestContext> rctx_weak_ptr;
76 
77  {
78  RequestContextScopeGuard rctx_guard;
79  rctx_weak_ptr = RequestContext::saveContext();
80  observer->scheduleTimeout(std::chrono::milliseconds(100));
81  observer->cancelTimeout();
82  }
83 
84  // Ensure that RequestContext created for the scope has been released and
85  // deleted.
86  EXPECT_EQ(rctx_weak_ptr.expired(), true);
87 
88  manager.loop();
89 
90  EXPECT_NE(expected, value);
91 }
92 
93 TEST(AsyncTimeout, cancel_schedule) {
94  int value = 0;
95  int const expected = 10;
96  EventBase manager;
97  std::unique_ptr<AsyncTimeout> observer;
98  std::weak_ptr<RequestContext> rctx_weak_ptr;
99 
100  {
101  RequestContextScopeGuard rctx_guard;
102  rctx_weak_ptr = RequestContext::saveContext();
103 
104  observer = AsyncTimeout::schedule(
105  std::chrono::milliseconds(100), manager, [&]() noexcept {
106  value = expected;
107  });
108 
109  observer->cancelTimeout();
110  }
111 
112  // Ensure that RequestContext created for the scope has been released and
113  // deleted.
114  EXPECT_EQ(rctx_weak_ptr.expired(), true);
115 
116  manager.loop();
117 
118  EXPECT_NE(expected, value);
119 }
120 
121 } // namespace folly
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
static std::shared_ptr< RequestContext > saveContext()
Definition: Request.h:196
static std::unique_ptr< AsyncTimeout > make(TimeoutManager &manager, TCallback &&callback)
Definition: AsyncTimeout.h:261
static std::unique_ptr< AsyncTimeout > schedule(TimeoutManager::timeout_type timeout, TimeoutManager &manager, TCallback &&callback)
Definition: AsyncTimeout.h:270
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
uint64_t value(const typename LockFreeRingBuffer< T, Atom >::Cursor &rbcursor)
TEST(SequencedExecutor, CPUThreadPoolExecutor)