2017-08-30 05:30:27 -04:00
|
|
|
#include "catch.hpp"
|
|
|
|
|
2018-04-19 15:03:25 -04:00
|
|
|
#include <osmium/thread/pool.hpp>
|
|
|
|
|
2017-08-30 05:30:27 -04:00
|
|
|
#include <stdexcept>
|
|
|
|
|
|
|
|
struct test_job_with_result {
|
|
|
|
int operator()() const {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test_job_throw {
|
2020-11-17 16:59:06 -05:00
|
|
|
[[noreturn]] void operator()() const {
|
2017-08-30 05:30:27 -04:00
|
|
|
throw std::runtime_error{"exception in pool thread"};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("number of threads in pool") {
|
|
|
|
|
|
|
|
// hardcoded setting
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 1, 0, 2) == 1);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 4, 0, 2) == 4);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 4, 0, 4) == 4);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(16, 0, 4) == 16);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(16, 0, 16) == 16);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 8, 4, 2) == 8);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 8, 16, 2) == 8);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(-2, 16, 2) == 1);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(-2, 16, 8) == 6);
|
|
|
|
|
|
|
|
// user decides through OSMIUM_POOL_THREADS env variable
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 0, 2) == 1);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, -2, 4) == 2);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, -1, 8) == 7);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 0, 16) == 14);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 1, 16) == 1);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 2, 16) == 2);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 4, 16) == 4);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size( 0, 8, 16) == 8);
|
|
|
|
|
|
|
|
// outliers
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(-100, 0, 16) == 1);
|
|
|
|
REQUIRE(osmium::thread::detail::get_pool_size(1000, 0, 16) == 32);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("if zero number of threads requested, threads configured") {
|
|
|
|
osmium::thread::Pool pool{0};
|
|
|
|
REQUIRE(pool.num_threads() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("if any negative number of threads requested, threads configured") {
|
|
|
|
osmium::thread::Pool pool{-1};
|
|
|
|
REQUIRE(pool.num_threads() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("if outlier negative number of threads requested, threads configured") {
|
|
|
|
osmium::thread::Pool pool{-100};
|
|
|
|
REQUIRE(pool.num_threads() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("if outlier positive number of threads requested, threads configured") {
|
|
|
|
osmium::thread::Pool pool{1000};
|
|
|
|
REQUIRE(pool.num_threads() > 0);
|
|
|
|
}
|
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can get access to default thread pool") {
|
2017-08-30 05:30:27 -04:00
|
|
|
auto& pool = osmium::thread::Pool::default_instance();
|
2020-11-17 16:59:06 -05:00
|
|
|
REQUIRE(pool.queue_empty());
|
|
|
|
}
|
2017-08-30 05:30:27 -04:00
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can send job to default thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::default_instance();
|
|
|
|
auto future = pool.submit(test_job_with_result{});
|
|
|
|
REQUIRE(future.get() == 42);
|
2017-08-30 05:30:27 -04:00
|
|
|
}
|
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can throw from job in default thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::default_instance();
|
|
|
|
auto future = pool.submit(test_job_throw{});
|
2022-08-16 13:26:21 -04:00
|
|
|
REQUIRE_THROWS_AS(future.get(), std::runtime_error);
|
2020-11-17 16:59:06 -05:00
|
|
|
}
|
2017-08-30 05:30:27 -04:00
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can get access to user provided thread pool") {
|
2017-08-30 05:30:27 -04:00
|
|
|
osmium::thread::Pool pool{7};
|
2020-11-17 16:59:06 -05:00
|
|
|
REQUIRE(pool.queue_empty());
|
|
|
|
}
|
2017-08-30 05:30:27 -04:00
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can access user-provided number of threads from pool") {
|
|
|
|
osmium::thread::Pool pool{7};
|
|
|
|
REQUIRE(pool.num_threads() == 7);
|
|
|
|
}
|
2017-08-30 05:30:27 -04:00
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can send job to user provided thread pool") {
|
|
|
|
osmium::thread::Pool pool{7};
|
|
|
|
auto future = pool.submit(test_job_with_result{});
|
|
|
|
REQUIRE(future.get() == 42);
|
|
|
|
}
|
2017-08-30 05:30:27 -04:00
|
|
|
|
2020-11-17 16:59:06 -05:00
|
|
|
TEST_CASE("can throw from job in user provided thread pool") {
|
|
|
|
osmium::thread::Pool pool{7};
|
|
|
|
auto future = pool.submit(test_job_throw{});
|
2022-08-16 13:26:21 -04:00
|
|
|
REQUIRE_THROWS_AS(future.get(), std::runtime_error);
|
2017-08-30 05:30:27 -04:00
|
|
|
}
|
|
|
|
|