summaryrefslogtreecommitdiff
path: root/test/util/async_task.cpp
blob: 95cf160548ebf78f219793bd351c11f92cc59b94 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <mbgl/util/async_task.hpp>
#include <mbgl/util/run_loop.hpp>
#include <mbgl/util/thread.hpp>

#include <mbgl/test/util.hpp>

#include <vector>

using namespace mbgl::util;

namespace {

class TestWorker {
public:
    TestWorker(AsyncTask *async_)
        : async(async_) {}

    void run() {
        for (unsigned i = 0; i < 100000; ++i) {
            async->send();
        }
    }

    void runWithCallback(std::function<void()> cb) {
        for (unsigned i = 0; i < 100000; ++i) {
            async->send();
        }

        cb();
    }

private:
    AsyncTask *async;
};

} // namespace

TEST(AsyncTask, RequestCoalescing) {
    RunLoop loop;

    unsigned count = 0;
    AsyncTask async([&count] { ++count; });

    async.send();
    async.send();
    async.send();
    async.send();
    async.send();

    loop.runOnce();

    EXPECT_EQ(count, 1);
}

TEST(AsyncTask, DestroyShouldNotRunQueue) {
    RunLoop loop;

    unsigned count = 0;
    auto async = std::make_unique<AsyncTask>([&count] { ++count; });

    async->send();
    async.reset();

    EXPECT_EQ(count, 0);
}

TEST(AsyncTask, RequestCoalescingMultithreaded) {
    RunLoop loop;

    unsigned count = 0;
    AsyncTask async([&count] { ++count; });

    std::vector<std::unique_ptr<Thread<TestWorker>>> threads;
    ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};

    unsigned numThreads = 25;
    for (unsigned i = 0; i < numThreads; ++i) {
        std::unique_ptr<Thread<TestWorker>> thread =
            std::make_unique<Thread<TestWorker>>(context, &async);

        thread->invoke(&TestWorker::run);
        threads.push_back(std::move(thread));
    }

    // Join all the threads
    threads.clear();

    loop.runOnce();

    EXPECT_EQ(count, 1);
}

TEST(AsyncTask, ThreadSafety) {
    RunLoop loop;

    unsigned count = 0;
    AsyncTask async([&count] { ++count; });

    unsigned numThreads = 25;

    auto callback = [&] {
        if (!--numThreads) {
            loop.stop();
        }
    };

    std::vector<std::unique_ptr<Thread<TestWorker>>> threads;
    std::vector<std::unique_ptr<mbgl::WorkRequest>> requests;
    ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular};

    for (unsigned i = 0; i < numThreads; ++i) {
        std::unique_ptr<Thread<TestWorker>> thread =
            std::make_unique<Thread<TestWorker>>(context, &async);

        requests.push_back(
            thread->invokeWithCallback(&TestWorker::runWithCallback, callback));

        threads.push_back(std::move(thread));
    }

    loop.run();

    // We expect here more than 1 but 1 would also be
    // a valid result, although very unlikely (I hope).
    EXPECT_GT(count, 1);
}