File indexing completed on 2024-04-28 03:51:40

0001 /*
0002     This file is part of the KDE Baloo Project
0003     SPDX-FileCopyrightText: 2021 Stefan BrĂ¼ns <stefan.bruens@rwth-aachen.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006 */
0007 
0008 #include "extractor/commandpipe.h"
0009 #include "testsconfig.h"
0010 
0011 #include <QProcess>
0012 #include <QSignalSpy>
0013 #include <QStandardPaths>
0014 #include <QTest>
0015 
0016 namespace Baloo {
0017 namespace Test {
0018 
0019 class ExtractorCommandPipeTest : public QObject
0020 {
0021     Q_OBJECT
0022 
0023 public:
0024     ExtractorCommandPipeTest() : m_controller(&m_worker, &m_worker) {}
0025 
0026 private Q_SLOTS:
0027 
0028     void initTestCase();
0029 
0030     void init();
0031     void cleanup();
0032 
0033     void emptyBatch();
0034     void singleBatch();
0035     void singleBatch_data();
0036     void multipleBatch();
0037     void multipleSequentialBatches();
0038 
0039 private:
0040     QProcess m_worker;
0041     QString m_workerPath;
0042     Baloo::Private::ControllerPipe m_controller;
0043 };
0044 
0045 void ExtractorCommandPipeTest::initTestCase()
0046 {
0047     m_workerPath = QStandardPaths::findExecutable(QStringLiteral("extractorcommandpipe_worker"), {QStringLiteral(EXTRACTOR_TESTS_HELPER_PATH)});
0048     QVERIFY(!m_workerPath.isEmpty());
0049 }
0050 
0051 void ExtractorCommandPipeTest::init()
0052 {
0053     connect(&m_worker, &QProcess::readyRead, &m_controller, &Baloo::Private::ControllerPipe::processStatusData);
0054 
0055     m_worker.setProgram(m_workerPath);
0056     m_worker.setProcessChannelMode(QProcess::ForwardedErrorChannel);
0057 
0058     m_worker.start(QIODevice::Unbuffered | QIODevice::ReadWrite);
0059     m_worker.waitForStarted();
0060     m_worker.setReadChannel(QProcess::StandardOutput);
0061 }
0062 
0063 void ExtractorCommandPipeTest::cleanup()
0064 {
0065     m_worker.closeWriteChannel();
0066     m_worker.waitForFinished();
0067     m_worker.close();
0068 }
0069 
0070 void ExtractorCommandPipeTest::emptyBatch()
0071 {
0072     QSignalSpy spy(&m_controller, &Baloo::Private::ControllerPipe::batchFinished);
0073 
0074     m_controller.processIds({});
0075 
0076     QVERIFY(spy.wait());
0077     QCOMPARE(spy.count(), 1);
0078 }
0079 
0080 void ExtractorCommandPipeTest::singleBatch()
0081 {
0082     QFETCH(QVector<quint64>, ids);
0083 
0084     QStringList finishedUrls;
0085     auto connection = connect(&m_controller, &Baloo::Private::ControllerPipe::urlFinished,
0086          [&finishedUrls](const QString& url) {
0087              finishedUrls.append(url);
0088              // qDebug() << "fileFinished" << url;
0089     });
0090 
0091     QSignalSpy spy(&m_controller, &Baloo::Private::ControllerPipe::batchFinished);
0092 
0093     m_controller.processIds(ids);
0094 
0095     QVERIFY(spy.wait());
0096 
0097     QCOMPARE(finishedUrls.size(), ids.size());
0098     disconnect(connection);
0099 }
0100 
0101 void ExtractorCommandPipeTest::singleBatch_data()
0102 {
0103     QTest::addColumn<QVector<quint64>>("ids");
0104 
0105     QTest::addRow("singleEntry") << QVector<quint64>{3};
0106     QTest::addRow("multipleEntry") << QVector<quint64>{1, 2, 3, 4, 5, 100, 101, 102, 103, 1000};
0107 
0108     QVector<quint64> longlist;
0109     for (quint64 i = 0; i < 50; i++) {
0110         longlist.append(i);
0111     }
0112     QTest::addRow("manyEntries") << longlist;
0113 }
0114 
0115 void ExtractorCommandPipeTest::multipleBatch()
0116 {
0117     QSignalSpy spy(&m_controller, &Baloo::Private::ControllerPipe::batchFinished);
0118 
0119     m_controller.processIds({1, 11});
0120     m_controller.processIds({2, 22});
0121     m_controller.processIds({3, 33});
0122 
0123     while (spy.count() < 3) {
0124         QVERIFY(spy.wait(500));
0125     }
0126     QCOMPARE(spy.count(), 3);
0127 }
0128 
0129 void ExtractorCommandPipeTest::multipleSequentialBatches()
0130 {
0131     QSignalSpy spy(&m_controller, &Baloo::Private::ControllerPipe::batchFinished);
0132 
0133     m_controller.processIds({1, 11});
0134     QVERIFY(spy.wait());
0135     QCOMPARE(spy.count(), 1);
0136 
0137     m_controller.processIds({2, 22});
0138     QVERIFY(spy.wait());
0139     QCOMPARE(spy.count(), 2);
0140 
0141     m_controller.processIds({3, 33});
0142     QVERIFY(spy.wait());
0143     QCOMPARE(spy.count(), 3);
0144 }
0145 
0146 } // namespace Test
0147 } // namespace Baloo
0148 
0149 using namespace Baloo::Test;
0150 
0151 QTEST_GUILESS_MAIN(ExtractorCommandPipeTest)
0152 
0153 #include "extractorcommandpipetest.moc"