2014-11-13 01:04:59 +02:00
|
|
|
/* This file is part of the KDE libraries
|
|
|
|
Copyright (c) 2005 Thomas Braxton <brax108@cox.net>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public
|
|
|
|
License version 2 as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public License
|
|
|
|
along with this library; see the file COPYING.LIB. If not, write to
|
|
|
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "klockfiletest.h"
|
2015-02-27 07:40:26 +00:00
|
|
|
#include "moc_klockfiletest.cpp"
|
2014-11-13 01:04:59 +02:00
|
|
|
|
|
|
|
#include <kdebug.h>
|
|
|
|
|
2019-06-21 19:56:58 +00:00
|
|
|
#include <QProcess>
|
2023-07-06 09:06:18 +03:00
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
2015-08-11 05:56:07 +03:00
|
|
|
|
2014-11-13 01:04:59 +02:00
|
|
|
// TODO test locking from two different threads
|
|
|
|
|
2022-10-08 03:28:20 +03:00
|
|
|
static const QString lockName = "klockfiletest";
|
|
|
|
static const char *const lockNameFull = "klockfiletest.klockfile";
|
2014-11-13 01:04:59 +02:00
|
|
|
|
2022-10-08 17:49:43 +03:00
|
|
|
void Test_KLockFile::initTestCase()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2022-10-08 17:49:43 +03:00
|
|
|
QFile::remove(QString::fromLatin1(lockNameFull));
|
2022-10-08 03:28:20 +03:00
|
|
|
lockFile = new KLockFile(lockName);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:49:43 +03:00
|
|
|
void Test_KLockFile::cleanupTestCase()
|
2021-09-03 21:18:08 +03:00
|
|
|
{
|
|
|
|
delete lockFile;
|
|
|
|
lockFile = nullptr;
|
|
|
|
}
|
|
|
|
|
2023-09-09 00:46:42 +03:00
|
|
|
static bool testLockFromProcess(const QString& lockName)
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2015-11-08 18:25:09 +02:00
|
|
|
const int ret = QProcess::execute(KDEBINDIR "/kdecore-klockfile_testlock", QStringList() << lockName);
|
2023-09-09 00:46:42 +03:00
|
|
|
return bool(ret);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:49:43 +03:00
|
|
|
void Test_KLockFile::testLock()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2015-10-01 07:46:47 +03:00
|
|
|
QVERIFY(!lockFile->isLocked());
|
2023-09-09 00:46:42 +03:00
|
|
|
QCOMPARE(lockFile->tryLock(), true);
|
2015-10-01 07:46:47 +03:00
|
|
|
QVERIFY(lockFile->isLocked());
|
|
|
|
|
|
|
|
// Try to lock it again, should fail
|
2022-10-08 03:28:20 +03:00
|
|
|
KLockFile *lockFile2 = new KLockFile(lockName);
|
2015-10-01 07:46:47 +03:00
|
|
|
QVERIFY(!lockFile2->isLocked());
|
2023-09-09 00:46:42 +03:00
|
|
|
QCOMPARE(lockFile2->tryLock(), false);
|
2015-10-01 07:46:47 +03:00
|
|
|
QVERIFY(!lockFile2->isLocked());
|
|
|
|
delete lockFile2;
|
|
|
|
|
|
|
|
// Also try from a different process.
|
2023-09-09 00:46:42 +03:00
|
|
|
QCOMPARE(testLockFromProcess(lockName), false);
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
2022-10-08 17:49:43 +03:00
|
|
|
void Test_KLockFile::testUnlock()
|
2014-11-13 01:04:59 +02:00
|
|
|
{
|
2015-10-01 07:46:47 +03:00
|
|
|
QVERIFY(lockFile->isLocked());
|
|
|
|
lockFile->unlock();
|
|
|
|
QVERIFY(!lockFile->isLocked());
|
2014-11-13 01:04:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QTEST_KDEMAIN_CORE(Test_KLockFile)
|