0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
#include <qsystemsemaphore.h>
|
|
45 |
//TESTED_CLASS=
|
|
46 |
//TESTED_FILES=
|
|
47 |
|
|
48 |
#define EXISTING_SHARE "existing"
|
|
49 |
#define LACKYLOC "../qsharedmemory/lackey"
|
|
50 |
#define LACKYWAITTIME 10000
|
|
51 |
|
|
52 |
class tst_QSystemSemaphore : public QObject
|
|
53 |
{
|
|
54 |
Q_OBJECT
|
|
55 |
|
|
56 |
public:
|
|
57 |
tst_QSystemSemaphore();
|
|
58 |
virtual ~tst_QSystemSemaphore();
|
|
59 |
|
|
60 |
public Q_SLOTS:
|
|
61 |
void init();
|
|
62 |
void cleanup();
|
|
63 |
|
|
64 |
private slots:
|
|
65 |
void key_data();
|
|
66 |
void key();
|
|
67 |
|
|
68 |
void basicacquire();
|
|
69 |
void complexacquire();
|
|
70 |
|
|
71 |
void basicProcesses();
|
|
72 |
|
|
73 |
void processes_data();
|
|
74 |
void processes();
|
|
75 |
|
|
76 |
void undo();
|
|
77 |
void initialValue();
|
|
78 |
|
|
79 |
private:
|
|
80 |
QSystemSemaphore *existingLock;
|
|
81 |
|
|
82 |
QString makeFile(const QString &resource)
|
|
83 |
{
|
|
84 |
QFile memory(resource);
|
|
85 |
if (!memory.open(QIODevice::ReadOnly)) {
|
|
86 |
qDebug() << "error reading resource" << resource;
|
|
87 |
return QString();
|
|
88 |
}
|
|
89 |
QTemporaryFile *file = new QTemporaryFile;
|
|
90 |
file->open();
|
|
91 |
file->write(memory.readAll());
|
|
92 |
tempFiles.append(file);
|
|
93 |
file->flush();
|
|
94 |
#ifdef Q_OS_WINCE
|
|
95 |
// flush does not flush to disk on Windows CE. It flushes it into its application
|
|
96 |
// cache. Thus we need to close the file to be able that other processes(lackey) can read it
|
|
97 |
QString fileName = file->fileName();
|
|
98 |
file->close();
|
|
99 |
return fileName;
|
|
100 |
#endif
|
|
101 |
return file->fileName();
|
|
102 |
}
|
|
103 |
|
|
104 |
QString acquire_js() { return makeFile(":/systemsemaphore_acquire.js"); }
|
|
105 |
QString release_js() { return makeFile(":/systemsemaphore_release.js"); }
|
|
106 |
QString acquirerelease_js() { return makeFile(":/systemsemaphore_acquirerelease.js"); }
|
|
107 |
QList<QTemporaryFile*> tempFiles;
|
|
108 |
};
|
|
109 |
|
|
110 |
tst_QSystemSemaphore::tst_QSystemSemaphore()
|
|
111 |
{
|
|
112 |
if (!QFile::exists(LACKYLOC "/lackey"))
|
|
113 |
qWarning() << "lackey executable doesn't exists!";
|
|
114 |
}
|
|
115 |
|
|
116 |
tst_QSystemSemaphore::~tst_QSystemSemaphore()
|
|
117 |
{
|
|
118 |
qDeleteAll(tempFiles);
|
|
119 |
}
|
|
120 |
|
|
121 |
void tst_QSystemSemaphore::init()
|
|
122 |
{
|
|
123 |
existingLock = new QSystemSemaphore(EXISTING_SHARE, 1, QSystemSemaphore::Create);
|
|
124 |
}
|
|
125 |
|
|
126 |
void tst_QSystemSemaphore::cleanup()
|
|
127 |
{
|
|
128 |
delete existingLock;
|
|
129 |
}
|
|
130 |
|
|
131 |
void tst_QSystemSemaphore::key_data()
|
|
132 |
{
|
|
133 |
QTest::addColumn<QString>("constructorKey");
|
|
134 |
QTest::addColumn<QString>("setKey");
|
|
135 |
|
|
136 |
QTest::newRow("null, null") << QString() << QString();
|
|
137 |
QTest::newRow("null, one") << QString() << QString("one");
|
|
138 |
QTest::newRow("one, two") << QString("one") << QString("two");
|
|
139 |
}
|
|
140 |
|
|
141 |
/*!
|
|
142 |
Basic key testing
|
|
143 |
*/
|
|
144 |
void tst_QSystemSemaphore::key()
|
|
145 |
{
|
|
146 |
QFETCH(QString, constructorKey);
|
|
147 |
QFETCH(QString, setKey);
|
|
148 |
|
|
149 |
QSystemSemaphore sem(constructorKey);
|
|
150 |
QCOMPARE(sem.key(), constructorKey);
|
|
151 |
QCOMPARE(sem.error(), QSystemSemaphore::NoError);
|
|
152 |
QCOMPARE(sem.errorString(), QString());
|
|
153 |
|
|
154 |
sem.setKey(setKey);
|
|
155 |
QCOMPARE(sem.key(), setKey);
|
|
156 |
QCOMPARE(sem.error(), QSystemSemaphore::NoError);
|
|
157 |
QCOMPARE(sem.errorString(), QString());
|
|
158 |
}
|
|
159 |
|
|
160 |
void tst_QSystemSemaphore::basicacquire()
|
|
161 |
{
|
|
162 |
QSystemSemaphore sem("QSystemSemaphore_basicacquire", 1, QSystemSemaphore::Create);
|
|
163 |
QVERIFY(sem.acquire());
|
|
164 |
QCOMPARE(sem.error(), QSystemSemaphore::NoError);
|
|
165 |
QVERIFY(sem.release());
|
|
166 |
QCOMPARE(sem.error(), QSystemSemaphore::NoError);
|
|
167 |
QCOMPARE(sem.errorString(), QString());
|
|
168 |
}
|
|
169 |
|
|
170 |
void tst_QSystemSemaphore::complexacquire()
|
|
171 |
{
|
|
172 |
QSystemSemaphore sem("QSystemSemaphore_complexacquire", 2, QSystemSemaphore::Create);
|
|
173 |
QVERIFY(sem.acquire());
|
|
174 |
QVERIFY(sem.release());
|
|
175 |
QVERIFY(sem.acquire());
|
|
176 |
QVERIFY(sem.release());
|
|
177 |
QVERIFY(sem.acquire());
|
|
178 |
QVERIFY(sem.acquire());
|
|
179 |
QVERIFY(sem.release());
|
|
180 |
QVERIFY(sem.release());
|
|
181 |
QCOMPARE(sem.error(), QSystemSemaphore::NoError);
|
|
182 |
QCOMPARE(sem.errorString(), QString());
|
|
183 |
}
|
|
184 |
|
|
185 |
void tst_QSystemSemaphore::basicProcesses()
|
|
186 |
{
|
|
187 |
#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
|
|
188 |
QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
|
|
189 |
#endif
|
|
190 |
QSystemSemaphore sem("store", 0, QSystemSemaphore::Create);
|
|
191 |
|
|
192 |
QStringList acquireArguments = QStringList() << acquire_js();
|
|
193 |
QStringList releaseArguments = QStringList() << release_js();
|
|
194 |
QProcess acquire;
|
|
195 |
acquire.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
196 |
|
|
197 |
QProcess release;
|
|
198 |
release.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
199 |
|
|
200 |
acquire.start(LACKYLOC "/lackey", acquireArguments);
|
|
201 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
202 |
QVERIFY(acquire.state() == QProcess::Running);
|
|
203 |
acquire.kill();
|
|
204 |
release.start(LACKYLOC "/lackey", releaseArguments);
|
|
205 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
206 |
release.waitForFinished(LACKYWAITTIME);
|
|
207 |
QVERIFY(acquire.state() == QProcess::NotRunning);
|
|
208 |
}
|
|
209 |
|
|
210 |
void tst_QSystemSemaphore::processes_data()
|
|
211 |
{
|
|
212 |
QTest::addColumn<int>("processes");
|
|
213 |
for (int i = 0; i < 5; ++i) {
|
|
214 |
QTest::newRow("1 process") << 1;
|
|
215 |
QTest::newRow("3 process") << 3;
|
|
216 |
QTest::newRow("10 process") << 10;
|
|
217 |
}
|
|
218 |
}
|
|
219 |
|
|
220 |
void tst_QSystemSemaphore::processes()
|
|
221 |
{
|
|
222 |
#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
|
|
223 |
QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
|
|
224 |
#endif
|
|
225 |
QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
|
|
226 |
|
|
227 |
QFETCH(int, processes);
|
|
228 |
QStringList scripts;
|
|
229 |
for (int i = 0; i < processes; ++i)
|
|
230 |
scripts.append(acquirerelease_js());
|
|
231 |
|
|
232 |
QList<QProcess*> consumers;
|
|
233 |
for (int i = 0; i < scripts.count(); ++i) {
|
|
234 |
QStringList arguments = QStringList() << scripts.at(i);
|
|
235 |
QProcess *p = new QProcess;
|
|
236 |
p->setProcessChannelMode(QProcess::ForwardedChannels);
|
|
237 |
consumers.append(p);
|
|
238 |
p->start(LACKYLOC "/lackey", arguments);
|
|
239 |
}
|
|
240 |
|
|
241 |
while (!consumers.isEmpty()) {
|
|
242 |
consumers.first()->waitForFinished();
|
|
243 |
QCOMPARE(consumers.first()->exitStatus(), QProcess::NormalExit);
|
|
244 |
QCOMPARE(consumers.first()->exitCode(), 0);
|
|
245 |
delete consumers.takeFirst();
|
|
246 |
}
|
|
247 |
}
|
|
248 |
|
|
249 |
void tst_QSystemSemaphore::undo()
|
|
250 |
{
|
|
251 |
#if defined(Q_OS_WIN) || defined(Q_OS_SYMBIAN)
|
|
252 |
QSKIP("This test only checks a unix behavior", SkipSingle);
|
|
253 |
#endif
|
|
254 |
|
|
255 |
QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
|
|
256 |
|
|
257 |
QStringList acquireArguments = QStringList() << acquire_js();
|
|
258 |
QProcess acquire;
|
|
259 |
acquire.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
260 |
acquire.start(LACKYLOC "/lackey", acquireArguments);
|
|
261 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
262 |
QVERIFY(acquire.state()== QProcess::NotRunning);
|
|
263 |
|
|
264 |
// At process exit the kernel should auto undo
|
|
265 |
|
|
266 |
acquire.start(LACKYLOC "/lackey", acquireArguments);
|
|
267 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
268 |
QVERIFY(acquire.state()== QProcess::NotRunning);
|
|
269 |
}
|
|
270 |
|
|
271 |
void tst_QSystemSemaphore::initialValue()
|
|
272 |
{
|
|
273 |
#if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86)
|
|
274 |
QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll);
|
|
275 |
#endif
|
|
276 |
QSystemSemaphore sem("store", 1, QSystemSemaphore::Create);
|
|
277 |
|
|
278 |
QStringList acquireArguments = QStringList() << acquire_js();
|
|
279 |
QStringList releaseArguments = QStringList() << release_js();
|
|
280 |
QProcess acquire;
|
|
281 |
acquire.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
282 |
|
|
283 |
QProcess release;
|
|
284 |
release.setProcessChannelMode(QProcess::ForwardedChannels);
|
|
285 |
|
|
286 |
acquire.start(LACKYLOC "/lackey", acquireArguments);
|
|
287 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
288 |
QVERIFY(acquire.state()== QProcess::NotRunning);
|
|
289 |
|
|
290 |
acquire.start(LACKYLOC "/lackey", acquireArguments << "2");
|
|
291 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
292 |
QVERIFY(acquire.state()== QProcess::Running);
|
|
293 |
acquire.kill();
|
|
294 |
|
|
295 |
release.start(LACKYLOC "/lackey", releaseArguments);
|
|
296 |
acquire.waitForFinished(LACKYWAITTIME);
|
|
297 |
release.waitForFinished(LACKYWAITTIME);
|
|
298 |
QVERIFY(acquire.state()== QProcess::NotRunning);
|
|
299 |
}
|
|
300 |
QTEST_MAIN(tst_QSystemSemaphore)
|
|
301 |
#include "tst_qsystemsemaphore.moc"
|
|
302 |
|