Fix flaky unit test

This commit is contained in:
Alexey Milovidov 2020-08-16 11:07:36 +03:00
parent 0d5da3e1f1
commit 1b929ec54a

View File

@ -1,6 +1,7 @@
#include <iostream>
#include <stdexcept>
#include <Common/ThreadPool.h>
#include <Poco/Event.h>
#include <gtest/gtest.h>
@ -9,21 +10,34 @@ static bool check()
{
ThreadPool pool(10);
/// The throwing thread.
pool.scheduleOrThrowOnError([] { throw std::runtime_error("Hello, world!"); });
try
{
for (size_t i = 0; i < 500; ++i)
pool.scheduleOrThrowOnError([] {}); /// An exception will be rethrown from this method.
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:
/// 1. The throwing thread has already throwed exception and the attempt to schedule the innocent thread will rethrow it.
/// 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));
}
}
catch (const std::runtime_error &)
{
pool.wait();
return true;
}
pool.wait();
return false;
__builtin_unreachable();
}