2019-06-29 23:23:53 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <Common/ThreadPool.h>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
|
2019-12-15 06:34:43 +00:00
|
|
|
static bool check()
|
2019-06-29 23:23:53 +00:00
|
|
|
{
|
|
|
|
ThreadPool pool(10);
|
|
|
|
|
2020-08-16 08:07:36 +00:00
|
|
|
/// The throwing thread.
|
2019-10-17 14:41:27 +00:00
|
|
|
pool.scheduleOrThrowOnError([] { throw std::runtime_error("Hello, world!"); });
|
2019-06-29 23:23:53 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2020-08-16 08:07:36 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
/// An exception from the throwing thread will be rethrown from this method
|
|
|
|
/// as soon as the throwing thread executed.
|
|
|
|
|
|
|
|
/// This innocent thread may or may not be executed, the following possibilities exist:
|
2020-08-16 12:09:41 +00:00
|
|
|
/// 1. The throwing thread has already thrown exception and the attempt to schedule the innocent thread will rethrow it.
|
2020-08-16 08:07:36 +00:00
|
|
|
/// 2. The throwing thread has not executed, the innocent thread will be scheduled and executed.
|
|
|
|
/// 3. The throwing thread has not executed, the innocent thread will be scheduled but before it will be executed,
|
|
|
|
/// the throwing thread will be executed and throw exception and it will prevent starting of execution of the innocent thread
|
|
|
|
/// the method will return and the exception will be rethrown only on call to "wait" or on next call on next loop iteration as (1).
|
|
|
|
pool.scheduleOrThrowOnError([]{});
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
}
|
2019-06-29 23:23:53 +00:00
|
|
|
}
|
|
|
|
catch (const std::runtime_error &)
|
|
|
|
{
|
2020-08-16 08:07:36 +00:00
|
|
|
pool.wait();
|
2019-06-29 23:23:53 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-16 08:07:36 +00:00
|
|
|
__builtin_unreachable();
|
2019-06-29 23:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(ThreadPool, ExceptionFromSchedule)
|
|
|
|
{
|
|
|
|
EXPECT_TRUE(check());
|
|
|
|
}
|