2 * Copyright 2016-present 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/executors/InlineExecutor.h>
18 #include <folly/futures/Future.h>
19 #include <folly/portability/GTest.h>
21 using namespace folly;
23 TEST(SelfDestruct, then) {
24 auto* p = new Promise<int>();
25 auto future = p->getFuture().then([p](int x) {
30 EXPECT_EQ(124, future.get());
33 TEST(SelfDestruct, ensure) {
34 auto* p = new Promise<int>();
35 auto future = p->getFuture().ensure([p] { delete p; });
37 EXPECT_EQ(123, future.get());
40 class ThrowingExecutorError : public std::runtime_error {
42 using std::runtime_error::runtime_error;
45 class ThrowingExecutor : public folly::Executor {
47 void add(folly::Func) override {
48 throw ThrowingExecutorError("ThrowingExecutor::add");
52 TEST(SelfDestruct, throwingExecutor) {
53 ThrowingExecutor executor;
54 auto* p = new Promise<int>();
56 p->getFuture().via(&executor).onError([p](ThrowingExecutorError const&) {
61 EXPECT_EQ(456, future.get());
64 TEST(SelfDestruct, throwingInlineExecutor) {
65 folly::InlineExecutor executor;
67 auto* p = new Promise<int>();
68 auto future = p->getFuture()
72 throw ThrowingExecutorError("callback throws");
74 .onError([](ThrowingExecutorError const&) { return 456; });
76 EXPECT_EQ(456, future.get());