summaryrefslogtreecommitdiffstats
path: root/tests/auto/qsystemsemaphore/tst_qsystemsemaphore.cpp
blob: 6bfab15ab5c8ba6f0523cdf261ce49ca8837a6e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/****************************************************************************
**
** Copyright (C) 2009 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"

#define LACKYLOC "../qsharedmemory/lackey"

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(LACKYLOC "/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(LACKYLOC "/lackey", acquireArguments);
    acquire.waitForFinished(5000);
    QVERIFY(acquire.state() == QProcess::Running);
    acquire.kill();
    release.start(LACKYLOC "/lackey", releaseArguments);
    acquire.waitForFinished(5000);
    release.waitForFinished(5000);
    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);
        p->start(LACKYLOC "/lackey", arguments);
    }

    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(LACKYLOC "/lackey", acquireArguments);
    acquire.waitForFinished(1000);
    QVERIFY(acquire.state()== QProcess::NotRunning);

    // At process exit the kernel should auto undo

    acquire.start(LACKYLOC "/lackey", acquireArguments);
    acquire.waitForFinished(1000);
    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(LACKYLOC "/lackey", acquireArguments);
    acquire.waitForFinished(10000);
    QVERIFY(acquire.state()== QProcess::NotRunning);

    acquire.start(LACKYLOC "/lackey", acquireArguments << "2");
    acquire.waitForFinished(1000);
    QVERIFY(acquire.state()== QProcess::Running);
    acquire.kill();

    release.start(LACKYLOC "/lackey", releaseArguments);
    acquire.waitForFinished(10000);
    release.waitForFinished(10000);
    QVERIFY(acquire.state()== QProcess::NotRunning);
}
QTEST_MAIN(tst_QSystemSemaphore)
#include "tst_qsystemsemaphore.moc"