From 8f62bb343bf48731efdfe1ee27eca18a5156a991 Mon Sep 17 00:00:00 2001 From: Oliver Wolff Date: Wed, 31 Oct 2012 10:27:33 +0100 Subject: move QSystemSemaphore autotest from qtscript to qtbase As the script dependency for that autotest is not really needed it should be moved to qtbase. Task-number: QTBUG-27705 Change-Id: I4ce0d34aca97cadd79b157b0f7c90c406bed4295 Reviewed-by: Joerg Bornemann --- .../kernel/qsystemsemaphore/qsystemsemaphore.pro | 3 + .../systemsemaphorehelper/main.cpp | 114 +++++++++ .../systemsemaphorehelper.pro | 9 + .../corelib/kernel/qsystemsemaphore/test/test.pro | 10 + .../qsystemsemaphore/test/tst_qsystemsemaphore.cpp | 275 +++++++++++++++++++++ 5 files changed, 411 insertions(+) create mode 100644 tests/auto/corelib/kernel/qsystemsemaphore/qsystemsemaphore.pro create mode 100644 tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/main.cpp create mode 100644 tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/systemsemaphorehelper.pro create mode 100644 tests/auto/corelib/kernel/qsystemsemaphore/test/test.pro create mode 100644 tests/auto/corelib/kernel/qsystemsemaphore/test/tst_qsystemsemaphore.cpp (limited to 'tests/auto/corelib/kernel/qsystemsemaphore') diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/qsystemsemaphore.pro b/tests/auto/corelib/kernel/qsystemsemaphore/qsystemsemaphore.pro new file mode 100644 index 0000000000..f8a49254d2 --- /dev/null +++ b/tests/auto/corelib/kernel/qsystemsemaphore/qsystemsemaphore.pro @@ -0,0 +1,3 @@ +TEMPLATE = subdirs + +SUBDIRS = systemsemaphorehelper test diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/main.cpp b/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/main.cpp new file mode 100644 index 0000000000..2e85d8610e --- /dev/null +++ b/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/main.cpp @@ -0,0 +1,114 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +int acquire(int count = 1) +{ + QSystemSemaphore sem("store"); + + for (int i = 0; i < count; ++i) { + if (!sem.acquire()) { + qWarning() << "Could not acquire" << sem.key(); + return EXIT_FAILURE; + } + } + qDebug("done aquiring"); + return EXIT_SUCCESS; +} + +int release() +{ + QSystemSemaphore sem("store"); + if (!sem.release()) { + qWarning() << "Could not release" << sem.key(); + return EXIT_FAILURE; + } + qDebug("done releasing"); + return EXIT_SUCCESS; +} + +int acquirerelease() +{ + QSystemSemaphore sem("store"); + if (!sem.acquire()) { + qWarning() << "Could not acquire" << sem.key(); + return EXIT_FAILURE; + } + if (!sem.release()) { + qWarning() << "Could not release" << sem.key(); + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} + +int main(int argc, char *argv[]) +{ + QCoreApplication app(argc, argv); + + QStringList arguments = app.arguments(); + // binary name is not used here + arguments.takeFirst(); + if (arguments.count() < 1) { + qWarning("Please call the helper with the function to call as argument"); + return EXIT_FAILURE; + } + QString function = arguments.takeFirst(); + if (function == QLatin1String("acquire")) { + int count = 1; + bool ok = true; + if (arguments.count()) + count = arguments.takeFirst().toInt(&ok); + if (!ok) + count = 1; + return acquire(count); + } else if (function == QLatin1String("release")) { + return release(); + } else if (function == QLatin1String("acquirerelease")) { + return acquirerelease(); + } else { + qWarning() << "Unknown function" << function; + } + return EXIT_SUCCESS; +} diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/systemsemaphorehelper.pro b/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/systemsemaphorehelper.pro new file mode 100644 index 0000000000..d1a4e04567 --- /dev/null +++ b/tests/auto/corelib/kernel/qsystemsemaphore/systemsemaphorehelper/systemsemaphorehelper.pro @@ -0,0 +1,9 @@ +QT = core testlib + +DESTDIR = ./ + +win32: CONFIG += console +mac:CONFIG -= app_bundle + +SOURCES += main.cpp + diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/test/test.pro b/tests/auto/corelib/kernel/qsystemsemaphore/test/test.pro new file mode 100644 index 0000000000..cc76b2c233 --- /dev/null +++ b/tests/auto/corelib/kernel/qsystemsemaphore/test/test.pro @@ -0,0 +1,10 @@ +CONFIG += testcase +QT = core testlib + +win32: CONFIG += console +mac:CONFIG -= app_bundle + +SOURCES += tst_qsystemsemaphore.cpp +TARGET = tst_qsystemsemaphore + +DESTDIR = ../ diff --git a/tests/auto/corelib/kernel/qsystemsemaphore/test/tst_qsystemsemaphore.cpp b/tests/auto/corelib/kernel/qsystemsemaphore/test/tst_qsystemsemaphore.cpp new file mode 100644 index 0000000000..85f7d2a4b2 --- /dev/null +++ b/tests/auto/corelib/kernel/qsystemsemaphore/test/tst_qsystemsemaphore.cpp @@ -0,0 +1,275 @@ +/**************************************************************************** +** +** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include + +#define EXISTING_SHARE "existing" +#define HELPERWAITTIME 10000 + +class tst_QSystemSemaphore : public QObject +{ + Q_OBJECT + +public: + tst_QSystemSemaphore(); + +public Q_SLOTS: + void initTestCase(); + void init(); + void cleanup(); + +private slots: + void key_data(); + void key(); + + void basicacquire(); + void complexacquire(); + + void basicProcesses(); + + void processes_data(); + void processes(); + +#ifndef Q_OS_WIN + void undo(); +#endif + void initialValue(); + +private: + QString helperBinary(); + QSystemSemaphore *existingLock; +}; + +tst_QSystemSemaphore::tst_QSystemSemaphore() +{ +} + +void tst_QSystemSemaphore::initTestCase() +{ + QVERIFY2(!helperBinary().isEmpty(), "Could not find helper binary"); +} + +void tst_QSystemSemaphore::init() +{ + existingLock = new QSystemSemaphore(EXISTING_SHARE, 1, QSystemSemaphore::Create); +} + +void tst_QSystemSemaphore::cleanup() +{ + delete existingLock; +} + +void tst_QSystemSemaphore::key_data() +{ + QTest::addColumn("constructorKey"); + QTest::addColumn("setKey"); + + QTest::newRow("null, null") << QString() << QString(); + QTest::newRow("null, one") << QString() << QString("one"); + QTest::newRow("one, two") << QString("one") << QString("two"); +} + +/*! + Basic key testing + */ +void tst_QSystemSemaphore::key() +{ + QFETCH(QString, constructorKey); + QFETCH(QString, setKey); + + QSystemSemaphore sem(constructorKey); + QCOMPARE(sem.key(), constructorKey); + QCOMPARE(sem.error(), QSystemSemaphore::NoError); + QCOMPARE(sem.errorString(), QString()); + + sem.setKey(setKey); + QCOMPARE(sem.key(), setKey); + QCOMPARE(sem.error(), QSystemSemaphore::NoError); + QCOMPARE(sem.errorString(), QString()); +} + +void tst_QSystemSemaphore::basicacquire() +{ + QSystemSemaphore sem("QSystemSemaphore_basicacquire", 1, QSystemSemaphore::Create); + QVERIFY(sem.acquire()); + QCOMPARE(sem.error(), QSystemSemaphore::NoError); + QVERIFY(sem.release()); + QCOMPARE(sem.error(), QSystemSemaphore::NoError); + QCOMPARE(sem.errorString(), QString()); +} + +void tst_QSystemSemaphore::complexacquire() +{ + QSystemSemaphore sem("QSystemSemaphore_complexacquire", 2, QSystemSemaphore::Create); + QVERIFY(sem.acquire()); + QVERIFY(sem.release()); + QVERIFY(sem.acquire()); + QVERIFY(sem.release()); + QVERIFY(sem.acquire()); + QVERIFY(sem.acquire()); + QVERIFY(sem.release()); + QVERIFY(sem.release()); + QCOMPARE(sem.error(), QSystemSemaphore::NoError); + QCOMPARE(sem.errorString(), QString()); +} + +void tst_QSystemSemaphore::basicProcesses() +{ + QSystemSemaphore sem("store", 0, QSystemSemaphore::Create); + + QProcess acquire; + acquire.setProcessChannelMode(QProcess::ForwardedChannels); + + QProcess release; + release.setProcessChannelMode(QProcess::ForwardedChannels); + + acquire.start(helperBinary(), QStringList("acquire")); + QVERIFY2(acquire.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state() == QProcess::Running); + acquire.kill(); + release.start(helperBinary(), QStringList("release")); + QVERIFY2(release.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + release.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state() == QProcess::NotRunning); +} + +void tst_QSystemSemaphore::processes_data() +{ + QTest::addColumn("processes"); + for (int i = 0; i < 5; ++i) { + QTest::newRow("1 process") << 1; + QTest::newRow("3 process") << 3; + QTest::newRow("10 process") << 10; + } +} + +void tst_QSystemSemaphore::processes() +{ + QSystemSemaphore sem("store", 1, QSystemSemaphore::Create); + + QFETCH(int, processes); + QVector scripts(processes, "acquirerelease"); + + QList consumers; + for (int i = 0; i < scripts.count(); ++i) { + QProcess *p = new QProcess; + p->setProcessChannelMode(QProcess::ForwardedChannels); + consumers.append(p); + p->start(helperBinary(), QStringList(scripts.at(i))); + } + + while (!consumers.isEmpty()) { + consumers.first()->waitForFinished(); + QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit); + QCOMPARE(consumers.first()->exitCode(), 0); + delete consumers.takeFirst(); + } +} + +// This test only checks a unix behavior. +#ifndef Q_OS_WIN +void tst_QSystemSemaphore::undo() +{ + QSystemSemaphore sem("store", 1, QSystemSemaphore::Create); + + QStringList acquireArguments = QStringList("acquire"); + QProcess acquire; + acquire.setProcessChannelMode(QProcess::ForwardedChannels); + acquire.start(helperBinary(), acquireArguments); + QVERIFY2(acquire.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state()== QProcess::NotRunning); + + // At process exit the kernel should auto undo + + acquire.start(helperBinary(), acquireArguments); + QVERIFY2(acquire.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state()== QProcess::NotRunning); +} +#endif + +void tst_QSystemSemaphore::initialValue() +{ + QSystemSemaphore sem("store", 1, QSystemSemaphore::Create); + + QStringList acquireArguments = QStringList("acquire"); + QStringList releaseArguments = QStringList("release"); + QProcess acquire; + acquire.setProcessChannelMode(QProcess::ForwardedChannels); + + QProcess release; + release.setProcessChannelMode(QProcess::ForwardedChannels); + + acquire.start(helperBinary(), acquireArguments); + QVERIFY2(acquire.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state()== QProcess::NotRunning); + + acquire.start(helperBinary(), acquireArguments << QLatin1String("2")); + QVERIFY2(acquire.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state()== QProcess::Running); + acquire.kill(); + + release.start(helperBinary(), releaseArguments); + QVERIFY2(release.waitForStarted(), "Could not start helper binary"); + acquire.waitForFinished(HELPERWAITTIME); + release.waitForFinished(HELPERWAITTIME); + QVERIFY(acquire.state()== QProcess::NotRunning); +} + +QString tst_QSystemSemaphore::helperBinary() +{ + QString binary = QStringLiteral("systemsemaphorehelper/systemsemaphorehelper"); +#ifdef Q_OS_WIN + binary += QStringLiteral(".exe"); +#endif + return binary; +} +QTEST_MAIN(tst_QSystemSemaphore) +#include "tst_qsystemsemaphore.moc" + -- cgit v1.2.3