Reset context shared_ptr in AsyncTimeout::cancelTimeout()
[folly.git] / folly / io / async / test / AsyncTimeoutTest.cpp
1 /*
2  * Copyright 2017 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
17 #include <folly/io/async/AsyncTimeout.h>
18 #include <folly/io/async/EventBase.h>
19 #include <folly/portability/GTest.h>
20
21 namespace folly {
22
23 TEST(AsyncTimeout, make) {
24   int value = 0;
25   int const expected = 10;
26   EventBase manager;
27
28   auto observer = AsyncTimeout::make(
29     manager,
30     [&]() noexcept { value = expected; }
31   );
32
33   observer->scheduleTimeout(std::chrono::milliseconds(100));
34
35   manager.loop();
36
37   EXPECT_EQ(expected, value);
38 }
39
40 TEST(AsyncTimeout, schedule) {
41   int value = 0;
42   int const expected = 10;
43   EventBase manager;
44
45   auto observer = AsyncTimeout::schedule(
46     std::chrono::milliseconds(100),
47     manager,
48     [&]() noexcept { value = expected; }
49   );
50
51   manager.loop();
52
53   EXPECT_EQ(expected, value);
54 }
55
56 TEST(AsyncTimeout, schedule_immediate) {
57   int value = 0;
58   int const expected = 10;
59   EventBase manager;
60
61   auto observer = AsyncTimeout::schedule(
62       std::chrono::milliseconds(0), manager, [&]() noexcept {
63         value = expected;
64       });
65
66   manager.loop();
67   EXPECT_EQ(expected, value);
68 }
69
70 TEST(AsyncTimeout, cancel_make) {
71   int value = 0;
72   int const expected = 10;
73   EventBase manager;
74
75   auto observer = AsyncTimeout::make(
76     manager,
77     [&]() noexcept { value = expected; }
78   );
79
80   std::weak_ptr<RequestContext> rctx_weak_ptr;
81
82   {
83     RequestContextScopeGuard rctx_guard;
84     rctx_weak_ptr = RequestContext::saveContext();
85     observer->scheduleTimeout(std::chrono::milliseconds(100));
86     observer->cancelTimeout();
87   }
88
89   // Ensure that RequestContext created for the scope has been released and
90   // deleted.
91   EXPECT_EQ(rctx_weak_ptr.expired(), true);
92
93   manager.loop();
94
95   EXPECT_NE(expected, value);
96 }
97
98 TEST(AsyncTimeout, cancel_schedule) {
99   int value = 0;
100   int const expected = 10;
101   EventBase manager;
102   std::unique_ptr<AsyncTimeout> observer;
103   std::weak_ptr<RequestContext> rctx_weak_ptr;
104
105   {
106     RequestContextScopeGuard rctx_guard;
107     rctx_weak_ptr = RequestContext::saveContext();
108
109     observer = AsyncTimeout::schedule(
110         std::chrono::milliseconds(100), manager, [&]() noexcept {
111           value = expected;
112         });
113
114     observer->cancelTimeout();
115   }
116
117   // Ensure that RequestContext created for the scope has been released and
118   // deleted.
119   EXPECT_EQ(rctx_weak_ptr.expired(), true);
120
121   manager.loop();
122
123   EXPECT_NE(expected, value);
124 }
125
126 } // namespace folly