diff options
Diffstat (limited to 'base/base_tests/worker_thread_tests.cpp')
-rw-r--r-- | base/base_tests/worker_thread_tests.cpp | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/base/base_tests/worker_thread_tests.cpp b/base/base_tests/worker_thread_tests.cpp new file mode 100644 index 0000000000..c2c825f319 --- /dev/null +++ b/base/base_tests/worker_thread_tests.cpp @@ -0,0 +1,70 @@ +#include "testing/testing.hpp" + +#include "base/worker_thread.hpp" + +#include <condition_variable> +#include <mutex> + +using namespace base; +using namespace std; + +namespace +{ +UNIT_TEST(WorkerThread_Smoke) +{ + { + WorkerThread thread; + } + + { + WorkerThread thread; + thread.Shutdown(WorkerThread::Exit::SkipPending); + } + + { + WorkerThread thread; + thread.Shutdown(WorkerThread::Exit::ExecPending); + } +} + +UNIT_TEST(WorkerThread_SimpleSync) +{ + int value = 0; + + mutex mu; + condition_variable cv; + bool done = false; + + WorkerThread thread; + thread.Push([&value]() { ++value; }); + thread.Push([&value]() { value *= 2; }); + thread.Push([&value]() { value = value * value * value; }); + thread.Push([&]() { + lock_guard<mutex> lk(mu); + done = true; + cv.notify_one(); + }); + + { + unique_lock<mutex> lk(mu); + cv.wait(lk, [&done]() { return done; }); + } + + TEST_EQUAL(value, 8, ()); +} + +UNIT_TEST(WorkerThread_SimpleFlush) +{ + int value = 0; + { + WorkerThread thread; + thread.Push([&value]() { ++value; }); + thread.Push([&value]() { + for (int i = 0; i < 10; ++i) + value *= 2; + }); + thread.Shutdown(WorkerThread::Exit::ExecPending); + } + TEST_EQUAL(value, 1024, ()); +} +} // namespace |