2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/futures/Future.h>
18 #include <folly/futures/Promise.h>
19 #include <folly/Baton.h>
20 #include <folly/portability/GTest.h>
22 using namespace folly;
24 TEST(Interrupt, raise) {
25 std::runtime_error eggs("eggs");
27 p.setInterruptHandler([&](const exception_wrapper& e) {
28 EXPECT_THROW(e.throwException(), decltype(eggs));
30 p.getFuture().raise(eggs);
33 TEST(Interrupt, cancel) {
35 p.setInterruptHandler([&](const exception_wrapper& e) {
36 EXPECT_THROW(e.throwException(), FutureCancellation);
38 p.getFuture().cancel();
41 TEST(Interrupt, handleThenInterrupt) {
44 p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
45 p.getFuture().cancel();
49 TEST(Interrupt, interruptThenHandle) {
52 p.getFuture().cancel();
53 p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
57 TEST(Interrupt, interruptAfterFulfilNoop) {
60 p.setInterruptHandler([&](const exception_wrapper& /* e */) { flag = true; });
62 p.getFuture().cancel();
66 TEST(Interrupt, secondInterruptNoop) {
69 p.setInterruptHandler([&](const exception_wrapper& /* e */) { count++; });
70 auto f = p.getFuture();
76 TEST(Interrupt, withinTimedOut) {
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 auto t = std::chrono::steady_clock::now() + std::chrono::milliseconds(100);
83 EXPECT_TRUE(done.timed_wait(t));