Control the number of threads in TestExecutor
authorYedidya Feldblum <yfeldblum@fb.com>
Fri, 5 May 2017 18:52:53 +0000 (11:52 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Fri, 5 May 2017 19:05:17 +0000 (12:05 -0700)
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

folly/futures/test/RetryingTest.cpp
folly/futures/test/TestExecutor.cpp
folly/futures/test/TestExecutor.h
folly/futures/test/TestExecutorTest.cpp

index 2de5c8a441a4a6884181eb4699ca60717381b24a..43a15ea2221754cdaec099821e8e061600fb5bab 100644 (file)
@@ -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());
index 16f8d26dfffe1a147ce2d3d89da84449c9b12b17..fc57e6de6462ce1928fccfb559f121ffaa1d0366 100644 (file)
@@ -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();
 }
 
index c09713bef7fadd31332767c1baed6bc68b011c3e..f022bc036c6a985f44847e03a79daace396a9959 100644 (file)
@@ -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);
index 750e904bf82ee3af3b6ce1e7eb39ff96904fe170..5af5893bce2d9cdc7bd21f9f93d80795f117ddad 100644 (file)
@@ -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));