summaryrefslogtreecommitdiffstats
path: root/tests/auto/qtipc/qsystemsemaphore
diff options
context:
space:
mode:
authorQt by Nokia <qt-info@nokia.com>2011-04-27 12:05:43 +0200
committeraxis <qt-info@nokia.com>2011-04-27 12:05:43 +0200
commit38be0d13830efd2d98281c645c3a60afe05ffece (patch)
tree6ea73f3ec77f7d153333779883e8120f82820abe /tests/auto/qtipc/qsystemsemaphore
Initial import from the monolithic Qt.
This is the beginning of revision history for this module. If you want to look at revision history older than this, please refer to the Qt Git wiki for how to use Git history grafting. At the time of writing, this wiki is located here: http://qt.gitorious.org/qt/pages/GitIntroductionWithQt If you have already performed the grafting and you don't see any history beyond this commit, try running "git log" with the "--follow" argument. Branched from the monolithic repo, Qt master branch, at commit 896db169ea224deb96c59ce8af800d019de63f12
Diffstat (limited to 'tests/auto/qtipc/qsystemsemaphore')
-rw-r--r--tests/auto/qtipc/qsystemsemaphore/.gitignore1
-rw-r--r--tests/auto/qtipc/qsystemsemaphore/files.qrc7
-rw-r--r--tests/auto/qtipc/qsystemsemaphore/qsystemsemaphore.pro38
-rw-r--r--tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp319
4 files changed, 365 insertions, 0 deletions
diff --git a/tests/auto/qtipc/qsystemsemaphore/.gitignore b/tests/auto/qtipc/qsystemsemaphore/.gitignore
new file mode 100644
index 0000000000..2b60bfb3e6
--- /dev/null
+++ b/tests/auto/qtipc/qsystemsemaphore/.gitignore
@@ -0,0 +1 @@
+tst_qsystemsemaphore
diff --git a/tests/auto/qtipc/qsystemsemaphore/files.qrc b/tests/auto/qtipc/qsystemsemaphore/files.qrc
new file mode 100644
index 0000000000..62966903e8
--- /dev/null
+++ b/tests/auto/qtipc/qsystemsemaphore/files.qrc
@@ -0,0 +1,7 @@
+<!DOCTYPE RCC><RCC version="1.0">
+<qresource>
+ <file alias="systemsemaphore_acquire.js">../lackey/scripts/systemsemaphore_acquire.js</file>
+ <file alias="systemsemaphore_release.js">../lackey/scripts/systemsemaphore_release.js</file>
+ <file alias="systemsemaphore_acquirerelease.js">../lackey/scripts/systemsemaphore_acquirerelease.js</file>
+</qresource>
+</RCC>
diff --git a/tests/auto/qtipc/qsystemsemaphore/qsystemsemaphore.pro b/tests/auto/qtipc/qsystemsemaphore/qsystemsemaphore.pro
new file mode 100644
index 0000000000..e05b6b878b
--- /dev/null
+++ b/tests/auto/qtipc/qsystemsemaphore/qsystemsemaphore.pro
@@ -0,0 +1,38 @@
+load(qttest_p4)
+#QT = core
+
+include(../qsharedmemory/src/src.pri)
+win32: CONFIG += console
+
+DEFINES += QSHAREDMEMORY_DEBUG
+DEFINES += QSYSTEMSEMAPHORE_DEBUG
+
+SOURCES += tst_qsystemsemaphore.cpp
+TARGET = tst_qsystemsemaphore
+
+RESOURCES += files.qrc
+
+wince*: {
+requires(contains(QT_CONFIG,script))
+# this test calls lackey, which then again depends on QtScript.
+# let's add it here so that it gets deployed easily
+QT += script
+lackey.files = $$OUT_PWD/../lackey/lackey.exe ../lackey/scripts
+lackey.path = .
+DEPLOYMENT += lackey
+}
+
+symbian: {
+requires(contains(QT_CONFIG,script))
+# this test calls lackey, which then again depends on QtScript.
+# let's add it here so that it gets deployed easily
+QT += script
+
+lackey.files = ../lackey/lackey.exe
+lackey.path = /sys/bin
+DEPLOYMENT += lackey
+
+# PowerMgmt capability needed to kill lackey process
+TARGET.CAPABILITY = PowerMgmt
+}
+
diff --git a/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp b/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp
new file mode 100644
index 0000000000..5cc283b368
--- /dev/null
+++ b/tests/auto/qtipc/qsystemsemaphore/tst_qsystemsemaphore.cpp
@@ -0,0 +1,319 @@
+/****************************************************************************
+**
+** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** 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, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+
+#include <QtTest/QtTest>
+#include <qsystemsemaphore.h>
+//TESTED_CLASS=
+//TESTED_FILES=
+
+#define EXISTING_SHARE "existing"
+#ifdef Q_OS_WINCE
+#define LACKEYLOC "."
+#else
+#define LACKEYLOC "../lackey"
+#endif
+#define LACKYWAITTIME 10000
+
+class tst_QSystemSemaphore : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QSystemSemaphore();
+ virtual ~tst_QSystemSemaphore();
+
+public Q_SLOTS:
+ void init();
+ void cleanup();
+
+private slots:
+ void key_data();
+ void key();
+
+ void basicacquire();
+ void complexacquire();
+
+ void basicProcesses();
+
+ void processes_data();
+ void processes();
+
+ void undo();
+ void initialValue();
+
+private:
+ QSystemSemaphore *existingLock;
+
+ QString makeFile(const QString &resource)
+ {
+ QFile memory(resource);
+ if (!memory.open(QIODevice::ReadOnly)) {
+ qDebug() << "error reading resource" << resource;
+ return QString();
+ }
+ QTemporaryFile *file = new QTemporaryFile;
+ file->open();
+ file->write(memory.readAll());
+ tempFiles.append(file);
+ file->flush();
+#ifdef Q_OS_WINCE
+ // flush does not flush to disk on Windows CE. It flushes it into its application
+ // cache. Thus we need to close the file to be able that other processes(lackey) can read it
+ QString fileName = file->fileName();
+ file->close();
+ return fileName;
+#endif
+ return file->fileName();
+ }
+
+ QString acquire_js() { return makeFile(":/systemsemaphore_acquire.js"); }
+ QString release_js() { return makeFile(":/systemsemaphore_release.js"); }
+ QString acquirerelease_js() { return makeFile(":/systemsemaphore_acquirerelease.js"); }
+ QList<QTemporaryFile*> tempFiles;
+};
+
+tst_QSystemSemaphore::tst_QSystemSemaphore()
+{
+ if (!QFile::exists(LACKEYLOC "/lackey"))
+ qWarning() << "lackey executable doesn't exists!";
+}
+
+tst_QSystemSemaphore::~tst_QSystemSemaphore()
+{
+ qDeleteAll(tempFiles);
+}
+
+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<QString>("constructorKey");
+ QTest::addColumn<QString>("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()
+{
+#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
+ QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
+#endif
+ QSystemSemaphore sem("store", 0, QSystemSemaphore::Create);
+
+ QStringList acquireArguments = QStringList() << acquire_js();
+ QStringList releaseArguments = QStringList() << release_js();
+ QProcess acquire;
+ acquire.setProcessChannelMode(QProcess::ForwardedChannels);
+
+ QProcess release;
+ release.setProcessChannelMode(QProcess::ForwardedChannels);
+
+ acquire.start(LACKEYLOC "/lackey", acquireArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state() == QProcess::Running);
+ acquire.kill();
+ release.start(LACKEYLOC "/lackey", releaseArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ release.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state() == QProcess::NotRunning);
+}
+
+void tst_QSystemSemaphore::processes_data()
+{
+ QTest::addColumn<int>("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()
+{
+#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
+ QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
+#endif
+ QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
+
+ QFETCH(int, processes);
+ QStringList scripts;
+ for (int i = 0; i < processes; ++i)
+ scripts.append(acquirerelease_js());
+
+ QList<QProcess*> consumers;
+ for (int i = 0; i < scripts.count(); ++i) {
+ QStringList arguments = QStringList() << scripts.at(i);
+ QProcess *p = new QProcess;
+ p->setProcessChannelMode(QProcess::ForwardedChannels);
+ consumers.append(p);
+#ifdef Q_OS_WINCE
+ // We can't start the same executable twice on Windows CE.
+ // Create a copy instead.
+ QString lackeyCopy = QLatin1String(LACKEYLOC "/lackey");
+ if (i > 0) {
+ lackeyCopy.append(QString::number(i));
+ lackeyCopy.append(QLatin1String(".exe"));
+ if (!QFile::exists(lackeyCopy))
+ QVERIFY(QFile::copy(LACKEYLOC "/lackey.exe", lackeyCopy));
+ }
+ p->start(lackeyCopy, arguments);
+#else
+ p->start(LACKEYLOC "/lackey", arguments);
+#endif
+ }
+
+ while (!consumers.isEmpty()) {
+ consumers.first()->waitForFinished();
+ QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit);
+ QCOMPARE(consumers.first()->exitCode(), 0);
+ delete consumers.takeFirst();
+ }
+}
+
+void tst_QSystemSemaphore::undo()
+{
+#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
+ QSKIP("This test only checks a unix behavior", SkipSingle);
+#endif
+
+ QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
+
+ QStringList acquireArguments = QStringList() << acquire_js();
+ QProcess acquire;
+ acquire.setProcessChannelMode(QProcess::ForwardedChannels);
+ acquire.start(LACKEYLOC "/lackey", acquireArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state()== QProcess::NotRunning);
+
+ // At process exit the kernel should auto undo
+
+ acquire.start(LACKEYLOC "/lackey", acquireArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state()== QProcess::NotRunning);
+}
+
+void tst_QSystemSemaphore::initialValue()
+{
+#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
+ QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
+#endif
+ QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
+
+ QStringList acquireArguments = QStringList() << acquire_js();
+ QStringList releaseArguments = QStringList() << release_js();
+ QProcess acquire;
+ acquire.setProcessChannelMode(QProcess::ForwardedChannels);
+
+ QProcess release;
+ release.setProcessChannelMode(QProcess::ForwardedChannels);
+
+ acquire.start(LACKEYLOC "/lackey", acquireArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state()== QProcess::NotRunning);
+
+ acquire.start(LACKEYLOC "/lackey", acquireArguments << "2");
+ acquire.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state()== QProcess::Running);
+ acquire.kill();
+
+ release.start(LACKEYLOC "/lackey", releaseArguments);
+ acquire.waitForFinished(LACKYWAITTIME);
+ release.waitForFinished(LACKYWAITTIME);
+ QVERIFY(acquire.state()== QProcess::NotRunning);
+}
+QTEST_MAIN(tst_QSystemSemaphore)
+#include "tst_qsystemsemaphore.moc"
+