diff --git a/autotests/unit/file/pendingfilequeuetest.cpp b/autotests/unit/file/pendingfilequeuetest.cpp index 16dcbf3c..9854400b 100644 --- a/autotests/unit/file/pendingfilequeuetest.cpp +++ b/autotests/unit/file/pendingfilequeuetest.cpp @@ -1,116 +1,127 @@ /* This file is part of the KDE Baloo project. Copyright (C) 2011 Sebastian Trueg Copyright (C) 2013-2014 Vishesh Handa This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "pendingfilequeue.h" #include #include #include -using namespace Baloo; +namespace Baloo { class PendingFileQueueTest : public QObject { Q_OBJECT public: PendingFileQueueTest(); private Q_SLOTS: void testTimeout(); void testRequeue(); }; PendingFileQueueTest::PendingFileQueueTest() { } void PendingFileQueueTest::testTimeout() { QString myUrl(QStringLiteral("/tmp")); PendingFileQueue queue; - queue.setMinimumTimeout(2); + queue.setMinimumTimeout(1); + queue.setTrackingTime(1); QSignalSpy spy(&queue, SIGNAL(indexModifiedFile(QString))); QVERIFY(spy.isValid()); PendingFile file(myUrl); file.setModified(); queue.enqueue(file); // The signal should be emitted immediately - QVERIFY(spy.wait()); + QVERIFY(spy.wait(50)); QCOMPARE(spy.count(), 1); QCOMPARE(spy.takeFirst().constFirst().toString(), myUrl); + QCOMPARE(queue.m_recentlyEmitted.count(), 1); // Enqueue the url again. This time it should wait for should wait for the // minimumTimeout queue.enqueue(file); - QTest::qWait(1000); + QTest::qWait(100); + QCOMPARE(queue.m_pendingFiles.count(), 1); + QCOMPARE(queue.m_recentlyEmitted.count(), 1); QVERIFY(spy.isEmpty()); - QVERIFY(spy.wait(2000)); + QVERIFY(spy.wait(1500)); QCOMPARE(spy.count(), 1); QCOMPARE(spy.takeFirst().constFirst().toString(), myUrl); + QCOMPARE(queue.m_pendingFiles.count(), 0); + QCOMPARE(queue.m_recentlyEmitted.count(), 1); + + // Wait for Tracking Time + QTest::qWait(1500); + QCOMPARE(queue.m_recentlyEmitted.count(), 0); } void PendingFileQueueTest::testRequeue() { QString myUrl(QStringLiteral("/tmp")); PendingFileQueue queue; queue.setMinimumTimeout(2); queue.setMaximumTimeout(5); QSignalSpy spy(&queue, SIGNAL(indexModifiedFile(QString))); QVERIFY(spy.isValid()); PendingFile file(myUrl); file.setModified(); queue.enqueue(file); // The signal should be emitted immediately QTest::qWait(20); QCOMPARE(spy.count(), 1); QCOMPARE(spy.takeFirst().constFirst().toString(), myUrl); // Send many events queue.enqueue(file); QTest::qWait(20); queue.enqueue(file); QTest::qWait(20); queue.enqueue(file); QTest::qWait(20); QTest::qWait(3500); QVERIFY(spy.isEmpty()); QVERIFY(spy.wait(2500)); QCOMPARE(spy.count(), 1); QCOMPARE(spy.takeFirst().constFirst().toString(), myUrl); } +} // namespace -QTEST_GUILESS_MAIN(PendingFileQueueTest) +QTEST_GUILESS_MAIN(Baloo::PendingFileQueueTest) #include "pendingfilequeuetest.moc" diff --git a/src/file/pendingfilequeue.h b/src/file/pendingfilequeue.h index b7f15eb8..f113c73e 100644 --- a/src/file/pendingfilequeue.h +++ b/src/file/pendingfilequeue.h @@ -1,99 +1,103 @@ /* This file is part of the KDE Baloo project. Copyright (C) 2011 Sebastian Trueg Copyright (C) 2013-2014 Vishesh Handa This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef PENDINGFILEQUEUE_H #define PENDINGFILEQUEUE_H #include "pendingfile.h" #include #include #include #include #include namespace Baloo { +class PendingFileQueueTest; + /** * */ class PendingFileQueue : public QObject { Q_OBJECT public: explicit PendingFileQueue(QObject* parent = nullptr); ~PendingFileQueue(); Q_SIGNALS: void indexNewFile(const QString& fileUrl); void indexModifiedFile(const QString& fileUrl); void indexXAttr(const QString& fileUrl); void removeFileIndex(const QString& fileUrl); public Q_SLOTS: void enqueue(const PendingFile& file); /** * The number of seconds the file should be tracked after it has * been emitted. This defaults to 2 minutes */ void setTrackingTime(int seconds); /** * Set the minimum amount of seconds a file should be kept in the queue * on receiving successive modifications events. */ void setMinimumTimeout(int seconds); void setMaximumTimeout(int seconds); private Q_SLOTS: void processCache(); void processPendingFiles(); void clearRecentlyEmitted(); private: QVector m_cache; QTimer m_cacheTimer; QTimer m_clearRecentlyEmittedTimer; QTimer m_pendingFilesTimer; /** * Holds the list of files that were recently emitted along with * the time they were last emitted. */ QHash m_recentlyEmitted; /** * The QTime contains the time when these file events should be processed. */ QHash m_pendingFiles; int m_minTimeout; int m_maxTimeout; int m_trackingTime; + + friend class PendingFileQueueTest; }; } #endif