diff options
author | Jack Lloyd <[email protected]> | 2019-06-05 10:23:52 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2019-06-05 10:23:52 -0400 |
commit | 5bc782f9dabbef1dd2f35cf29fd874f148b4d451 (patch) | |
tree | 3ba31f6e0ba3801686a0bee3ffe7fcc3edf4f040 /src/tests | |
parent | a3f402aba66c4fa07fef7d71b457f1a3c0709be7 (diff) |
Add a test that Theaad_Pool is tolerant of exceptions during tasks
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_thread_utils.cpp | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/src/tests/test_thread_utils.cpp b/src/tests/test_thread_utils.cpp index 2374df860..f79877cf6 100644 --- a/src/tests/test_thread_utils.cpp +++ b/src/tests/test_thread_utils.cpp @@ -25,21 +25,39 @@ Test::Result thread_pool() // Using lots of threads since here the works spend most of the time sleeping Botan::Thread_Pool pool(16); - auto sleep_and_return = [](size_t x) -> size_t { + auto sleep_or_throw = [](size_t x) -> size_t { std::this_thread::sleep_for(std::chrono::milliseconds((x*97)%127)); + + if(x % 2 == 0) + throw x; return x; }; std::vector<std::future<size_t>> futures; for(size_t i = 0; i != 100; ++i) { - auto fut = pool.run(sleep_and_return, i); + auto fut = pool.run(sleep_or_throw, i); futures.push_back(std::move(fut)); } for(size_t i = 0; i != futures.size(); ++i) { - result.test_eq("Expected return value", futures[i].get(), i); + if(i % 2 == 0) + { + try + { + futures[i].get(); + result.test_failure("Expected future to throw"); + } + catch(size_t x) + { + result.test_eq("Expected thrown value", x, i); + } + } + else + { + result.test_eq("Expected return value", futures[i].get(), i); + } } pool.shutdown(); |