diff --git a/daggy/include/daggy/ThreadPool.hpp b/daggy/include/daggy/ThreadPool.hpp index ebf5bc7..a6bd1fc 100644 --- a/daggy/include/daggy/ThreadPool.hpp +++ b/daggy/include/daggy/ThreadPool.hpp @@ -22,7 +22,7 @@ namespace daggy { std::future addTask(AsyncTask fn); private: - using QueuedAsyncTask = std::shared_ptr; + using QueuedAsyncTask = std::shared_ptr>; std::atomic shutdown_; std::mutex guard_; diff --git a/daggy/src/ThreadPool.cpp b/daggy/src/ThreadPool.cpp index 5121be2..2b740a0 100644 --- a/daggy/src/ThreadPool.cpp +++ b/daggy/src/ThreadPool.cpp @@ -13,7 +13,9 @@ ThreadPool::ThreadPool(size_t nWorkers) { std::unique_lock lk(guard_); cv_.wait(lk, []{ return true; }); if (shutdown_) return; - tsk.swap(taskQueue_.front()); + if (taskQueue_.empty()) continue; + + tsk = taskQueue_.front(); taskQueue_.pop_front(); } @@ -41,7 +43,7 @@ std::future ThreadPool::addTask(std::function fn) { std::future result = task->get_future(); { std::unique_lock lk(guard_); - taskQueue_.emplace_back(std::move(task)); + taskQueue_.push_back(task); } cv_.notify_one(); return result; diff --git a/tests/unit_executor_forkingexecutor.cpp b/tests/unit_executor_forkingexecutor.cpp index 4eb6735..86a0fa0 100644 --- a/tests/unit_executor_forkingexecutor.cpp +++ b/tests/unit_executor_forkingexecutor.cpp @@ -29,13 +29,22 @@ TEST_CASE("Basic Execution", "[forking_executor]") { } SECTION("Large Output") { - const std::string BIG_FILE{"/usr/share/dict/linux.words"}; - std::vector cmd{"/usr/bin/cat", BIG_FILE}; + const std::vector BIG_FILES{ + "/usr/share/dict/linux.words" + , "/usr/share/dict/cracklib-small" + , "/etc/ssh/moduli" + }; - auto rec = ex.runCommand(cmd); + for (const auto & bigFile : BIG_FILES) { + if (! std::filesystem::exists(bigFile)) continue; - REQUIRE(rec.rc == 0); - REQUIRE(rec.output.size() == std::filesystem::file_size(BIG_FILE)); - REQUIRE(rec.error.empty()); + std::vector cmd{"/usr/bin/cat", bigFile}; + + auto rec = ex.runCommand(cmd); + + REQUIRE(rec.rc == 0); + REQUIRE(rec.output.size() == std::filesystem::file_size(bigFile)); + REQUIRE(rec.error.empty()); + } } } diff --git a/tests/unit_threadpool.cpp b/tests/unit_threadpool.cpp index 67bfe2a..7163009 100644 --- a/tests/unit_threadpool.cpp +++ b/tests/unit_threadpool.cpp @@ -12,13 +12,20 @@ TEST_CASE("Threadpool Construction", "[threadpool]") { ThreadPool tp(10); std::vector> res; - for (size_t i = 0; i < 100; ++i) { - res.push_back(tp.addTask([&cnt]() -> void { cnt++; return; })); + + SECTION("Simple runs") { + for (size_t i = 0; i < 100; ++i) + res.push_back(tp.addTask([&cnt]() { cnt++; return; })); + for (auto & r : res) r.get(); + REQUIRE(cnt == 100); } - for (auto & r : res) { - r.get(); + SECTION("Slow runs") { + using namespace std::chrono_literals; + for (size_t i = 0; i < 100; ++i) + res.push_back(tp.addTask([&cnt]() { std::this_thread::sleep_for(20ms); cnt++; return; })); + for (auto & r : res) r.get(); + REQUIRE(cnt == 100); } - REQUIRE(cnt == 100); }