From bc5e9afd0d9332433c3356064352b54cfbf5aee5 Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Fri, 24 Feb 2017 15:01:53 +0200 Subject: [tests] Added unit tests for Thread::pause/resume --- test/util/thread.test.cpp | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) (limited to 'test/util/thread.test.cpp') diff --git a/test/util/thread.test.cpp b/test/util/thread.test.cpp index fc41fd4b78..972bddf383 100644 --- a/test/util/thread.test.cpp +++ b/test/util/thread.test.cpp @@ -3,6 +3,8 @@ #include +#include + using namespace mbgl::util; class TestObject { @@ -216,3 +218,79 @@ TEST(Thread, WorkRequestDeletionCancelsImmediately) { started.get_future().get(); request1.reset(); } + +TEST(Thread, DeletePausedThread) { + RunLoop loop; + + std::atomic_bool flag(false); + + auto thread = std::make_unique>(ThreadContext{"Test"}); + thread->pause(); + thread->invoke(&TestWorker::send, [&] { flag = true; }, [] {}); + + // Should not hang. + thread.reset(); + + // Should process the queue before destruction. + ASSERT_TRUE(flag); +} + +TEST(Thread, Pause) { + RunLoop loop; + + std::atomic_bool flag(false); + + Thread thread1({"Test1"}); + thread1.pause(); + + Thread thread2({"Test2"}); + + for (unsigned i = 0; i < 100; ++i) { + thread1.invoke(&TestWorker::send, [&] { flag = true; }, [] {}); + thread2.invoke(&TestWorker::send, [&] { ASSERT_FALSE(flag); }, [] {}); + } + + // Queue a message at the end of thread2 queue. + thread2.invoke(&TestWorker::send, [&] { loop.stop(); }, [] {}); + loop.run(); +} + +TEST(Thread, Resume) { + RunLoop loop; + + std::atomic_bool flag(false); + + Thread thread({"Test"}); + thread.pause(); + + for (unsigned i = 0; i < 100; ++i) { + thread.invoke(&TestWorker::send, [&] { flag = true; }, [] {}); + } + + // Thread messages are ondered, when we resume, this is going + // to me the last thing to run on the message queue. + thread.invoke(&TestWorker::send, [&] { loop.stop(); }, [] {}); + + // This test will be flaky if the thread doesn't get paused. + ASSERT_FALSE(flag); + + thread.resume(); + loop.run(); + + ASSERT_TRUE(flag); +} + +TEST(Thread, PauseResume) { + RunLoop loop; + + Thread thread({"Test"}); + + // Test if multiple pause/resume work. + for (unsigned i = 0; i < 100; ++i) { + thread.pause(); + thread.resume(); + } + + thread.invoke(&TestWorker::send, [&] { loop.stop(); }, [] {}); + loop.run(); +} -- cgit v1.2.1