MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / BOOST_AUTO_TEST_CASE

Function BOOST_AUTO_TEST_CASE

src/test/threadpool_tests.cpp:81–120  ·  view source on GitHub ↗

Test 0, submit task to a non-started, interrupted, or stopped pool

Source from the content-addressed store, hash-verified

79
80// Test 0, submit task to a non-started, interrupted, or stopped pool
81BOOST_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
123BOOST_AUTO_TEST_CASE(submit_tasks_complete_successfully)

Callers

nothing calls this directly

Calls 15

SubmitErrorStringFunction · 0.85
BlockWorkersFunction · 0.85
HasReasonClass · 0.85
SubmitFunction · 0.85
UninterruptibleSleepFunction · 0.85
GetNumCoresFunction · 0.85
releaseMethod · 0.80
joinMethod · 0.80
errorMethod · 0.45
StartMethod · 0.45
InterruptMethod · 0.45
StopMethod · 0.45

Tested by

no test coverage detected