summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2017-02-24 15:01:53 +0200
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2017-02-25 14:03:38 +0200
commitbc5e9afd0d9332433c3356064352b54cfbf5aee5 (patch)
tree825cc9df51ced3ff8b0dd5663308c4b992ca12c1
parent86300ccedfc5ccdace551ce76f9185513d736127 (diff)
downloadqtlocation-mapboxgl-bc5e9afd0d9332433c3356064352b54cfbf5aee5.tar.gz
[tests] Added unit tests for Thread::pause/resume
-rw-r--r--test/util/thread.test.cpp78
1 files changed, 78 insertions, 0 deletions
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 <mbgl/test/util.hpp>
+#include <atomic>
+
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<Thread<TestWorker>>(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<TestWorker> thread1({"Test1"});
+ thread1.pause();
+
+ Thread<TestWorker> 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<TestWorker> 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<TestWorker> 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();
+}