File: qtimer.cpp

package info (click to toggle)
qcoro 0.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,700 kB
  • sloc: cpp: 8,573; python: 32; xml: 26; makefile: 23; sh: 15
file content (105 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (2)
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
// SPDX-FileCopyrightText: 2021 Daniel Vrátil <dvratil@kde.org>
//
// SPDX-License-Identifier: MIT

#include "testobject.h"

#include "qcorotimer.h"

#include <chrono>

#include <QElapsedTimer>

using namespace std::chrono_literals;

class QCoroTimerTest : public QCoro::TestObject<QCoroTimerTest> {
    Q_OBJECT

private:
    QCoro::Task<> testTriggers_coro(QCoro::TestContext) {
        QTimer timer;
        timer.setInterval(100ms);
        timer.start();

        co_await timer;
    }

    QCoro::Task<> testQCoroWrapperTriggers_coro(QCoro::TestContext) {
        QTimer timer;
        timer.setInterval(100ms);
        timer.start();

        co_await qCoro(timer).waitForTimeout();
    }

    QCoro::Task<> testDoesntBlockEventLoop_coro(QCoro::TestContext) {
        QCoro::EventLoopChecker eventLoopResponsive;

        QTimer timer;
        timer.setInterval(500ms);
        timer.start();

        co_await timer;

        QCORO_VERIFY(eventLoopResponsive);
    }

    QCoro::Task<> testDoesntCoAwaitInactiveTimer_coro(QCoro::TestContext ctx) {
        ctx.setShouldNotSuspend();

        QTimer timer;
        timer.setInterval(1s);
        // Don't start the timer!

        co_await timer;
    }

    QCoro::Task<> testDoesntCoAwaitNullTimer_coro(QCoro::TestContext ctx) {
        ctx.setShouldNotSuspend();

        QTimer *timer = nullptr;

        co_await timer;
    }

    void testThenTriggers_coro(TestLoop &el) {
        QTimer timer;
        bool triggered = false;
        timer.start(10ms);
        qCoro(timer).waitForTimeout().then([&el, &triggered]() {
            triggered = true;
            el.quit();
        });
        el.exec();
        QVERIFY(triggered);
    }

    QCoro::Task<> testSleepFor_coro(QCoro::TestContext) {
        QElapsedTimer elapsed;
        elapsed.start();
        co_await QCoro::sleepFor(100ms);
        QCORO_VERIFY(elapsed.elapsed() >= 75);
    }

    QCoro::Task<> testSleepUntil_coro(QCoro::TestContext) {
        QElapsedTimer elapsed;
        elapsed.start();
        co_await QCoro::sleepUntil(std::chrono::steady_clock::now() + 500ms);
        QCORO_VERIFY(elapsed.elapsed() >= 475);
    }

private Q_SLOTS:
    addTest(Triggers)
    addTest(QCoroWrapperTriggers)
    addTest(DoesntBlockEventLoop)
    addTest(DoesntCoAwaitInactiveTimer)
    addTest(DoesntCoAwaitNullTimer)
    addTest(SleepFor)
    addTest(SleepUntil)

    addThenTest(Triggers)
};

QTEST_GUILESS_MAIN(QCoroTimerTest)

#include "qtimer.moc"