kdelibs/kdecore/tests/klockfiletest.cpp

77 lines
2.1 KiB
C++
Raw Normal View History

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