1 | /* |
2 | * Copyright 2014-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 | |
17 | #include <folly/futures/Future.h> |
18 | #include <folly/futures/Promise.h> |
19 | #include <folly/portability/GTest.h> |
20 | #include <folly/synchronization/Baton.h> |
21 | |
22 | using namespace folly; |
23 | |
24 | TEST(Interrupt, raise) { |
25 | using eggs_t = std::runtime_error; |
26 | Promise<Unit> p; |
27 | p.setInterruptHandler([&](const exception_wrapper& e) { |
28 | EXPECT_THROW(e.throw_exception(), eggs_t); |
29 | }); |
30 | p.getFuture().raise(eggs_t("eggs" )); |
31 | } |
32 | |
33 | TEST(Interrupt, cancel) { |
34 | Promise<Unit> p; |
35 | p.setInterruptHandler([&](const exception_wrapper& e) { |
36 | EXPECT_THROW(e.throw_exception(), FutureCancellation); |
37 | }); |
38 | p.getFuture().cancel(); |
39 | } |
40 | |
41 | TEST(Interrupt, handleThenInterrupt) { |
42 | Promise<int> p; |
43 | bool flag = false; |
44 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; }); |
45 | p.getFuture().cancel(); |
46 | EXPECT_TRUE(flag); |
47 | } |
48 | |
49 | TEST(Interrupt, interruptThenHandle) { |
50 | Promise<int> p; |
51 | bool flag = false; |
52 | p.getFuture().cancel(); |
53 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; }); |
54 | EXPECT_TRUE(flag); |
55 | } |
56 | |
57 | TEST(Interrupt, interruptAfterFulfilNoop) { |
58 | Promise<Unit> p; |
59 | bool flag = false; |
60 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; }); |
61 | p.setValue(); |
62 | p.getFuture().cancel(); |
63 | EXPECT_FALSE(flag); |
64 | } |
65 | |
66 | TEST(Interrupt, secondInterruptNoop) { |
67 | Promise<Unit> p; |
68 | int count = 0; |
69 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { count++; }); |
70 | auto f = p.getFuture(); |
71 | f.cancel(); |
72 | f.cancel(); |
73 | EXPECT_EQ(1, count); |
74 | } |
75 | |
76 | TEST(Interrupt, futureWithinTimedOut) { |
77 | Promise<int> p; |
78 | Baton<> done; |
79 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); }); |
80 | p.getFuture().within(std::chrono::milliseconds(1)); |
81 | // Give it 100ms to time out and call the interrupt handler |
82 | EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100))); |
83 | } |
84 | |
85 | TEST(Interrupt, semiFutureWithinTimedOut) { |
86 | Promise<int> p; |
87 | Baton<> done; |
88 | p.setInterruptHandler([&](const exception_wrapper& /* e */) { done.post(); }); |
89 | p.getSemiFuture().within(std::chrono::milliseconds(1)); |
90 | // Give it 100ms to time out and call the interrupt handler |
91 | EXPECT_TRUE(done.try_wait_for(std::chrono::milliseconds(100))); |
92 | } |
93 | |