summaryrefslogtreecommitdiffstats
path: root/tests/manual/wasm/eventloop/eventloop_auto/main.cpp
blob: b2ff6779fd071bffe6e1cf6f98ea1439fcc9e67d (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0

#include <QtCore/QCoreApplication>
#include <QtCore/QEvent>
#include <QtCore/QMutex>
#include <QtCore/QObject>
#include <QtCore/QThread>
#include <QtCore/QTimer>
#include <QtCore/private/qstdweb_p.h>

#include <qtwasmtestlib.h>

#include "emscripten.h"

const int timerTimeout = 10;

class WasmEventDispatcherTest: public QObject
{
    Q_OBJECT
private slots:
    void postEventMainThread();
    void timerMainThread();
    void timerMainThreadMultiple();

#if QT_CONFIG(thread)
    void postEventSecondaryThread();
    void postEventSecondaryThreads();
    void postEventToSecondaryThread();
    void timerSecondaryThread();
#endif

    void postEventAsyncify();
    void timerAsyncify();
    void postEventAsyncifyLoop();

private:
// Disabled test function: Asyncify wait on pthread_join is not supported,
// see https://github.com/emscripten-core/emscripten/issues/9910
#if QT_CONFIG(thread)
    void threadAsyncifyWait();
#endif
};

class EventTarget : public QObject
{
    Q_OBJECT

public:
    static EventTarget *create(std::function<void()> callback)
    {
        return new EventTarget(callback);
    }

    static QEvent *createEvent()
    {
        return new QEvent(QEvent::User);
    }

protected:
    EventTarget(std::function<void()> callback)
        : m_callback(callback) { }

    bool event(QEvent *evt)
    {
        if (evt->type() == QEvent::User) {
            m_callback();
            deleteLater();
            return true;
        }
        return QObject::event(evt);
    }

private:
    std::function<void()> m_callback;
};

class CompleteTestFunctionRefGuard {
public:
    CompleteTestFunctionRefGuard(CompleteTestFunctionRefGuard const&) = delete;
    CompleteTestFunctionRefGuard& operator=(CompleteTestFunctionRefGuard const&) = delete;

    static CompleteTestFunctionRefGuard *create() {
        return new CompleteTestFunctionRefGuard();
    }

    void ref() {
        QMutexLocker lock(&mutex);
        ++m_counter;
    }

    void deref() {
        const bool finalDeref = [this] {
            QMutexLocker lock(&mutex);
            return --m_counter == 0;
        }();

        if (finalDeref)
           QtWasmTest::completeTestFunction();
    }
private:
    CompleteTestFunctionRefGuard() { };

    QMutex mutex;
    int m_counter = 0;
};

#if QT_CONFIG(thread)

class TestThread : public QThread
{
public:
    static QThread *create(std::function<void()> started, std::function<void()> finished)
    {
        TestThread *thread = new TestThread();
        connect(thread, &QThread::started, [started]() {
            started();
        });
        connect(thread, &QThread::finished, [thread, finished]() {
            finished();
            thread->deleteLater();
        });
        thread->start();
        return thread;
    }
};

#endif

// Post event to the main thread and verify that it is processed.
void WasmEventDispatcherTest::postEventMainThread()
{
    QCoreApplication::postEvent(EventTarget::create([](){
        QtWasmTest::completeTestFunction();
    }), EventTarget::createEvent());
}

// Create a timer on the main thread and verify that it fires
void WasmEventDispatcherTest::timerMainThread()
{
    QTimer::singleShot(timerTimeout, [](){
        QtWasmTest::completeTestFunction();
    });
}

void WasmEventDispatcherTest::timerMainThreadMultiple()
{
    CompleteTestFunctionRefGuard *completeGuard = CompleteTestFunctionRefGuard::create();
    int timers = 10;
    for (int i = 0; i < timers; ++i) {
        completeGuard->ref();
        QTimer::singleShot(timerTimeout * i, [completeGuard](){
            completeGuard->deref();
        });
    }
}

#if QT_CONFIG(thread)

// Post event on a secondary thread and verify that it is processed.
void WasmEventDispatcherTest::postEventSecondaryThread()
{
    auto started = [](){
        QCoreApplication::postEvent(EventTarget::create([](){
            QThread::currentThread()->quit();
        }), EventTarget::createEvent());
    };

    auto finished = [](){
        QtWasmTest::completeTestFunction();
    };

    TestThread::create(started, finished);
}

// Post event _to_ a secondary thread and verify that it is processed.
void WasmEventDispatcherTest::postEventToSecondaryThread()
{
    auto started = [](){};
    auto finished = [](){
        QtWasmTest::completeTestFunction();
    };

    QThread *t = TestThread::create(started, finished);
    EventTarget *target = EventTarget::create([](){
        QThread::currentThread()->quit();
    });
    target->moveToThread(t);
    QCoreApplication::postEvent(target, EventTarget::createEvent());
}

// Post events to many secondary threads and verify that they are processed.
void WasmEventDispatcherTest::postEventSecondaryThreads()
{
    // This test completes afer all threads has finished
    CompleteTestFunctionRefGuard *completeGuard = CompleteTestFunctionRefGuard::create();
    completeGuard->ref(); // including this thread

    auto started = [](){
        QCoreApplication::postEvent(EventTarget::create([](){
            QThread::currentThread()->quit();
        }), EventTarget::createEvent());
    };

    auto finished = [completeGuard](){
        completeGuard->deref();
    };

    // Start a nymber of threads in parallel, keeping in mind that the browser
    // has some max number of concurrent web workers (maybe 20), and that starting
    // a new web worker requires completing a GET network request for the worker's JS.
    const int numThreads = 10;
    for (int i = 0; i < numThreads; ++i) {
        completeGuard->ref();
        TestThread::create(started, finished);
    }

    completeGuard->deref();
}

// Create a timer a secondary thread and verify that it fires
void WasmEventDispatcherTest::timerSecondaryThread()
{
    auto started = [](){
        QTimer::singleShot(timerTimeout, [](){
            QThread::currentThread()->quit();
        });
    };

    auto finished = [](){
        QtWasmTest::completeTestFunction();
    };

    TestThread::create(started, finished);
}

#endif

// Post an event to the main thread and asyncify wait for it
void WasmEventDispatcherTest::postEventAsyncify()
{
    if (!qstdweb::haveAsyncify()) {
        QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
        return;
    }

    QEventLoop loop;
    QCoreApplication::postEvent(EventTarget::create([&loop](){
        loop.quit();
    }), EventTarget::createEvent());
    loop.exec();

    QtWasmTest::completeTestFunction();
}

// Create a timer on the main thread and asyncify wait for it
void WasmEventDispatcherTest::timerAsyncify()
{
    if (!qstdweb::haveAsyncify()) {
        QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
        return;
    }

    QEventLoop loop;
    QTimer::singleShot(timerTimeout, [&loop](){
        loop.quit();
    });
    loop.exec();

    QtWasmTest::completeTestFunction();
}

// Asyncify wait in a loop
void WasmEventDispatcherTest::postEventAsyncifyLoop()
{
    if (!qstdweb::haveAsyncify()) {
        QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");
        return;
    }

    for (int i = 0; i < 10; ++i) {
        QEventLoop loop;
        QCoreApplication::postEvent(EventTarget::create([&loop]() {
            loop.quit();
        }), EventTarget::createEvent());
        loop.exec();
    }

    QtWasmTest::completeTestFunction();
}

#if QT_CONFIG(thread)
// Asyncify wait for QThread::wait() / pthread_join()
void WasmEventDispatcherTest::threadAsyncifyWait()
{
    if (!qstdweb::haveAsyncify())
        QtWasmTest::completeTestFunction(QtWasmTest::TestResult::Skip, "requires asyncify");

    const int threadCount = 15;

    QVector<QThread *> threads;
    threads.reserve(threadCount);

    for (int i = 0; i < threadCount; ++i) {
        QThread *thread = new QThread();
        threads.push_back(thread);
        thread->start();
    }

    for (int i = 0; i < threadCount; ++i) {
        QThread *thread = threads[i];
        thread->wait();
        delete thread;
    }

    QtWasmTest::completeTestFunction();
}
#endif

int main(int argc, char **argv)
{
    auto testObject = std::make_shared<WasmEventDispatcherTest>();
    QtWasmTest::initTestCase<QCoreApplication>(argc, argv, testObject);
    return 0;
}

#include "main.moc"