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 <gtest/gtest.h>
19 #include <folly/futures/Future.h>
20 #include <folly/futures/InlineExecutor.h>
22 using namespace folly;
24 TEST(SelfDestruct, then) {
25 auto* p = new Promise<int>();
26 auto future = p->getFuture().then([p](int x) {
31 EXPECT_EQ(124, future.get());
34 TEST(SelfDestruct, ensure) {
35 auto* p = new Promise<int>();
36 auto future = p->getFuture().ensure([p] { delete p; });
38 EXPECT_EQ(123, future.get());
41 class ThrowingExecutorError : public std::runtime_error {
43 using std::runtime_error::runtime_error;
46 class ThrowingExecutor : public folly::Executor {
48 void add(folly::Func) override {
49 throw ThrowingExecutorError("ThrowingExecutor::add");
53 TEST(SelfDestruct, throwingExecutor) {
54 ThrowingExecutor executor;
55 auto* p = new Promise<int>();
57 p->getFuture().via(&executor).onError([p](ThrowingExecutorError const&) {
62 EXPECT_EQ(456, future.get());
65 TEST(SelfDestruct, throwingInlineExecutor) {
66 folly::InlineExecutor executor;
68 auto* p = new Promise<int>();
69 auto future = p->getFuture()
73 throw ThrowingExecutorError("callback throws");
75 .onError([](ThrowingExecutorError const&) { return 456; });
77 EXPECT_EQ(456, future.get());