Changeset View
Changeset View
Standalone View
Standalone View
autotests/kdirwatch_unittest.cpp
Show First 20 Lines • Show All 110 Lines • ▼ Show 20 Line(s) | 91 | private Q_SLOTS: // test methods | |||
---|---|---|---|---|---|
111 | void testDelete(); | 111 | void testDelete(); | ||
112 | void testDeleteAndRecreateFile(); | 112 | void testDeleteAndRecreateFile(); | ||
113 | void testDeleteAndRecreateDir(); | 113 | void testDeleteAndRecreateDir(); | ||
114 | void testMoveTo(); | 114 | void testMoveTo(); | ||
115 | void nestedEventLoop(); | 115 | void nestedEventLoop(); | ||
116 | void testHardlinkChange(); | 116 | void testHardlinkChange(); | ||
117 | void stopAndRestart(); | 117 | void stopAndRestart(); | ||
118 | void shouldIgnoreQrcPaths(); | 118 | void shouldIgnoreQrcPaths(); | ||
119 | void benchCreateTree(); | ||||
120 | void benchCreateWatcher(); | ||||
121 | void benchNotifyWatcher(); | ||||
119 | 122 | | |||
120 | protected Q_SLOTS: // internal slots | 123 | protected Q_SLOTS: // internal slots | ||
121 | void nestedEventLoopSlot(); | 124 | void nestedEventLoopSlot(); | ||
122 | 125 | | |||
123 | private: | 126 | private: | ||
124 | void waitUntilMTimeChange(const QString &path); | 127 | void waitUntilMTimeChange(const QString &path); | ||
125 | void waitUntilNewSecond(); | 128 | void waitUntilNewSecond(); | ||
126 | void waitUntilAfter(const QDateTime &ctime); | 129 | void waitUntilAfter(const QDateTime &ctime); | ||
127 | QList<QVariantList> waitForDirtySignal(KDirWatch &watch, int expected); | 130 | QList<QVariantList> waitForDirtySignal(KDirWatch &watch, int expected); | ||
128 | QList<QVariantList> waitForDeletedSignal(KDirWatch &watch, int expected); | 131 | QList<QVariantList> waitForDeletedSignal(KDirWatch &watch, int expected); | ||
129 | bool waitForOneSignal(KDirWatch &watch, const char *sig, const QString &path); | 132 | bool waitForOneSignal(KDirWatch &watch, const char *sig, const QString &path); | ||
130 | bool waitForRecreationSignal(KDirWatch &watch, const QString &path); | 133 | bool waitForRecreationSignal(KDirWatch &watch, const QString &path); | ||
131 | bool verifySignalPath(QSignalSpy &spy, const char *sig, const QString &expectedPath); | 134 | bool verifySignalPath(QSignalSpy &spy, const char *sig, const QString &expectedPath); | ||
132 | void createFile(const QString &path); | 135 | void createFile(const QString &path); | ||
133 | QString createFile(int num); | 136 | QString createFile(int num); | ||
134 | void removeFile(int num); | 137 | void removeFile(int num); | ||
135 | void appendToFile(const QString &path); | 138 | void appendToFile(const QString &path); | ||
136 | void appendToFile(int num); | 139 | void appendToFile(int num); | ||
140 | int createDirectoryTree(const QString &path, int depth = 4); | ||||
137 | 141 | | |||
138 | QTemporaryDir m_tempDir; | 142 | QTemporaryDir m_tempDir; | ||
139 | QString m_path; | 143 | QString m_path; | ||
140 | bool m_slow; | 144 | bool m_slow; | ||
141 | }; | 145 | }; | ||
142 | 146 | | |||
143 | QTEST_MAIN(KDirWatch_UnitTest) | 147 | QTEST_MAIN(KDirWatch_UnitTest) | ||
144 | 148 | | |||
Show All 22 Lines | |||||
167 | 171 | | |||
168 | // helper method: delete a file (identified by number) | 172 | // helper method: delete a file (identified by number) | ||
169 | void KDirWatch_UnitTest::removeFile(int num) | 173 | void KDirWatch_UnitTest::removeFile(int num) | ||
170 | { | 174 | { | ||
171 | const QString fileName = QLatin1String(s_filePrefix) + QString::number(num); | 175 | const QString fileName = QLatin1String(s_filePrefix) + QString::number(num); | ||
172 | QFile::remove(m_path + fileName); | 176 | QFile::remove(m_path + fileName); | ||
173 | } | 177 | } | ||
174 | 178 | | |||
179 | int KDirWatch_UnitTest::createDirectoryTree(const QString& basePath, int depth) | ||||
180 | { | ||||
181 | int filesCreated = 0; | ||||
182 | | ||||
183 | const int numFiles = 10; | ||||
184 | for (int i = 0; i < numFiles; ++i) { | ||||
185 | createFile(basePath + QLatin1Char('/') + s_filePrefix + QString::number(i)); | ||||
186 | ++filesCreated; | ||||
187 | } | ||||
188 | | ||||
189 | if (depth <= 0) { | ||||
190 | return filesCreated; | ||||
191 | } | ||||
192 | | ||||
193 | const int numFolders = 5; | ||||
194 | for (int i = 0; i < numFolders; ++i) { | ||||
195 | const QString childPath = basePath + QLatin1String("/subdir") + QString::number(i); | ||||
196 | QDir().mkdir(childPath); | ||||
197 | filesCreated += createDirectoryTree(childPath, depth - 1); | ||||
198 | } | ||||
199 | | ||||
200 | return filesCreated; | ||||
201 | } | ||||
202 | | ||||
175 | void KDirWatch_UnitTest::waitUntilMTimeChange(const QString &path) | 203 | void KDirWatch_UnitTest::waitUntilMTimeChange(const QString &path) | ||
176 | { | 204 | { | ||
177 | // Wait until the current second is more than the file's mtime | 205 | // Wait until the current second is more than the file's mtime | ||
178 | // otherwise this change will go unnoticed | 206 | // otherwise this change will go unnoticed | ||
179 | 207 | | |||
180 | QFileInfo fi(path); | 208 | QFileInfo fi(path); | ||
181 | QVERIFY(fi.exists()); | 209 | QVERIFY(fi.exists()); | ||
182 | const QDateTime ctime = qMax(fi.lastModified(), fi.created()); | 210 | const QDateTime ctime = qMax(fi.lastModified(), fi.created()); | ||
▲ Show 20 Lines • Show All 586 Lines • ▼ Show 20 Line(s) | 783 | { | |||
769 | file.close(); | 797 | file.close(); | ||
770 | QVERIFY(file.exists()); | 798 | QVERIFY(file.exists()); | ||
771 | QVERIFY(dirtySpy.wait()); | 799 | QVERIFY(dirtySpy.wait()); | ||
772 | QVERIFY(dirtySpy.count() > 0); | 800 | QVERIFY(dirtySpy.count() > 0); | ||
773 | QVERIFY(file.remove()); | 801 | QVERIFY(file.remove()); | ||
774 | QVERIFY(QDir::setCurrent(oldCwd)); | 802 | QVERIFY(QDir::setCurrent(oldCwd)); | ||
775 | } | 803 | } | ||
776 | 804 | | |||
805 | void KDirWatch_UnitTest::benchCreateTree() | ||||
806 | { | ||||
807 | QTemporaryDir dir; | ||||
808 | | ||||
809 | QBENCHMARK { | ||||
810 | createDirectoryTree(dir.path()); | ||||
811 | } | ||||
812 | } | ||||
813 | | ||||
814 | void KDirWatch_UnitTest::benchCreateWatcher() | ||||
815 | { | ||||
816 | QTemporaryDir dir; | ||||
817 | createDirectoryTree(dir.path()); | ||||
818 | | ||||
819 | QBENCHMARK { | ||||
820 | KDirWatch watch; | ||||
821 | watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch:: WatchFiles); | ||||
822 | } | ||||
823 | } | ||||
824 | | ||||
825 | void KDirWatch_UnitTest::benchNotifyWatcher() | ||||
826 | { | ||||
827 | QTemporaryDir dir; | ||||
828 | // create the dir once upfront | ||||
829 | auto numFiles = createDirectoryTree(dir.path()); | ||||
830 | waitUntilMTimeChange(dir.path()); | ||||
831 | | ||||
832 | KDirWatch watch; | ||||
833 | watch.addDir(dir.path(), KDirWatch::WatchSubDirs | KDirWatch:: WatchFiles); | ||||
834 | | ||||
835 | // now touch all the files repeatedly and wait for the dirty updates to come in | ||||
836 | QSignalSpy spy(&watch, &KDirWatch::dirty); | ||||
837 | QBENCHMARK { | ||||
838 | createDirectoryTree(dir.path()); | ||||
839 | QTRY_COMPARE_WITH_TIMEOUT(spy.count(), numFiles, s_maxTries * 50); | ||||
840 | spy.clear(); | ||||
841 | } | ||||
842 | } | ||||
843 | | ||||
777 | #include "kdirwatch_unittest.moc" | 844 | #include "kdirwatch_unittest.moc" |