浏览代码

Simplify how the event queue works. Now it is just a timer
that gets restarted on every new event, and the processing starts
when no events reset the timer for the event interval.

Duncan Mac-Vicar P 15 年之前
父节点
当前提交
83eeeb4216
共有 3 个文件被更改,包括 39 次插入43 次删除
  1. 11 34
      src/mirall/folderwatcher.cpp
  2. 0 3
      src/mirall/folderwatcher.h
  3. 28 6
      test/testfolderwatcher.cpp

+ 11 - 34
src/mirall/folderwatcher.cpp

@@ -32,7 +32,7 @@ static const uint32_t standard_event_mask =
 
 /* minimum amount of seconds between two
    events  to consider it a new event */
-#define DEFAULT_EVENT_INTERVAL_SEC 5
+#define DEFAULT_EVENT_INTERVAL_SEC 3
 
 namespace Mirall {
 
@@ -209,52 +209,29 @@ void FolderWatcher::slotINotifyEvent(int mask, int cookie, const QString &path)
     }
 
     _pendingPaths.append(path);
-    slotProcessPaths();
+    setProcessTimer();
 }
 
 void FolderWatcher::slotProcessTimerTimeout()
 {
-    qDebug() << "* Scheduled processing of event queue for" << root();
-    if (!_pendingPaths.empty())
-        slotProcessPaths();
+    qDebug() << "* Processing of event queue for" << root();
+    if (!_pendingPaths.empty()) {
+        QStringList notifyPaths(_pendingPaths);
+        _pendingPaths.clear();
+        //qDebug() << lastEventTime << eventTime;
+        qDebug() << "  * Notify" << notifyPaths.size() << "changed items for" << root();
+        emit folderChanged(notifyPaths);
+    }
 }
 
 void FolderWatcher::setProcessTimer()
 {
     if (!_processTimer->isActive()) {
-        qDebug() << "* Pending events for" << root() << "will be processed in" << eventInterval() << "seconds (" << QTime::currentTime().addSecs(eventInterval()).toString("HH:mm:ss") << ")." << _pendingPaths.size() << "events until now )";
+        qDebug() << "* Pending events for" << root() << "will be processed after events stop for" << eventInterval() << "seconds (" << QTime::currentTime().addSecs(eventInterval()).toString("HH:mm:ss") << ")." << _pendingPaths.size() << "events until now )";
     }
     _processTimer->start(eventInterval() * 1000);
 }
 
-void FolderWatcher::slotProcessPaths()
-{
-    QTime eventTime = QTime::currentTime();
-    QTime lastEventTime = _lastEventTime;
-    _lastEventTime = eventTime;
-
-    // if the events are disabled or the last event happened
-    // recently eg: copying lot of ifles
-    if (!eventsEnabled() ||
-        ( !lastEventTime.isNull() &&
-          (lastEventTime.secsTo(eventTime) < eventInterval()) ))
-    {
-        // in case this is the last file from a bulk copy
-        // set the process timer again so that we process the
-        // queue we are not processing now
-        setProcessTimer();
-        return;
-    }
-
-    QStringList notifyPaths(_pendingPaths);
-    _pendingPaths.clear();
-    //qDebug() << lastEventTime << eventTime;
-
-    qDebug() << "  * Notify" << notifyPaths.size() << "changed items for" << root();
-
-    emit folderChanged(notifyPaths);
-}
-
 }
 
 #include "folderwatcher.moc"

+ 0 - 3
src/mirall/folderwatcher.h

@@ -107,7 +107,6 @@ protected slots:
     void slotAddFolderRecursive(const QString &path);
     // called when the manually process timer triggers
     void slotProcessTimerTimeout();
-    void slotProcessPaths();
 
 private:
     bool _eventsEnabled;
@@ -120,8 +119,6 @@ private:
 
     QTimer *_processTimer;
 
-    QTime _lastEventTime;
-
     // to cancel events that belong to the same action
     int _lastMask;
     QString _lastPath;

+ 28 - 6
test/testfolderwatcher.cpp

@@ -11,34 +11,56 @@
 
 void TestFolderWatcher::initTestCase()
 {
-    Mirall::INotify::initialize();
+
 }
 
 void TestFolderWatcher::cleanupTestCase()
 {
-    Mirall::INotify::cleanup();
 }
 
 void TestFolderWatcher::testFilesAdded()
 {
+    Mirall::INotify::initialize();
     Mirall::TemporaryDir tmp;
     Mirall::FolderWatcher watcher(tmp.path());
 
+    // lower the event interval
+    watcher.setEventInterval(1);
+
     qDebug() << "Monitored: " << watcher.folders();
 
     QDir subdir = QDir(tmp.path());
-    QSignalSpy spy(&watcher, SIGNAL(folderChanged(const QString &)));
+    QSignalSpy spy(&watcher, SIGNAL(folderChanged(const QStringList &)));
 
     QVERIFY(subdir.mkpath(tmp.path() + "/sub1/sub2"));
     QVERIFY(subdir.mkpath(tmp.path() + "/sub2"));
 
      while (spy.count() == 0)
-         QTest::qWait(200);
+         QTest::qWait(1010);
 
-    // 2 directory changes
-    QCOMPARE(spy.count(), 2);
+    // 1 directory changes
+    QCOMPARE(spy.count(), 1);
+    QList<QVariant> arguments = spy.takeFirst();
+    QStringList paths = arguments.at(0).toStringList();
+    qDebug() << paths;
+    QCOMPARE(paths.size(), 2);
 
     qDebug() << "Monitored: " << watcher.folders();
+
+    // the new sub2 directory should be now also bee in the list of watches
+    QFile file(tmp.path() + "/sub1/sub2/foo.txt");
+    file.open(QIODevice::WriteOnly);
+    file.write("hello", 5);
+    file.close();
+
+    //while (spy.count() == )
+    QTest::qWait(1010);
+
+    // 1 file changes
+    QCOMPARE(spy.count(), 1);
+
+
+    Mirall::INotify::cleanup();
 }
 
 QTEST_MAIN(TestFolderWatcher)