2015-03-04 06:50:42 -05:00
|
|
|
#include "catch.hpp"
|
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <thread>
|
|
|
|
|
|
|
|
#include <osmium/thread/pool.hpp>
|
|
|
|
|
|
|
|
static std::atomic<int> result;
|
|
|
|
|
|
|
|
struct test_job_ok {
|
|
|
|
void operator()() const {
|
|
|
|
result = 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test_job_with_result {
|
|
|
|
int operator()() const {
|
|
|
|
return 42;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct test_job_throw {
|
|
|
|
void operator()() const {
|
|
|
|
throw std::runtime_error("exception in pool thread");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("thread") {
|
|
|
|
|
|
|
|
SECTION("can get access to thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::instance();
|
|
|
|
REQUIRE(pool.queue_empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("can send job to thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::instance();
|
|
|
|
result = 0;
|
2015-04-13 09:44:38 -04:00
|
|
|
auto future = pool.submit(test_job_ok {});
|
2015-03-04 06:50:42 -05:00
|
|
|
|
|
|
|
// wait a bit for the other thread to get a chance to run
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
|
|
|
|
|
|
REQUIRE(result == 1);
|
|
|
|
|
|
|
|
future.get();
|
|
|
|
|
|
|
|
REQUIRE(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("can send job to thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::instance();
|
2015-04-13 09:44:38 -04:00
|
|
|
auto future = pool.submit(test_job_with_result {});
|
2015-03-04 06:50:42 -05:00
|
|
|
|
|
|
|
REQUIRE(future.get() == 42);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("can throw from job in thread pool") {
|
|
|
|
auto& pool = osmium::thread::Pool::instance();
|
|
|
|
result = 0;
|
|
|
|
|
2015-04-13 09:44:38 -04:00
|
|
|
auto future = pool.submit(test_job_throw {});
|
2015-03-04 06:50:42 -05:00
|
|
|
|
|
|
|
REQUIRE_THROWS_AS(future.get(), std::runtime_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|