summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2014-09-04 16:29:25 +0200
committerDavid Faure <david.faure@kdab.com>2014-09-11 00:17:33 +0200
commit7b9f7f3891230ddd2b2ef5cf3c760a605a02ee96 (patch)
treefb4ff47e82c6c24371a88fff04db1b256d1351b0 /tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
parent92404c9cdb195b0ce161b6d096711def9162745d (diff)
QLockFile: on Windows, retry deleting the lock file if it is being read.
A "sharing violation" happens when trying to delete the lock file and another thread/process is reading it at that very moment. Detect the error and try again, up to 10000 times - to avoid an infinite loop if QFile::remove fails for another reason such as a sudden change of permissions preventing us from deleting our own lock file. On Unix the deletion can't fail because of readers, but it doesn't hurt to check the return value there too, to catch other reasons for failures such as a sudden permission change. Task-number: QTBUG-38853 Change-Id: Icf12a74faed4a4916e3427abc09d9c33aa141476 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp')
-rw-r--r--tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
index bd9e28beb5..de5d641c57 100644
--- a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
+++ b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp
@@ -57,6 +57,7 @@ private slots:
void lockUnlock();
void lockOutOtherProcess();
void lockOutOtherThread();
+ void raceWithOtherThread();
void waitForLock_data();
void waitForLock();
void staleLockFromCrashedProcess_data();
@@ -165,6 +166,25 @@ void tst_QLockFile::lockOutOtherThread()
QCOMPARE(ret2.result(), QLockFile::NoError);
}
+static QLockFile::LockError lockFromThread(const QString &fileName)
+{
+ QLockFile lockInThread(fileName);
+ lockInThread.lock();
+ return lockInThread.error();
+}
+
+// QTBUG-38853, best way to trigger it was to add a QThread::sleep(1) in QLockFilePrivate::getLockInfo() after the first readLine.
+// Then (on Windows), the QFile::remove() in unlock() (called by the first thread who got the lock, in the destructor)
+// would fail due to the existing reader on the file. Fixed by checking the return value of QFile::remove() in unlock().
+void tst_QLockFile::raceWithOtherThread()
+{
+ const QString fileName = dir.path() + "/raceWithOtherThread";
+ QFuture<QLockFile::LockError> ret = QtConcurrent::run<QLockFile::LockError>(lockFromThread, fileName);
+ QFuture<QLockFile::LockError> ret2 = QtConcurrent::run<QLockFile::LockError>(lockFromThread, fileName);
+ QCOMPARE(ret.result(), QLockFile::NoError);
+ QCOMPARE(ret2.result(), QLockFile::NoError);
+}
+
static bool lockFromThread(const QString &fileName, int sleepMs, QSemaphore *semThreadReady, QSemaphore *semMainThreadDone)
{
QLockFile lockFile(fileName);