summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2015-07-15 18:21:37 +0300
committerThiago Marcos P. Santos <thiago@mapbox.com>2015-07-16 19:38:38 +0300
commit7d4349e308a4ff2c90feda2d9ddd348f7a2621ae (patch)
tree4fca6ebae1f1db4cb8381f1b075dc3e4759166ad
parent7d500e9330eaf096fdbe89e33b1bf00526873e0e (diff)
downloadqtlocation-mapboxgl-7d4349e308a4ff2c90feda2d9ddd348f7a2621ae.tar.gz
Added unit tests for the WorkQueue
-rw-r--r--test/miscellaneous/work_queue.cpp63
-rw-r--r--test/test.gypi1
2 files changed, 64 insertions, 0 deletions
diff --git a/test/miscellaneous/work_queue.cpp b/test/miscellaneous/work_queue.cpp
new file mode 100644
index 0000000000..a5f616fe5b
--- /dev/null
+++ b/test/miscellaneous/work_queue.cpp
@@ -0,0 +1,63 @@
+#include "../fixtures/util.hpp"
+
+#include <mbgl/util/run_loop.hpp>
+#include <mbgl/util/thread.hpp>
+#include <mbgl/util/work_queue.hpp>
+
+#include <thread>
+
+using namespace mbgl::util;
+
+class TestThread {
+public:
+ TestThread(WorkQueue* queue_) : queue(queue_) {}
+
+ void send(std::function<void()>&& fn) {
+ EXPECT_TRUE(ThreadContext::currentlyOn(ThreadType::Map));
+
+ queue->push(std::move(fn));
+ }
+
+private:
+ WorkQueue* queue;
+};
+
+TEST(WorkQueue, push) {
+ RunLoop loop(uv_default_loop());
+
+ WorkQueue queue;
+ Thread<TestThread> thread({"Test", ThreadType::Map, ThreadPriority::Regular}, &queue);
+
+ uint8_t count = 0;
+
+ auto endTest = [&]() {
+ EXPECT_TRUE(ThreadContext::currentlyOn(ThreadType::Main));
+
+ if (++count == 4) {
+ loop.stop();
+ }
+ };
+
+ thread.invoke(&TestThread::send, endTest);
+ thread.invoke(&TestThread::send, endTest);
+ thread.invoke(&TestThread::send, endTest);
+ thread.invoke(&TestThread::send, endTest);
+
+ uv_run(uv_default_loop(), UV_RUN_DEFAULT);
+}
+
+TEST(WorkQueue, cancel) {
+ RunLoop loop(uv_default_loop());
+
+ WorkQueue queue;
+
+ auto work = [&]() {
+ FAIL() << "Should never be called";
+ };
+
+ queue.push(work);
+ queue.push(work);
+ queue.push(work);
+ queue.push(work);
+ queue.push(work);
+}
diff --git a/test/test.gypi b/test/test.gypi
index 461719e33e..7d97290c8d 100644
--- a/test/test.gypi
+++ b/test/test.gypi
@@ -69,6 +69,7 @@
'miscellaneous/thread.cpp',
'miscellaneous/tile.cpp',
'miscellaneous/transform.cpp',
+ 'miscellaneous/work_queue.cpp',
'miscellaneous/variant.cpp',
'storage/storage.hpp',