diff options
Diffstat (limited to 'test/util/work_queue.cpp')
-rw-r--r-- | test/util/work_queue.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/test/util/work_queue.cpp b/test/util/work_queue.cpp new file mode 100644 index 0000000000..a6cd6c3f88 --- /dev/null +++ b/test/util/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; + + 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); + + loop.run(); +} + +TEST(WorkQueue, cancel) { + RunLoop 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); +} |