First Commit
This commit is contained in:
commit
8a6c7106e3
1312 files changed
+425396
No files matched your search
+106
@@ -0,0 +1,106 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
#include <osmium/thread/pool.hpp>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
struct test_job_with_result {
|
||||
int operator()() const {
|
||||
return 42;
|
||||
}
|
||||
};
|
||||
|
||||
struct test_job_throw {
|
||||
[[noreturn]] void operator()() const {
|
||||
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") {
|
||||
const osmium::thread::Pool pool{0};
|
||||
REQUIRE(pool.num_threads() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("if any negative number of threads requested, threads configured") {
|
||||
const osmium::thread::Pool pool{-1};
|
||||
REQUIRE(pool.num_threads() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("if outlier negative number of threads requested, threads configured") {
|
||||
const osmium::thread::Pool pool{-100};
|
||||
REQUIRE(pool.num_threads() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("if outlier positive number of threads requested, threads configured") {
|
||||
const osmium::thread::Pool pool{1000};
|
||||
REQUIRE(pool.num_threads() > 0);
|
||||
}
|
||||
|
||||
TEST_CASE("can get access to default thread pool") {
|
||||
auto& pool = osmium::thread::Pool::default_instance();
|
||||
REQUIRE(pool.queue_empty());
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("can throw from job in default thread pool") {
|
||||
auto& pool = osmium::thread::Pool::default_instance();
|
||||
auto future = pool.submit(test_job_throw{});
|
||||
REQUIRE_THROWS_AS(future.get(), std::runtime_error);
|
||||
}
|
||||
|
||||
TEST_CASE("can get access to user provided thread pool") {
|
||||
const osmium::thread::Pool pool{7};
|
||||
REQUIRE(pool.queue_empty());
|
||||
}
|
||||
|
||||
TEST_CASE("can access user-provided number of threads from pool") {
|
||||
const osmium::thread::Pool pool{7};
|
||||
REQUIRE(pool.num_threads() == 7);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
TEST_CASE("can throw from job in user provided thread pool") {
|
||||
osmium::thread::Pool pool{7};
|
||||
auto future = pool.submit(test_job_throw{});
|
||||
REQUIRE_THROWS_AS(future.get(), std::runtime_error);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
#include <osmium/thread/queue.hpp>
|
||||
|
||||
TEST_CASE("Basic use of thread-safe queue") {
|
||||
osmium::thread::Queue<int> queue;
|
||||
REQUIRE(queue.empty());
|
||||
queue.push(22);
|
||||
REQUIRE_FALSE(queue.empty());
|
||||
REQUIRE(queue.size() == 1);
|
||||
int value = 0;
|
||||
queue.wait_and_pop(value);
|
||||
REQUIRE(value == 22);
|
||||
REQUIRE(queue.empty());
|
||||
}
|
||||
|
||||
TEST_CASE("Queue can have max elements and can be named") {
|
||||
const osmium::thread::Queue<int> queue{100, "Queue of max size 100"};
|
||||
}
|
||||
|
||||
TEST_CASE("When queue is shut down, nothing goes in or out") {
|
||||
osmium::thread::Queue<std::string> queue;
|
||||
REQUIRE(queue.in_use());
|
||||
REQUIRE(queue.empty());
|
||||
queue.push("foo");
|
||||
queue.push("bar");
|
||||
queue.push("baz");
|
||||
REQUIRE(queue.size() == 3);
|
||||
|
||||
std::string value;
|
||||
|
||||
queue.wait_and_pop(value);
|
||||
REQUIRE(value == "foo");
|
||||
REQUIRE(queue.size() == 2);
|
||||
REQUIRE(queue.in_use());
|
||||
queue.shutdown();
|
||||
REQUIRE_FALSE(queue.in_use());
|
||||
REQUIRE(queue.empty());
|
||||
queue.push("lost");
|
||||
REQUIRE(queue.empty());
|
||||
|
||||
value.clear();
|
||||
queue.try_pop(value);
|
||||
REQUIRE(value.empty());
|
||||
queue.wait_and_pop(value);
|
||||
REQUIRE(value.empty());
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
#include "catch.hpp"
|
||||
|
||||
#include <osmium/thread/util.hpp>
|
||||
|
||||
#include <future>
|
||||
#include <stdexcept>
|
||||
#include <type_traits>
|
||||
|
||||
TEST_CASE("check_for_exception") {
|
||||
std::promise<int> p;
|
||||
auto f = p.get_future();
|
||||
|
||||
SECTION("not ready") {
|
||||
osmium::thread::check_for_exception(f);
|
||||
}
|
||||
SECTION("ready") {
|
||||
p.set_value(3);
|
||||
osmium::thread::check_for_exception(f);
|
||||
}
|
||||
SECTION("no shared state") {
|
||||
p.set_value(3);
|
||||
REQUIRE(f.get() == 3);
|
||||
osmium::thread::check_for_exception(f);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("check_for_exception with exception") {
|
||||
std::promise<int> p;
|
||||
auto f = p.get_future();
|
||||
|
||||
try {
|
||||
throw std::runtime_error{"TEST"};
|
||||
} catch (...) {
|
||||
p.set_exception(std::current_exception());
|
||||
}
|
||||
|
||||
REQUIRE_THROWS_AS(osmium::thread::check_for_exception(f), std::runtime_error);
|
||||
}
|
||||
|
||||
static_assert(std::is_nothrow_move_constructible<osmium::thread::thread_handler>::value, "thread_handler must have noexcept move constructor");
|
||||
|
||||
TEST_CASE("empty thread_handler") {
|
||||
const osmium::thread::thread_handler th;
|
||||
}
|
||||
|
||||
int foo; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
void test_func(int value) {
|
||||
foo = value;
|
||||
}
|
||||
|
||||
TEST_CASE("valid thread_handler") {
|
||||
foo = 22;
|
||||
test_func(17);
|
||||
REQUIRE(foo == 17);
|
||||
{
|
||||
const osmium::thread::thread_handler th{test_func, 5};
|
||||
}
|
||||
REQUIRE(foo == 5);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user