Commit 0eff88b3 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Control the number of threads in TestExecutor

Summary:
[Folly] Control the number of threads in `TestExecutor`.

Let the caller control the number of threads to use in each given case.

Reviewed By: spacedentist

Differential Revision: D4999699

fbshipit-source-id: 4acf68cf17fbca14f0779daf0268d54c5606e4a8
parent 3703b1b0
......@@ -151,7 +151,7 @@ TEST(RetryingTest, large_retries) {
PCHECK(setrlimit(RLIMIT_AS, &oldMemLimit) == 0);
};
TestExecutor executor;
TestExecutor executor(4);
// size of implicit promise is at least the size of the return.
using LargeReturn = array<uint64_t, 16000>;
auto func = [&executor](size_t retryNum) -> Future<LargeReturn> {
......@@ -172,6 +172,8 @@ TEST(RetryingTest, large_retries) {
func));
}
// 40 * 10,000 * 16,000B > 1GB; we should avoid OOM
for (auto& f : futures) {
f.wait();
EXPECT_TRUE(f.hasValue());
......
......@@ -20,9 +20,9 @@ using namespace std;
namespace folly {
TestExecutor::TestExecutor() {
const auto kWorkers = std::max(1U, thread::hardware_concurrency());
for (auto idx = 0U; idx < kWorkers; ++idx) {
TestExecutor::TestExecutor(size_t numThreads) {
const auto kWorkers = std::max(size_t(1), numThreads);
for (auto idx = 0u; idx < kWorkers; ++idx) {
workers_.emplace_back([this] {
while (true) {
Func work;
......@@ -57,7 +57,7 @@ void TestExecutor::add(Func f) {
}
}
uint32_t TestExecutor::numThreads() const {
size_t TestExecutor::numThreads() const {
return workers_.size();
}
......
......@@ -29,13 +29,13 @@ namespace folly {
*/
class TestExecutor : public Executor {
public:
TestExecutor();
explicit TestExecutor(size_t numThreads);
~TestExecutor() override;
void add(Func f) override;
uint32_t numThreads() const;
size_t numThreads() const;
private:
void addImpl(Func f);
......
......@@ -24,8 +24,9 @@ using namespace folly;
TEST(TestExecutor, parallel_run) {
mutex m;
set<thread::id> ids;
auto executor = std::make_unique<TestExecutor>();
auto executor = std::make_unique<TestExecutor>(4);
const auto numThreads = executor->numThreads();
EXPECT_EQ(4, numThreads);
for (auto idx = 0U; idx < numThreads * 10; ++idx) {
executor->add([&m, &ids]() mutable {
/* sleep override */ this_thread::sleep_for(milliseconds(100));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment