2 * Copyright 2017 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/Async.h>
18 #include <folly/futures/ManualExecutor.h>
19 #include <gtest/gtest.h>
21 using namespace folly;
23 TEST(AsyncFunc, manual_executor) {
24 auto x = std::make_shared<ManualExecutor>();
25 auto oldX = getCPUExecutor();
27 auto f = async([] { return 42; });
28 EXPECT_FALSE(f.isReady());
30 EXPECT_EQ(42, f.value());
34 TEST(AsyncFunc, value_lambda) {
35 auto lambda = [] { return 42; };
36 auto future = async(lambda);
37 EXPECT_EQ(42, future.get());
40 TEST(AsyncFunc, void_lambda) {
41 auto lambda = [] { /*do something*/ return; };
42 auto future = async(lambda);
43 // Futures with a void returning function, return Unit type
44 EXPECT_EQ(typeid(Unit), typeid(future.get()));
47 TEST(AsyncFunc, moveonly_lambda) {
48 auto lambda = [] { return std::unique_ptr<int>(new int(42)); };
49 auto future = async(lambda);
50 EXPECT_EQ(42, *future.get());