move wangle/concurrent to folly/executors
[folly.git] / folly / executors / test / ThreadedExecutorTest.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/ThreadedExecutor.h>
18
19 #include <gtest/gtest.h>
20
21 #include <folly/Conv.h>
22 #include <folly/futures/Future.h>
23 #include <folly/gen/Base.h>
24
25 namespace {
26
27 class ThreadedExecutorTest : public testing::Test {};
28 }
29
30 TEST_F(ThreadedExecutorTest, example) {
31   folly::ThreadedExecutor x;
32   auto ret = folly::via(&x)
33                  .then([&] { return 42; })
34                  .then([&](int n) { return folly::to<std::string>(n); })
35                  .get();
36
37   EXPECT_EQ("42", ret);
38 }
39
40 TEST_F(ThreadedExecutorTest, dtor_waits) {
41   constexpr auto kDelay = std::chrono::milliseconds(100);
42   auto x = std::make_unique<folly::ThreadedExecutor>();
43   auto fut = folly::via(&*x, [&] { /* sleep override */
44                                    std::this_thread::sleep_for(kDelay);
45   });
46   x = nullptr;
47
48   EXPECT_TRUE(fut.isReady());
49 }
50
51 TEST_F(ThreadedExecutorTest, many) {
52   constexpr auto kNumTasks = 1024;
53   folly::ThreadedExecutor x;
54   auto rets =
55       folly::collect(
56           folly::gen::range<size_t>(0, kNumTasks) |
57           folly::gen::map([&](size_t i) {
58             return folly::via(&x).then([=] { return i; }).then([](size_t k) {
59               return folly::to<std::string>(k);
60             });
61           }) |
62           folly::gen::as<std::vector>())
63           .get();
64
65   EXPECT_EQ("42", rets[42]);
66 }
67
68 TEST_F(ThreadedExecutorTest, many_sleeping_constant_time) {
69   constexpr auto kNumTasks = 256;
70   constexpr auto kDelay = std::chrono::milliseconds(100);
71   folly::ThreadedExecutor x;
72   auto rets =
73       folly::collect(
74           folly::gen::range<size_t>(0, kNumTasks) |
75           folly::gen::map([&](size_t i) {
76             return folly::via(&x)
77                 .then([=] {
78                   /* sleep override */ std::this_thread::sleep_for(kDelay);
79                 })
80                 .then([=] { return i; })
81                 .then([](size_t k) { return folly::to<std::string>(k); });
82           }) |
83           folly::gen::as<std::vector>())
84           .get();
85
86   EXPECT_EQ("42", rets[42]);
87 }
88
89 TEST_F(ThreadedExecutorTest, many_sleeping_decreasing_time) {
90   constexpr auto kNumTasks = 256;
91   constexpr auto kDelay = std::chrono::milliseconds(100);
92   folly::ThreadedExecutor x;
93   auto rets =
94       folly::collect(
95           folly::gen::range<size_t>(0, kNumTasks) |
96           folly::gen::map([&](size_t i) {
97             return folly::via(&x)
98                 .then([=] {
99                   auto delay = kDelay * (kNumTasks - i) / kNumTasks;
100                   /* sleep override */ std::this_thread::sleep_for(delay);
101                 })
102                 .then([=] { return i; })
103                 .then([](size_t k) { return folly::to<std::string>(k); });
104           }) |
105           folly::gen::as<std::vector>())
106           .get();
107
108   EXPECT_EQ("42", rets[42]);
109 }