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
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> {
func));
}
+ // 40 * 10,000 * 16,000B > 1GB; we should avoid OOM
+
for (auto& f : futures) {
f.wait();
EXPECT_TRUE(f.hasValue());
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;
}
}
-uint32_t TestExecutor::numThreads() const {
+size_t TestExecutor::numThreads() const {
return workers_.size();
}
*/
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);
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));