4a7f0cf007979b0436eff22f5ba15ce0166cf4db
[folly.git] / folly / executors / test / AsyncTest.cpp
1 /*
2  * Copyright 2017 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 using namespace folly;
22
23 TEST(AsyncFunc, manual_executor) {
24   auto x = std::make_shared<ManualExecutor>();
25   auto oldX = getCPUExecutor();
26   setCPUExecutor(x);
27   auto f = async([] { return 42; });
28   EXPECT_FALSE(f.isReady());
29   x->run();
30   EXPECT_EQ(42, f.value());
31   setCPUExecutor(oldX);
32 }
33
34 TEST(AsyncFunc, value_lambda) {
35   auto lambda = [] { return 42; };
36   auto future = async(lambda);
37   EXPECT_EQ(42, future.get());
38 }
39
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()));
45 }
46
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());
51 }