File: test_backgroundparser.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (455 lines) | stat: -rw-r--r-- 15,413 bytes parent folder | download
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
 * This file is part of KDevelop
 *
 * Copyright 2012 by Sven Brauch <svenbrauch@googlemail.com>
 * Copyright 2012 by Milian Wolff <mail@milianw.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Library General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "test_backgroundparser.h"

#include <QTest>
#include <QElapsedTimer>
#include <QTemporaryFile>
#include <QApplication>
#include <QSemaphore>

#include <KTextEditor/Editor>
#include <KTextEditor/View>

#include <tests/autotestshell.h>
#include <tests/testcore.h>
#include <tests/testlanguagecontroller.h>

#include <language/duchain/duchain.h>
#include <language/duchain/duchainlock.h>
#include <language/backgroundparser/backgroundparser.h>

#include <interfaces/ilanguagecontroller.h>

#include "testlanguagesupport.h"
#include "testparsejob.h"

QTEST_MAIN(TestBackgroundparser)

#define QVERIFY_RETURN(statement, retval) \
    do { if (!QTest::qVerify((statement), # statement, "", __FILE__, __LINE__)) \
             return retval; } while (0)

using namespace KDevelop;

JobPlan::JobPlan()
{
}

void JobPlan::addJob(const JobPrototype& job)
{
    m_jobs << job;
}

void JobPlan::clear()
{
    m_jobs.clear();
    m_finishedJobs.clear();
    m_createdJobs.clear();
}

void JobPlan::parseJobCreated(ParseJob* job)
{
    // e.g. for the benchmark
    if (m_jobs.isEmpty()) {
        return;
    }

    auto* testJob = qobject_cast<TestParseJob*>(job);
    Q_ASSERT(testJob);

    qDebug() << "assigning propierties for created job" << testJob->document().toUrl();
    testJob->duration_ms = jobForUrl(testJob->document()).m_duration;

    m_createdJobs.append(testJob->document());
}

void JobPlan::addJobsToParser()
{
    // add parse jobs
    for (const JobPrototype& job : qAsConst(m_jobs)) {
        ICore::self()->languageController()->backgroundParser()->addDocument(
            job.m_url, TopDUContext::Empty, job.m_priority, this, job.m_flags
        );
    }
}

bool JobPlan::runJobs(int timeoutMS)
{
    addJobsToParser();

    ICore::self()->languageController()->backgroundParser()->parseDocuments();

    QElapsedTimer t;
    t.start();

    while (!t.hasExpired(timeoutMS) && m_jobs.size() != m_finishedJobs.size()) {
        QTest::qWait(50);
    }

    QVERIFY_RETURN(m_jobs.size() == m_createdJobs.size(), false);

    QVERIFY_RETURN(m_finishedJobs.size() == m_jobs.size(), false);

    // verify they're started in the right order
    int currentBestPriority = BackgroundParser::BestPriority;
    for (const IndexedString& url : qAsConst(m_createdJobs)) {
        const JobPrototype p = jobForUrl(url);
        QVERIFY_RETURN(p.m_priority >= currentBestPriority, false);
        currentBestPriority = p.m_priority;
    }

    return true;
}

JobPrototype JobPlan::jobForUrl(const IndexedString& url) const
{
    auto it = std::find_if(m_jobs.begin(), m_jobs.end(), [&](const JobPrototype& job) {
        return (job.m_url == url);
    });

    return (it != m_jobs.end()) ? *it: JobPrototype();
}

void JobPlan::updateReady(const IndexedString& url, const ReferencedTopDUContext& /*context*/)
{
    if (!ICore::self() || ICore::self()->shuttingDown()) {
        // core was shutdown before we get to handle the delayed signal, cf. testShutdownWithRunningJobs
        return;
    }

    qDebug() << "update ready on " << url.toUrl();

    const JobPrototype job = jobForUrl(url);
    QVERIFY(job.m_url.toUrl().isValid());

    if (job.m_flags & ParseJob::RequiresSequentialProcessing) {
        // ensure that all jobs that respect sequential processing
        // with lower priority have been run
        for (const JobPrototype& otherJob : qAsConst(m_jobs)) {
            if (otherJob.m_url == job.m_url) {
                continue;
            }
            if (otherJob.m_flags & ParseJob::RespectsSequentialProcessing &&
                otherJob.m_priority < job.m_priority) {
                QVERIFY(m_finishedJobs.contains(otherJob.m_url));
            }
        }
    }

    QVERIFY(!m_finishedJobs.contains(job.m_url));
    m_finishedJobs << job.m_url;
}

int JobPlan::numJobs() const
{
    return m_jobs.size();
}

int JobPlan::numCreatedJobs() const
{
    return m_createdJobs.size();
}

int JobPlan::numFinishedJobs() const
{
    return m_finishedJobs.size();
}

void TestBackgroundparser::initTestCase()
{
    AutoTestShell::init();
    TestCore* core = TestCore::initialize(Core::NoUi);

    DUChain::self()->disablePersistentStorage();

    auto* langController = new TestLanguageController(core);
    core->setLanguageController(langController);
    langController->backgroundParser()->setThreadCount(4);
    langController->backgroundParser()->abortAllJobs();

    m_langSupport = new TestLanguageSupport(this);
    connect(m_langSupport, &TestLanguageSupport::parseJobCreated,
            &m_jobPlan, &JobPlan::parseJobCreated);
    langController->addTestLanguage(m_langSupport, QStringList() << QStringLiteral("text/plain"));

    const auto languages = langController->languagesForUrl(QUrl::fromLocalFile(QStringLiteral("/foo.txt")));
    QCOMPARE(languages.size(), 1);
    QCOMPARE(languages.first(), m_langSupport);
}

void TestBackgroundparser::cleanupTestCase()
{
    TestCore::shutdown();
    m_langSupport = nullptr;
}

void TestBackgroundparser::init()
{
    m_jobPlan.clear();
}

void TestBackgroundparser::testShutdownWithRunningJobs()
{
    m_jobPlan.clear();
    // prove that background parsing happens with sequential flags although there is a high-priority
    // foreground thread (active document being edited, ...) running all the time.

    // the long-running high-prio job
    m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile(QStringLiteral("/test_fgt_hp.txt")),
                                  -500, ParseJob::IgnoresSequentialProcessing, 1000));

    m_jobPlan.addJobsToParser();

    ICore::self()->languageController()->backgroundParser()->parseDocuments();
    QTest::qWait(50);

    // shut down with running jobs, make sure we don't crash
    cleanupTestCase();

    // restart again to restore invariant (core always running in test functions)
    initTestCase();
}

void TestBackgroundparser::testParseOrdering_foregroundThread()
{
    m_jobPlan.clear();
    // prove that background parsing happens with sequential flags although there is a high-priority
    // foreground thread (active document being edited, ...) running all the time.

    // the long-running high-prio job
    m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile(QStringLiteral("/test_fgt_hp.txt")), -500,
                                  ParseJob::IgnoresSequentialProcessing, 630));

    // several small background jobs
    for (int i = 0; i < 10; i++) {
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test_fgt_lp__" + QString::number(i) + ".txt"), i,
                                      ParseJob::FullSequentialProcessing, 40));
    }

    // not enough time if the small jobs run after the large one
    QVERIFY(m_jobPlan.runJobs(700));
}

void TestBackgroundparser::testParseOrdering_noSequentialProcessing()
{
    m_jobPlan.clear();
    for (int i = 0; i < 20; i++) {
        // create jobs with no sequential processing, and different priorities
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test_nsp1__" + QString::number(i) + ".txt"), i,
                                      ParseJob::IgnoresSequentialProcessing, i));
    }

    for (int i = 0; i < 8; i++) {
        // create a few more jobs with the same priority
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test_nsp2__" + QString::number(i) + ".txt"), 10,
                                      ParseJob::IgnoresSequentialProcessing, i));
    }

    QVERIFY(m_jobPlan.runJobs(1000));
}

void TestBackgroundparser::testParseOrdering_lockup()
{
    m_jobPlan.clear();
    for (int i = 3; i > 0; i--) {
        // add 3 jobs which do not care about sequential processing, at 4 threads it should take no more than 1s to process them
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test" + QString::number(i) + ".txt"), i,
                                      ParseJob::IgnoresSequentialProcessing, 200));
    }

    // add one job which requires sequential processing with high priority
    m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile(QStringLiteral("/test_hp.txt")), -200,
                                  ParseJob::FullSequentialProcessing, 200));
    // verify that the low-priority nonsequential jobs are run simultaneously with the other one.
    QVERIFY(m_jobPlan.runJobs(700));
}

void TestBackgroundparser::testParseOrdering_simple()
{
    m_jobPlan.clear();
    for (int i = 20; i > 0; i--) {
        // the job with priority i should be at place i in the finished list
        // (lower priority value -> should be parsed first)
        ParseJob::SequentialProcessingFlags flags = ParseJob::FullSequentialProcessing;
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test" + QString::number(i) + ".txt"),
                                      i, flags));
    }

    // also add a few jobs which ignore the processing
    for (int i = 0; i < 5; ++i) {
        m_jobPlan.addJob(JobPrototype(QUrl::fromLocalFile("/test2-" + QString::number(i) + ".txt"),
                                      BackgroundParser::NormalPriority,
                                      ParseJob::IgnoresSequentialProcessing));
    }

    QVERIFY(m_jobPlan.runJobs(1000));
}

void TestBackgroundparser::benchmark()
{
    const int jobs = 10000;

    QVector<IndexedString> jobUrls;
    jobUrls.reserve(jobs);
    for (int i = 0; i < jobs; ++i) {
        jobUrls << IndexedString("/test" + QString::number(i) + ".txt");
    }

    QBENCHMARK {
        for (const IndexedString& url : qAsConst(jobUrls)) {
            ICore::self()->languageController()->backgroundParser()->addDocument(url);
        }

        ICore::self()->languageController()->backgroundParser()->parseDocuments();

        while (ICore::self()->languageController()->backgroundParser()->queuedCount()) {
            QTest::qWait(50);
        }
    }
}

void TestBackgroundparser::benchmarkDocumentChanges()
{
    KTextEditor::Editor* editor = KTextEditor::Editor::instance();
    QVERIFY(editor);
    KTextEditor::Document* doc = editor->createDocument(this);
    QVERIFY(doc);

    QString tmpFileName;
    {
        QTemporaryFile file;
        QVERIFY(file.open());
        tmpFileName = file.fileName();
    }

    doc->saveAs(QUrl::fromLocalFile(tmpFileName));

    DocumentChangeTracker tracker(doc);

    doc->setText(QStringLiteral("hello world"));
    // required for proper benchmark results
    doc->createView(nullptr);
    QBENCHMARK {
        for (int i = 0; i < 5000; i++) {
            {
                KTextEditor::Document::EditingTransaction t(doc);
                doc->insertText(KTextEditor::Cursor(0, 0), QStringLiteral("This is a test line.\n"));
            }
            QApplication::processEvents();
        }
    }
    doc->clear();
    doc->save();
}

// see also: https://bugs.kde.org/355100
void TestBackgroundparser::testNoDeadlockInJobCreation()
{
    m_jobPlan.clear();

    // we need to run the background thread first (best priority)
    const auto runUrl = QUrl::fromLocalFile(QStringLiteral("/lockInRun.txt"));
    const auto run = IndexedString(runUrl);
    m_jobPlan.addJob(JobPrototype(runUrl, BackgroundParser::BestPriority,
                                  ParseJob::IgnoresSequentialProcessing, 0));

    // before handling the foreground code (worst priority)
    const auto ctorUrl = QUrl::fromLocalFile(QStringLiteral("/lockInCtor.txt"));
    const auto ctor = IndexedString(ctorUrl);
    m_jobPlan.addJob(JobPrototype(ctorUrl, BackgroundParser::WorstPriority,
                                  ParseJob::IgnoresSequentialProcessing, 0));

    // make sure that the background thread has the duchain locked for write
    QSemaphore semaphoreA;
    // make sure the foreground thread is inside the parse job ctor
    QSemaphore semaphoreB;

    QObject lifetimeControl; // used to disconnect signal at end of scope

    // actually distribute the complicate code across threads to trigger the
    // deadlock reliably
    QObject::connect(m_langSupport, &TestLanguageSupport::aboutToCreateParseJob,
                     &lifetimeControl, [&](const IndexedString& url, ParseJob** job) {
        if (url == run) {
            auto testJob = new TestParseJob(url, m_langSupport);
            testJob->run_callback = [&](const IndexedString& url) {
                                        // this is run in the background parse thread
                                        DUChainWriteLocker lock;
                                        semaphoreA.release();
                                        // sync with the foreground parse job ctor
                                        semaphoreB.acquire();
                                        // this is acquiring the background parse lock
                                        // we want to support this order - i.e. DUChain -> Background Parser
                                        ICore::self()->languageController()->backgroundParser()->isQueued(
                                            url);
                                    };
            *job = testJob;
        } else if (url == ctor) {
            // this is run in the foreground, essentially the same
            // as code run within the parse job ctor
            semaphoreA.acquire();
            semaphoreB.release();
            // Note how currently, the background parser is locked while creating a parse job
            // thus locking the duchain here used to trigger a lock order inversion
            DUChainReadLocker lock;
            *job = new TestParseJob(url, m_langSupport);
        }
    }, Qt::DirectConnection);

    // should be able to run quickly, if no deadlock occurs
    QVERIFY(m_jobPlan.runJobs(500));
}

void TestBackgroundparser::testSuspendResume()
{
    auto parser = ICore::self()->languageController()->backgroundParser();

    m_jobPlan.clear();

    const auto runUrl = QUrl::fromLocalFile(QStringLiteral("/file.txt"));
    const auto job = JobPrototype(runUrl, BackgroundParser::BestPriority,
                                  ParseJob::IgnoresSequentialProcessing, 0);
    m_jobPlan.addJob(job);

    parser->suspend();

    m_jobPlan.addJobsToParser();

    parser->parseDocuments();
    QTest::qWait(250);

    QCOMPARE(m_jobPlan.numCreatedJobs(), 0);
    QCOMPARE(m_jobPlan.numFinishedJobs(), 0);

    parser->resume();
    QVERIFY(m_jobPlan.runJobs(100));

    // run once again, this time suspend and resume quickly after another
    m_jobPlan.clear();
    m_jobPlan.addJob(job);

    parser->suspend();
    parser->resume();
    QVERIFY(m_jobPlan.runJobs(100));
}