summaryrefslogtreecommitdiff
path: root/test/util/work_queue.cpp
blob: 60c72f7358bda09fbe9969164bec4e91b2cc1fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <mbgl/test/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) {
        queue->push(std::move(fn));
    }

private:
    WorkQueue* queue;
};

TEST(WorkQueue, push) {
    RunLoop loop;

    WorkQueue queue;
    Thread<TestThread> thread({"Test"}, &queue);

    uint8_t count = 0;

    auto endTest = [&]() {
        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);
}