diff options
Diffstat (limited to 'tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp')
-rw-r--r-- | tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp | 108 |
1 files changed, 49 insertions, 59 deletions
diff --git a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp index 4969e417f4..b7056e20c9 100644 --- a/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp +++ b/tests/auto/corelib/io/qlockfile/tst_qlockfile.cpp @@ -1,46 +1,34 @@ -/**************************************************************************** -** -** Copyright (C) 2013 David Faure <faure+bluesystems@kde.org> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - - -#include <QtTest/QtTest> +// Copyright (C) 2021 The Qt Company Ltd. +// Copyright (C) 2013 David Faure <faure+bluesystems@kde.org> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +#undef QT_NO_FOREACH // this file contains unported legacy Q_FOREACH uses + +#include <QTest> #include <QtConcurrentRun> +#if QT_CONFIG(process) +#include <QProcess> +#endif +#include <QSemaphore> +#include <QFutureSynchronizer> + #include <qlockfile.h> #include <qtemporarydir.h> #include <qsysinfo.h> #if defined(Q_OS_UNIX) && !defined(Q_OS_VXWORKS) #include <unistd.h> + +#include <sys/stat.h> // utimensat #include <sys/time.h> -#elif defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#elif defined(Q_OS_WIN) # include <qt_windows.h> +# include <QOperatingSystemVersion> #endif #include <private/qlockfile_p.h> // for getLockFileHandle() +using namespace std::chrono_literals; + class tst_QLockFile : public QObject { Q_OBJECT @@ -78,7 +66,7 @@ public: void tst_QLockFile::initTestCase() { -#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_EMBEDDED) +#ifdef Q_OS_ANDROID QSKIP("This test requires deploying and running external console applications"); #elif !QT_CONFIG(process) QSKIP("This test requires QProcess support"); @@ -96,6 +84,7 @@ void tst_QLockFile::lockUnlock() const QString fileName = dir.path() + "/lock1"; QVERIFY(!QFile(fileName).exists()); QLockFile lockFile(fileName); + QCOMPARE(lockFile.fileName(), fileName); QVERIFY(lockFile.lock()); QVERIFY(lockFile.isLocked()); QCOMPARE(int(lockFile.error()), int(QLockFile::NoError)); @@ -110,7 +99,7 @@ void tst_QLockFile::lockUnlock() QVERIFY(lockFile.getLockInfo(&pid, &hostname, &appname)); QCOMPARE(pid, QCoreApplication::applicationPid()); QCOMPARE(appname, qAppName()); - QVERIFY(!lockFile.tryLock(200)); + QVERIFY(!lockFile.tryLock(200ms)); QCOMPARE(int(lockFile.error()), int(QLockFile::LockFailedError)); // Unlock deletes the lock file @@ -163,13 +152,13 @@ void tst_QLockFile::lockOutOtherThread() QVERIFY(lockFile.lock()); // Other thread can't acquire lock - QFuture<QLockFile::LockError> ret = QtConcurrent::run<QLockFile::LockError>(tryLockFromThread, fileName); + auto ret = QtConcurrent::run(tryLockFromThread, fileName); QCOMPARE(ret.result(), QLockFile::LockFailedError); lockFile.unlock(); // Now other thread can acquire lock - QFuture<QLockFile::LockError> ret2 = QtConcurrent::run<QLockFile::LockError>(tryLockFromThread, fileName); + auto ret2 = QtConcurrent::run(tryLockFromThread, fileName); QCOMPARE(ret2.result(), QLockFile::NoError); } @@ -186,13 +175,13 @@ static QLockFile::LockError lockFromThread(const QString &fileName) 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); + auto ret = QtConcurrent::run(lockFromThread, fileName); + auto ret2 = QtConcurrent::run(lockFromThread, fileName); QCOMPARE(ret.result(), QLockFile::NoError); QCOMPARE(ret2.result(), QLockFile::NoError); } -static bool lockFromThread(const QString &fileName, int sleepMs, QSemaphore *semThreadReady, QSemaphore *semMainThreadDone) +static bool lockFromThreadAndWait(const QString &fileName, int sleepMs, QSemaphore *semThreadReady, QSemaphore *semMainThreadDone) { QLockFile lockFile(fileName); if (!lockFile.lock()) { @@ -233,7 +222,7 @@ void tst_QLockFile::waitForLock() QLockFile lockFile(fileName); QSemaphore semThreadReady, semMainThreadDone; // Lock file from a thread - QFuture<bool> ret = QtConcurrent::run<bool>(lockFromThread, fileName, threadSleepMs, &semThreadReady, &semMainThreadDone); + auto ret = QtConcurrent::run(lockFromThreadAndWait, fileName, threadSleepMs, &semThreadReady, &semMainThreadDone); semThreadReady.acquire(); if (releaseEarly) // let the thread release the lock after threadSleepMs @@ -248,7 +237,7 @@ void tst_QLockFile::waitForLock() if (!releaseEarly) // only let the thread release the lock now semMainThreadDone.release(); - QVERIFY(ret); // waits for the thread to finish + QVERIFY(ret.result()); // waits for the thread to finish } void tst_QLockFile::staleLockFromCrashedProcess_data() @@ -289,7 +278,7 @@ void tst_QLockFile::staleLockFromCrashedProcessReusedPid() { #if !QT_CONFIG(process) QSKIP("This test requires QProcess support"); -#elif defined(Q_OS_WINRT) || defined(QT_PLATFORM_UIKIT) +#elif defined(QT_PLATFORM_UIKIT) QSKIP("We cannot retrieve information about other processes on this platform."); #else const QString fileName = dir.path() + "/staleLockFromCrashedProcessReusedPid"; @@ -328,7 +317,7 @@ void tst_QLockFile::staleShortLockFromBusyProcess() QString hostname, appname; QTRY_VERIFY(secondLock.getLockInfo(&pid, &hostname, &appname)); #ifdef Q_OS_UNIX - QCOMPARE(pid, proc.pid()); + QCOMPARE(pid, proc.processId()); #endif secondLock.setStaleLockTime(100); @@ -355,8 +344,8 @@ void tst_QLockFile::staleLongLockFromBusyProcess() QTRY_VERIFY(QFile::exists(fileName)); QLockFile secondLock(fileName); - secondLock.setStaleLockTime(0); - QVERIFY(!secondLock.tryLock(100)); // never stale + secondLock.setStaleLockTime(0ms); + QVERIFY(!secondLock.tryLock(100ms)); // never stale QCOMPARE(int(secondLock.error()), int(QLockFile::LockFailedError)); qint64 pid; QTRY_VERIFY(secondLock.getLockInfo(&pid, NULL, NULL)); @@ -410,7 +399,7 @@ void tst_QLockFile::staleLockRace() QThreadPool::globalInstance()->setMaxThreadCount(10); QFutureSynchronizer<QString> synchronizer; for (int i = 0; i < 8; ++i) - synchronizer.addFuture(QtConcurrent::run<QString>(tryStaleLockFromThread, fileName)); + synchronizer.addFuture(QtConcurrent::run(tryStaleLockFromThread, fileName)); synchronizer.waitForFinished(); foreach (const QFuture<QString> &future, synchronizer.futures()) QVERIFY2(future.result().isEmpty(), qPrintable(future.result())); @@ -463,7 +452,7 @@ Q_DECLARE_OPERATORS_FOR_FLAGS(ProcessProperties) static inline ProcessProperties processProperties() { ProcessProperties result; -#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT) +#if defined(Q_OS_WIN) HANDLE processToken = NULL; if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &processToken)) { DWORD elevation; // struct containing a DWORD, not present in some MinGW headers. @@ -492,7 +481,7 @@ void tst_QLockFile::noPermissionsWindows() { // Windows: Do the permissions test in a system directory in which // files cannot be created. -#if !defined(Q_OS_WIN) || defined(Q_OS_WINRT) +#if !defined(Q_OS_WIN) QSKIP("This test is for desktop Windows only"); #endif #ifdef Q_OS_WIN @@ -524,15 +513,15 @@ void tst_QLockFile::corruptedLockFile() } QLockFile secondLock(fileName); - secondLock.setStaleLockTime(100); - QVERIFY(secondLock.tryLock(10000)); + secondLock.setStaleLockTime(100ms); + QVERIFY(secondLock.tryLock(10s)); QCOMPARE(int(secondLock.error()), int(QLockFile::NoError)); } void tst_QLockFile::corruptedLockFileInTheFuture() { -#if !defined(Q_OS_UNIX) - QSKIP("This tests needs utimes"); +#if !defined(Q_OS_UNIX) || defined(Q_OS_VXWORKS) + QSKIP("This test needs utimensat"); #else // This test is the same as the previous one, but the corruption was so there is a corrupted // .rmlock whose timestamp is in the future @@ -544,11 +533,12 @@ void tst_QLockFile::corruptedLockFileInTheFuture() QVERIFY(file.open(QFile::WriteOnly)); } - struct timeval times[2]; - gettimeofday(times, 0); - times[1].tv_sec = (times[0].tv_sec += 600); - times[1].tv_usec = times[0].tv_usec; - utimes(fileName.toLocal8Bit(), times); + struct timespec times[2]; + clock_gettime(CLOCK_REALTIME, times); + times[0].tv_sec += 600; + times[1].tv_sec = times[0].tv_sec; + times[1].tv_nsec = times[0].tv_nsec; + utimensat(0 /* ignored */, fileName.toLocal8Bit(), times, 0); QTest::ignoreMessage(QtInfoMsg, "QLockFile: Lock file '" + fileName.toUtf8() + "' has a modification time in the future"); corruptedLockFile(); @@ -577,7 +567,7 @@ void tst_QLockFile::hostnameChange() { // we should fail to lock QLockFile lock2(lockFile); - QVERIFY(!lock2.tryLock(1000)); + QVERIFY(!lock2.tryLock(1s)); } } @@ -604,7 +594,7 @@ void tst_QLockFile::differentMachines() { // we should fail to lock QLockFile lock2(lockFile); - QVERIFY(!lock2.tryLock(1000)); + QVERIFY(!lock2.tryLock(1s)); } } @@ -633,7 +623,7 @@ void tst_QLockFile::reboot() f.close(); // we should succeed in locking - QVERIFY(lock1.tryLock(0)); + QVERIFY(lock1.tryLock(0ms)); } bool tst_QLockFile::overwritePidInLockFile(const QString &filePath, qint64 pid) |