1 | /* |
2 | * Copyright 2017-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/executors/Async.h> |
18 | #include <folly/executors/ManualExecutor.h> |
19 | #include <folly/portability/GTest.h> |
20 | |
21 | #include <memory> |
22 | |
23 | using namespace folly; |
24 | |
25 | TEST(AsyncFunc, manual_executor) { |
26 | auto x = std::make_shared<ManualExecutor>(); |
27 | auto oldX = getCPUExecutor(); |
28 | setCPUExecutor(x); |
29 | auto f = async([] { return 42; }); |
30 | EXPECT_FALSE(f.isReady()); |
31 | x->run(); |
32 | EXPECT_EQ(42, f.value()); |
33 | setCPUExecutor(oldX); |
34 | } |
35 | |
36 | TEST(AsyncFunc, value_lambda) { |
37 | auto lambda = [] { return 42; }; |
38 | auto future = async(lambda); |
39 | EXPECT_EQ(42, std::move(future).get()); |
40 | } |
41 | |
42 | TEST(AsyncFunc, void_lambda) { |
43 | auto lambda = [] { /*do something*/ return; }; |
44 | auto future = async(lambda); |
45 | // Futures with a void returning function, return Unit type |
46 | EXPECT_EQ(typeid(Unit), typeid(std::move(future).get())); |
47 | } |
48 | |
49 | TEST(AsyncFunc, moveonly_lambda) { |
50 | auto lambda = [] { return std::make_unique<int>(42); }; |
51 | auto future = async(lambda); |
52 | EXPECT_EQ(42, *std::move(future).get()); |
53 | } |
54 | |