File: kcompoundjobtest.cpp

package info (click to toggle)
kdevelop 4%3A24.12.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 71,888 kB
  • sloc: cpp: 290,869; python: 3,626; javascript: 3,518; sh: 1,316; ansic: 703; xml: 401; php: 95; lisp: 66; makefile: 31; sed: 12
file content (240 lines) | stat: -rw-r--r-- 6,815 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
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
/*
    This file is part of the KDE project
    SPDX-FileCopyrightText: 2013 Kevin Funk <kevin@kfunk.org>
    SPDX-FileCopyrightText: 2023 Igor Kushnir <igorkuo@gmail.com>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include "kcompoundjobtest.h"

#include "ksequentialcompoundjob.h"

#include <QEventLoop>
#include <QMetaEnum>
#include <QSignalSpy>
#include <QStandardPaths>
#include <QTest>
#include <QTimer>

namespace
{
class TestSequentialCompoundJob : public KSequentialCompoundJob
{
public:
    using KSequentialCompoundJob::addSubjob;
    using KSequentialCompoundJob::clearSubjobs;
};

struct JobSpies {
    QSignalSpy finished;
    QSignalSpy result;
    QSignalSpy destroyed;
    explicit JobSpies(KJob *job)
        : finished(job, &KJob::finished)
        , result(job, &KJob::result)
        , destroyed(job, &QObject::destroyed)
    {
    }
};
} // namespace

TestJob::TestJob(QObject *parent)
    : KJob(parent)
{
}

void TestJob::start()
{
    QTimer::singleShot(1000, this, &TestJob::emitResult);
}

KillableTestJob::KillableTestJob(QObject *parent)
    : TestJob(parent)
{
    setCapabilities(Killable);
}

bool KillableTestJob::doKill()
{
    return true;
}

void TestCompoundJob::start()
{
    if (hasSubjobs()) {
        subjobs().first()->start();
    } else {
        emitResult();
    }
}

void TestCompoundJob::subjobFinished(KJob *job)
{
    KCompoundJob::subjobFinished(job);

    if (error()) {
        return; // KCompoundJob::subjobFinished() must have called emitResult().
    }
    if (hasSubjobs()) {
        // start next
        subjobs().first()->start();
    } else {
        emitResult();
    }
}

KCompoundJobTest::KCompoundJobTest()
{
}

void KCompoundJobTest::initTestCase()
{
    QStandardPaths::setTestModeEnabled(true);
}

/**
 * In case a compound job is deleted during execution
 * we still want to assure that we don't crash
 *
 * see bug: https://bugs.kde.org/show_bug.cgi?id=230692
 */
template<class CompoundJob>
static void testDeletionDuringExecution()
{
    QObject *someParent = new QObject;
    KJob *job = new TestJob(someParent);

    auto *compoundJob = new CompoundJob;
    compoundJob->setAutoDelete(false);
    QVERIFY(compoundJob->addSubjob(job));

    QCOMPARE(job->parent(), compoundJob);

    QSignalSpy destroyed_spy(job, &QObject::destroyed);
    // check if job got reparented properly
    delete someParent;
    someParent = nullptr;
    // the job should still exist, because it is a child of KCompoundJob now
    QCOMPARE(destroyed_spy.size(), 0);

    // start async, the subjob takes 1 second to finish
    compoundJob->start();

    // delete the job during the execution
    delete compoundJob;
    compoundJob = nullptr;
    // at this point, the subjob should be deleted, too
    QCOMPARE(destroyed_spy.size(), 1);
}

void KCompoundJobTest::testDeletionDuringExecution_data()
{
    QTest::addColumn<bool>("useSequentialCompoundJob");
    QTest::newRow("CompoundJob") << false;
    QTest::newRow("SequentialCompoundJob") << true;
}

void KCompoundJobTest::testDeletionDuringExecution()
{
    QFETCH(const bool, useSequentialCompoundJob);
    if (useSequentialCompoundJob) {
        ::testDeletionDuringExecution<TestSequentialCompoundJob>();
    } else {
        ::testDeletionDuringExecution<TestCompoundJob>();
    }
}

template<class CompoundJob>
static void testFinishingSubjob()
{
    auto *const job = new KillableTestJob;
    auto *const compoundJob = new CompoundJob;
    QVERIFY(compoundJob->addSubjob(job));

    JobSpies jobSpies(job);
    JobSpies compoundJobSpies(compoundJob);

    compoundJob->start();

    using Action = KCompoundJobTest::Action;
    QFETCH(const Action, action);
    switch (action) {
    case Action::Finish:
        job->emitResult();
        break;
    case Action::KillVerbosely:
        QVERIFY(job->kill(KJob::EmitResult));
        break;
    case Action::KillQuietly:
        QVERIFY(job->kill(KJob::Quietly));
        break;
    case Action::Destroy:
        job->deleteLater();
        break;
    }

    QEventLoop loop;
    QTimer::singleShot(100, &loop, &QEventLoop::quit);
    QObject::connect(compoundJob, &QObject::destroyed, &loop, &QEventLoop::quit);
    QCOMPARE(loop.exec(), 0);

    // The following 3 comparisons verify that KJob works as expected.
    QCOMPARE(jobSpies.finished.size(), 1); // KJob::finished() is always emitted.
    // KJob::result() is not emitted when a job is killed quietly or destroyed.
    QCOMPARE(jobSpies.result.size(), action == Action::Finish || action == Action::KillVerbosely);
    // An auto-delete job is destroyed via deleteLater() when finished.
    QCOMPARE(jobSpies.destroyed.size(), 1);

    // KCompoundJob must listen to &KJob::finished signal to invoke subjobFinished()
    // no matter how a subjob is finished - normally, killed or destroyed.
    // CompoundJob calls emitResult() and is destroyed when its last subjob finishes.
    QFETCH(const bool, crashOnFailure);
    if (crashOnFailure) {
        if (compoundJobSpies.destroyed.empty()) {
            // compoundJob is still alive. This must be a bug.
            // The clearSubjobs() call will segfault if the already destroyed job
            // has not been removed from the subjob list.
            compoundJob->clearSubjobs();
            delete compoundJob;
        }
    } else {
        QCOMPARE(compoundJobSpies.finished.size(), 1);
        QCOMPARE(compoundJobSpies.result.size(), 1);
        QCOMPARE(compoundJobSpies.destroyed.size(), 1);
    }
}

void KCompoundJobTest::testFinishingSubjob_data()
{
    QTest::addColumn<bool>("useSequentialCompoundJob");
    QTest::addColumn<Action>("action");
    QTest::addColumn<bool>("crashOnFailure");

    const auto actionName = [](Action action) {
        return QMetaEnum::fromType<Action>().valueToKey(static_cast<int>(action));
    };

    for (bool useSequentialCompoundJob : {false, true}) {
        const char *const sequentialStr = useSequentialCompoundJob ? "sequential-" : "";
        for (bool crashOnFailure : {false, true}) {
            const char *const failureStr = crashOnFailure ? "segfault-on-failure" : "compound-job-destroyed";
            for (Action action : {Action::Finish, Action::KillVerbosely, Action::KillQuietly, Action::Destroy}) {
                const QByteArray dataTag = QByteArray{actionName(action)} + "-a-subjob-" + sequentialStr + failureStr;
                QTest::newRow(dataTag.constData()) << useSequentialCompoundJob << action << crashOnFailure;
            }
        }
    }
}

void KCompoundJobTest::testFinishingSubjob()
{
    QFETCH(const bool, useSequentialCompoundJob);
    if (useSequentialCompoundJob) {
        ::testFinishingSubjob<TestSequentialCompoundJob>();
    } else {
        ::testFinishingSubjob<TestCompoundJob>();
    }
}

QTEST_GUILESS_MAIN(KCompoundJobTest)