proxygen
AsyncTest.cpp
Go to the documentation of this file.
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>
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();
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 }
Definition: InvokeTest.cpp:58
auto f
auto async(F &&fn)
Definition: Async.h:24
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
std::shared_ptr< Executor > getCPUExecutor()
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
void setCPUExecutor(std::weak_ptr< Executor > executor)
TEST(SequencedExecutor, CPUThreadPoolExecutor)