From 1f2ce78f16a3a8b5d182fbb9c03c373efaa136d8 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 17 Apr 2015 15:29:20 +0200 Subject: tst_QFileSystemWatcher: Improve diagnostics. Add a special signal spy that records the time and path received to obtain information on what triggered it for the flaky cases where more than the expected signals were emitted. Task-number: QTBUG-30943 Change-Id: I67510ce5e8e19b49c0ca41457f8357b720cade76 Reviewed-by: Simon Hausmann --- .../qfilesystemwatcher/tst_qfilesystemwatcher.cpp | 74 ++++++++++++++++++++-- 1 file changed, 67 insertions(+), 7 deletions(-) (limited to 'tests/auto/corelib/io') diff --git a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp index 458dbc532d..0ebfd2ae35 100644 --- a/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp +++ b/tests/auto/corelib/io/qfilesystemwatcher/tst_qfilesystemwatcher.cpp @@ -36,6 +36,9 @@ #include #include +#include +#include +#include /* All tests need to run in temporary directories not used * by the application to avoid non-deterministic failures on Windows @@ -387,6 +390,60 @@ void tst_QFileSystemWatcher::addPaths() QCOMPARE(watcher.addPaths(paths), QStringList()); } +// A signal spy that records the paths and times received for better diagnostics. +class FileSystemWatcherSpy : public QObject { + Q_OBJECT +public: + enum Mode { + SpyOnDirectoryChanged, + SpyOnFileChanged + }; + + explicit FileSystemWatcherSpy(QFileSystemWatcher *watcher, Mode mode) + { + connect(watcher, mode == SpyOnDirectoryChanged ? + &QFileSystemWatcher::directoryChanged : &QFileSystemWatcher::fileChanged, + this, &FileSystemWatcherSpy::spySlot); + m_elapsedTimer.start(); + } + + int count() const { return m_entries.size(); } + void clear() + { + m_entries.clear(); + m_elapsedTimer.restart(); + } + + QByteArray receivedFilesMessage() const + { + QString result; + QTextStream str(&result); + str << "At " << m_elapsedTimer.elapsed() << "ms, received " + << count() << " changes: "; + for (int i =0, e = m_entries.size(); i < e; ++i) { + if (i) + str << ", "; + str << m_entries.at(i).timeStamp << "ms: " << QDir::toNativeSeparators(m_entries.at(i).path); + } + return result.toLocal8Bit(); + } + +private slots: + void spySlot(const QString &p) { m_entries.append(Entry(m_elapsedTimer.elapsed(), p)); } + +private: + struct Entry { + Entry() : timeStamp(0) {} + Entry(qint64 t, const QString &p) : timeStamp(t), path(p) {} + + qint64 timeStamp; + QString path; + }; + + QElapsedTimer m_elapsedTimer; + QList m_entries; +}; + void tst_QFileSystemWatcher::removePaths() { QFileSystemWatcher watcher; @@ -438,9 +495,8 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() QVERIFY(watcher.addPath(testFileName)); QSignalSpy fileChangedSpy(&watcher, &QFileSystemWatcher::fileChanged); - QSignalSpy dirChangedSpy(&watcher, &QFileSystemWatcher::directoryChanged); + FileSystemWatcherSpy dirChangedSpy(&watcher, FileSystemWatcherSpy::SpyOnDirectoryChanged); QVERIFY(fileChangedSpy.isValid()); - QVERIFY(dirChangedSpy.isValid()); QEventLoop eventLoop; QTimer timer; connect(&timer, SIGNAL(timeout()), &eventLoop, SLOT(quit())); @@ -460,7 +516,7 @@ void tst_QFileSystemWatcher::watchFileAndItsDirectory() #endif QTRY_VERIFY(fileChangedSpy.count() > 0); - QCOMPARE(dirChangedSpy.count(), 0); + QVERIFY2(dirChangedSpy.count() == 0, dirChangedSpy.receivedFilesMessage()); fileChangedSpy.clear(); QFile secondFile(secondFileName); @@ -640,13 +696,14 @@ private: // emitted with the destination path instead of the starting path void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved() { + const int fileCount = 10; QTemporaryDir temporaryDirectory(m_tempDirPattern); QVERIFY(temporaryDirectory.isValid()); QDir testDir(temporaryDirectory.path()); QVERIFY(testDir.mkdir("movehere")); QString movePath = testDir.filePath("movehere"); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < fileCount; ++i) { QFile f(testDir.filePath(QString("test%1.txt").arg(i))); QVERIFY(f.open(QIODevice::WriteOnly)); f.write(QByteArray("i am ") + QByteArray::number(i)); @@ -659,6 +716,7 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved() // add files to watcher QFileInfoList files = testDir.entryInfoList(QDir::Files | QDir::NoSymLinks); + QCOMPARE(files.size(), fileCount); foreach (const QFileInfo &finfo, files) QVERIFY(watcher.addPath(finfo.absoluteFilePath())); @@ -667,14 +725,16 @@ void tst_QFileSystemWatcher::signalsEmittedAfterFileMoved() connect(&watcher, SIGNAL(fileChanged(QString)), &signalReceiver, SLOT(fileChanged(QString))); // watch signals - QSignalSpy changedSpy(&watcher, &QFileSystemWatcher::fileChanged); - QVERIFY(changedSpy.isValid()); + FileSystemWatcherSpy changedSpy(&watcher, FileSystemWatcherSpy::SpyOnFileChanged); + QCOMPARE(changedSpy.count(), 0); // move files to second directory foreach (const QFileInfo &finfo, files) QVERIFY(testDir.rename(finfo.fileName(), QString("movehere/%2").arg(finfo.fileName()))); - QTRY_COMPARE(changedSpy.count(), 10); + QCoreApplication::processEvents(); + QVERIFY2(changedSpy.count() <= fileCount, changedSpy.receivedFilesMessage()); + QTRY_COMPARE(changedSpy.count(), fileCount); } #endif // QT_NO_FILESYSTEMWATCHER -- cgit v1.2.3