kdelibs/kdecore/tests/klockfiletest.cpp
Ivailo Monev 95a8be4470 kdecore: rewrite KLockFile
the PID check was racy, bonus points for not writing data at all to the
lock and not reading it meaning less disk I/O

oh, yes - by using O_CLOEXEC the lock is stale-safe

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
2023-09-09 00:46:42 +03:00

76 lines
2.1 KiB
C++

/* 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"
#include "moc_klockfiletest.cpp"
#include <kdebug.h>
#include <QProcess>
#include <QTextStream>
#include <unistd.h>
// TODO test locking from two different threads
static const QString lockName = "klockfiletest";
static const char *const lockNameFull = "klockfiletest.klockfile";
void Test_KLockFile::initTestCase()
{
QFile::remove(QString::fromLatin1(lockNameFull));
lockFile = new KLockFile(lockName);
}
void Test_KLockFile::cleanupTestCase()
{
delete lockFile;
lockFile = nullptr;
}
static bool testLockFromProcess(const QString& lockName)
{
const int ret = QProcess::execute(KDEBINDIR "/kdecore-klockfile_testlock", QStringList() << lockName);
return bool(ret);
}
void Test_KLockFile::testLock()
{
QVERIFY(!lockFile->isLocked());
QCOMPARE(lockFile->tryLock(), true);
QVERIFY(lockFile->isLocked());
// Try to lock it again, should fail
KLockFile *lockFile2 = new KLockFile(lockName);
QVERIFY(!lockFile2->isLocked());
QCOMPARE(lockFile2->tryLock(), false);
QVERIFY(!lockFile2->isLocked());
delete lockFile2;
// Also try from a different process.
QCOMPARE(testLockFromProcess(lockName), false);
}
void Test_KLockFile::testUnlock()
{
QVERIFY(lockFile->isLocked());
lockFile->unlock();
QVERIFY(!lockFile->isLocked());
}
QTEST_KDEMAIN_CORE(Test_KLockFile)