diff --git a/test/miscellaneous/work_queue.cpp b/test/miscellaneous/work_queue.cpp new file mode 100644 index 000000000..a5f616fe5 --- /dev/null +++ b/test/miscellaneous/work_queue.cpp @@ -0,0 +1,63 @@ +#include "../fixtures/util.hpp" + +#include +#include +#include + +#include + +using namespace mbgl::util; + +class TestThread { +public: + TestThread(WorkQueue* queue_) : queue(queue_) {} + + void send(std::function&& 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 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 461719e33..7d97290c8 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',