File: qcoroprocess.cpp

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

#include "testobject.h"

#include "qcoro/core/qcoroprocess.h"

#include <QProcess>

#ifdef Q_OS_WIN
// There's no equivalent to "true" command on Windows, so do a single ping to localhost instead,
// which terminates almost immediately.
#define DUMMY_EXEC QStringLiteral("ping")
#define DUMMY_ARGS                                                                                 \
    { QStringLiteral("127.0.0.1"), QStringLiteral("-n"), QStringLiteral("1") }
// On windows, the equivalent to Linux "sleep" is "timeout", but it fails due to QProcess redirecting
// stdin, which "timeout" doesn't support (it waits for keypress to interrupt). However, "ping" pings
// every second, so specifying number of pings to the desired timeout makes it behave basically like
// the Linux "sleep".
#define SLEEP_EXEC QStringLiteral("ping")
#define SLEEP_ARGS(timeout)                                                                        \
    { QStringLiteral("127.0.0.1"), QStringLiteral("-n"), QString::number(timeout) }

#else
#define DUMMY_EXEC QStringLiteral("true")
#define DUMMY_ARGS {}
#define SLEEP_EXEC QStringLiteral("sleep")
#define SLEEP_ARGS(timeout) { QString::number(timeout) }
#endif

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

private:
    QCoro::Task<> testStartTriggers_coro(QCoro::TestContext context) {
#ifdef Q_OS_WIN
        // QProcess::start() on Windows is synchronous, despite what the documentation says,
        // so the coroutine will not suspend.
        context.setShouldNotSuspend();
#else
        Q_UNUSED(context);
#endif

        QProcess process;
        const bool ok = co_await qCoro(process).start(DUMMY_EXEC, DUMMY_ARGS);

        QCORO_VERIFY(ok);
        QCORO_COMPARE(process.state(), QProcess::Running);

        process.waitForFinished();
    }

    void testThenStartTriggers_coro(TestLoop &el) {
        QProcess process;
        bool called = false;
        qCoro(process).start(DUMMY_EXEC, DUMMY_ARGS).then([&](bool started) {
            called = true;
            el.quit();
            QVERIFY(started);
            QCOMPARE(process.state(), QProcess::Running);
        });
        el.exec();
        QVERIFY(called);

        process.waitForFinished();
    }

    QCoro::Task<> testStartNoArgsTriggers_coro(QCoro::TestContext context) {
#ifdef Q_OS_WIN
        context.setShouldNotSuspend();
#else
        Q_UNUSED(context);
#endif

        QProcess process;
        process.setProgram(DUMMY_EXEC);
        process.setArguments(DUMMY_ARGS);

        const bool ok = co_await qCoro(process).start();

        QCORO_VERIFY(ok);
        QCORO_COMPARE(process.state(), QProcess::Running);

        process.waitForFinished();
    }

    void testThenStartNoArgsTriggers_coro(TestLoop &el) {
        QProcess process;
        process.setProgram(DUMMY_EXEC);
        process.setArguments(DUMMY_ARGS);

        bool called = false;
        qCoro(process).start().then([&](bool started) {
            called = true;
            el.quit();
            QVERIFY(started);
            QCOMPARE(process.state(), QProcess::Running);
        });
        el.exec();
        QVERIFY(called);

        process.waitForFinished();
    }

    QCoro::Task<> testStartDoesntBlock_coro(QCoro::TestContext) {
        QCoro::EventLoopChecker eventLoopResponsive{1, 0ms};

        QProcess process;
        const bool ok = co_await qCoro(process).start(DUMMY_EXEC, DUMMY_ARGS);

        QCORO_VERIFY(ok);
        QCORO_VERIFY(eventLoopResponsive);

        process.waitForFinished();
    }

    QCoro::Task<> testStartDoesntCoAwaitRunningProcess_coro(QCoro::TestContext ctx) {
        QProcess process;
#if defined(__GNUC__) && !defined(__clang__)
#pragma message "Workaround for GCC ICE!"
        // Workaround GCC bug https://bugzilla.redhat.com/1952671
        // GCC ICEs at the end of this function due to presence of two co_await statements.
        process.start(SLEEP_EXEC, SLEEP_ARGS(1));
        process.waitForStarted();
#else
        const bool ok = co_await qCoro(process).start(SLEEP_EXEC, SLEEP_ARGS(1));
        QCORO_VERIFY(ok);
#endif

        QCORO_COMPARE(process.state(), QProcess::Running);

        ctx.setShouldNotSuspend();

        QTest::ignoreMessage(QtWarningMsg, "QProcess::start: Process is already running");
        co_await qCoro(process).start();

        process.waitForFinished();
    }

    QCoro::Task<> testFinishTriggers_coro(QCoro::TestContext) {
        QProcess process;
        process.start(SLEEP_EXEC, SLEEP_ARGS(1));
        process.waitForStarted();

        QCORO_COMPARE(process.state(), QProcess::Running);

        const auto ok = co_await qCoro(process).waitForFinished();

        QCORO_VERIFY(ok);
        QCORO_COMPARE(process.state(), QProcess::NotRunning);
    }

    void testThenFinishTriggers_coro(TestLoop &el) {
        QProcess process;
        process.start(SLEEP_EXEC, SLEEP_ARGS(1));
        process.waitForStarted();

        QCOMPARE(process.state(), QProcess::Running);

        bool called = false;
        qCoro(process).waitForFinished().then([&](bool finished) {
            called = true;
            el.quit();
            QVERIFY(finished);

            QCOMPARE(process.state(), QProcess::NotRunning);
            QCOMPARE(process.exitStatus(), QProcess::NormalExit);
        });
        el.exec();
        QVERIFY(called);
    }

    QCoro::Task<> testFinishDoesntCoAwaitFinishedProcess_coro(QCoro::TestContext ctx) {
        QProcess process;
        process.start(DUMMY_EXEC, QStringList DUMMY_ARGS);
        process.waitForFinished();

        ctx.setShouldNotSuspend();

        const auto ok = co_await qCoro(process).waitForFinished();
        QCORO_VERIFY(!ok);
    }

    QCoro::Task<> testFinishCoAwaitTimeout_coro(QCoro::TestContext) {
        QProcess process;

        process.start(SLEEP_EXEC, SLEEP_ARGS(2));
        process.waitForStarted();

        QCORO_COMPARE(process.state(), QProcess::Running);

        const auto ok = co_await qCoro(process).waitForFinished(1s);

        QCORO_VERIFY(!ok);
        QCORO_COMPARE(process.state(), QProcess::Running);

        process.waitForFinished();
    }

    void testThenFinishCoAwaitTimeout_coro(TestLoop &el) {
        QProcess process;
        process.start(SLEEP_EXEC, SLEEP_ARGS(2));
        process.waitForStarted();

        QCOMPARE(process.state(), QProcess::Running);
        bool called = false;
        qCoro(process).waitForFinished(1s).then([&](bool finished) {
            called = true;
            el.quit();
            QVERIFY(!finished);
        });
        el.exec();
        QVERIFY(called);

        process.waitForFinished();
    }

private Q_SLOTS:
    addCoroAndThenTests(StartTriggers)
    addCoroAndThenTests(StartNoArgsTriggers)
#ifndef Q_OS_WIN // start always blocks on Windows
    addTest(StartDoesntBlock)
#endif
    addTest(StartDoesntCoAwaitRunningProcess)
    addCoroAndThenTests(FinishTriggers)
    addTest(FinishDoesntCoAwaitFinishedProcess)
    addCoroAndThenTests(FinishCoAwaitTimeout)
};

QTEST_GUILESS_MAIN(QCoroProcessTest)

#include "qcoroprocess.moc"