Test 0, submit task to a non-started, interrupted, or stopped pool
| 79 | |
| 80 | // Test 0, submit task to a non-started, interrupted, or stopped pool |
| 81 | BOOST_AUTO_TEST_CASE(submit_fails_with_correct_error) |
| 82 | { |
| 83 | ThreadPool threadPool(POOL_NAME); |
| 84 | const auto fn_empty = [&] {}; |
| 85 | |
| 86 | // Never started: Inactive |
| 87 | auto res = threadPool.Submit(fn_empty); |
| 88 | BOOST_CHECK(!res); |
| 89 | BOOST_CHECK_EQUAL(SubmitErrorString(res.error()), "No active workers"); |
| 90 | |
| 91 | // Interrupted (workers still alive): Interrupted, and Start() must be rejected too |
| 92 | std::counting_semaphore<> blocker(0); |
| 93 | threadPool.Start(NUM_WORKERS_DEFAULT); |
| 94 | const auto blocking_tasks = BlockWorkers(threadPool, blocker, NUM_WORKERS_DEFAULT); |
| 95 | threadPool.Interrupt(); |
| 96 | res = threadPool.Submit(fn_empty); |
| 97 | BOOST_CHECK(!res); |
| 98 | BOOST_CHECK_EQUAL(SubmitErrorString(res.error()), "Interrupted"); |
| 99 | BOOST_CHECK_EXCEPTION(threadPool.Start(NUM_WORKERS_DEFAULT), std::runtime_error, HasReason("Thread pool has been interrupted or is stopping")); |
| 100 | blocker.release(NUM_WORKERS_DEFAULT); |
| 101 | WAIT_FOR(blocking_tasks); |
| 102 | |
| 103 | // Interrupted then stopped: Inactive |
| 104 | threadPool.Stop(); |
| 105 | res = threadPool.Submit(fn_empty); |
| 106 | BOOST_CHECK(!res); |
| 107 | BOOST_CHECK_EQUAL(SubmitErrorString(res.error()), "No active workers"); |
| 108 | |
| 109 | // Started then stopped: Inactive |
| 110 | threadPool.Start(NUM_WORKERS_DEFAULT); |
| 111 | threadPool.Stop(); |
| 112 | res = threadPool.Submit(fn_empty); |
| 113 | BOOST_CHECK(!res); |
| 114 | BOOST_CHECK_EQUAL(SubmitErrorString(res.error()), "No active workers"); |
| 115 | |
| 116 | std::vector<std::function<void()>> tasks; |
| 117 | const auto range_res{threadPool.Submit(std::move(tasks))}; |
| 118 | BOOST_CHECK(!range_res); |
| 119 | BOOST_CHECK_EQUAL(SubmitErrorString(range_res.error()), "No active workers"); |
| 120 | } |
| 121 | |
| 122 | // Test 1, submit tasks and verify completion |
| 123 | BOOST_AUTO_TEST_CASE(submit_tasks_complete_successfully) |
nothing calls this directly
no test coverage detected