folly copyright 2015 -> copyright 2016
[folly.git] / folly / io / async / test / AsyncTimeoutTest.cpp
1 /*
2  * Copyright 2016 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
20 #include <gtest/gtest.h>
21
22 namespace folly {
23
24 TEST(AsyncTimeout, make) {
25   int value = 0;
26   int const expected = 10;
27   EventBase manager;
28
29   auto observer = AsyncTimeout::make(
30     manager,
31     [&]() noexcept { value = expected; }
32   );
33
34   observer->scheduleTimeout(std::chrono::milliseconds(100));
35
36   manager.loop();
37
38   EXPECT_EQ(expected, value);
39 }
40
41 TEST(AsyncTimeout, schedule) {
42   int value = 0;
43   int const expected = 10;
44   EventBase manager;
45
46   auto observer = AsyncTimeout::schedule(
47     std::chrono::milliseconds(100),
48     manager,
49     [&]() noexcept { value = expected; }
50   );
51
52   manager.loop();
53
54   EXPECT_EQ(expected, value);
55 }
56
57 TEST(AsyncTimeout, cancel_make) {
58   int value = 0;
59   int const expected = 10;
60   EventBase manager;
61
62   auto observer = AsyncTimeout::make(
63     manager,
64     [&]() noexcept { value = expected; }
65   );
66
67   observer->scheduleTimeout(std::chrono::milliseconds(100));
68   observer->cancelTimeout();
69
70   manager.loop();
71
72   EXPECT_NE(expected, value);
73 }
74
75 TEST(AsyncTimeout, cancel_schedule) {
76   int value = 0;
77   int const expected = 10;
78   EventBase manager;
79
80   auto observer = AsyncTimeout::schedule(
81     std::chrono::milliseconds(100),
82     manager,
83     [&]() noexcept { value = expected; }
84   );
85
86   observer->cancelTimeout();
87
88   manager.loop();
89
90   EXPECT_NE(expected, value);
91 }
92
93 } // namespace folly {