summaryrefslogtreecommitdiffstats
path: root/tests/auto/qsystemreadwritelock_oop/tst_qsystemreadwritelock_oop.cpp
blob: 7c93006fb618bfdbb14e915a1a583f761f9757c0 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** 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.
**
** 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.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

//TESTED_COMPONENT=src/publishsubscribe

#include <QtTest/QtTest>
#include <QDir>
#include <QProcess>
#include <QIODevice>
#include <QLocalServer>
#include <QLocalSocket>
#include "qsystemreadwritelock_p.h"
#include "common.h"

QTM_USE_NAMESPACE
class tst_QSystemReadWriteLock_oop : public QObject{
    Q_OBJECT
private slots:
    void initTestCase();
    void readLockBlockRelease();
    void writeLockBlockRelease();
    void multipleReadersBlockRelease();
    void multipleReadersLoop();
    void multipleWritersLoop();
    void exclusiveWriteTest();
    void writerPrecedence();

private:
    bool waitForLine(QIODevice *device, const QString &expectedLine,
                    bool print = false, int timeout=5000);

};

void tst_QSystemReadWriteLock_oop::initTestCase()
{
    // if an old writeLockExcl.tmp exists at the beginning of this test case, try to remove it,
    // otherwise test functions may fail
    QDir cwd;
    if (cwd.exists("writeLockExcl.tmp")) {
        QVERIFY(cwd.remove("writeLockExcl.tmp"));
    }
}

/*
    A writer aquires a write-lock, a reader tries to lock
    the writer releases the lock, the reader gets the lock
*/
void tst_QSystemReadWriteLock_oop::readLockBlockRelease()
{
    bool print = false;
    QSystemReadWriteLock testLock("Viper", QSystemReadWriteLock::Create);
    if (print)
        qDebug() << "Main process: About to lock for writing";
    testLock.lockForWrite();
    if (print)
        qDebug() << "Main process: After lock for writing";

    QLocalServer server;
    QString connectionName = "readLockBlockRelease";
    QVERIFY(QLocalServer::removeServer(connectionName));
    QVERIFY(server.listen(connectionName));

    QProcess reader;
    reader.setReadChannel(QProcess::StandardError);
    QStringList args;
    args << connectionName;
    args << "ReadLock";

    reader.start("./lackey", args);
    QVERIFY(reader.waitForStarted());

    QVERIFY(server.waitForNewConnection(5000));
    QLocalSocket *oopSocket = server.nextPendingConnection();

    QVERIFY(waitForLine(oopSocket, Lackey::BeforeLockForRead,print));
    QVERIFY(!waitForLine(oopSocket, Lackey::AfterLockForRead,false,1000));

    testLock.unlock();
    if (print)
        qDebug() << "Main process: After unlock(write)";

    QVERIFY(waitForLine(oopSocket, Lackey::AfterLockForRead, print));
    QVERIFY(waitForLine(oopSocket, Lackey::AfterUnlockForRead, print));
    QVERIFY(reader.waitForFinished());
}

/*
    writer1(main process) aquires a write-lock, writer2(lackey) blocks,
    writer1 releases the lock, writer2 gets the lock
*/
void tst_QSystemReadWriteLock_oop::writeLockBlockRelease()
{
    bool print = false;
    QSystemReadWriteLock testLock("Viper", QSystemReadWriteLock::Create);
    if (print)
        qDebug() << "Main process: About to lock for writing";
    testLock.lockForWrite();
    if (print)
        qDebug() << "Main process: After lock for writing";

    QLocalServer server;
    QString connectionName = "writeLockBlockRelease";
    QVERIFY(QLocalServer::removeServer(connectionName));
    QVERIFY(server.listen(connectionName));

    QProcess writer;
    writer.setReadChannel(QProcess::StandardError);
    QStringList args;
    args << connectionName;
    args << "WriteLock";
    writer.start("./lackey", args);
    QVERIFY(writer.waitForStarted());

    QVERIFY(server.waitForNewConnection(5000));
    QLocalSocket *oopSocket = server.nextPendingConnection();

    QVERIFY(waitForLine(oopSocket, Lackey::BeforeLockForWrite,print));
    QVERIFY(!waitForLine(oopSocket, Lackey::AfterUnlockForWrite,false, 1000));

    testLock.unlock();
    if (print)
        qDebug() << "Main process: After unlock(write)";

    QVERIFY(waitForLine(oopSocket, Lackey::AfterLockForWrite, print));
    QVERIFY(waitForLine(oopSocket, Lackey::AfterUnlockForWrite, print));
    QVERIFY(writer.waitForFinished());
}

/*
    Two readers aquire a read-lock, one writer attempts a write lock and blocks,
    the readers release their locks, the writer gets the lock.
*/
void tst_QSystemReadWriteLock_oop::multipleReadersBlockRelease()
{
    bool print = false;
    //Use the Create enum to ensure synchronization primitives are reset
    //to a "clean" state
    QSystemReadWriteLock rwlock("Viper", QSystemReadWriteLock::Create);

    const int numReaders = 2;
    QProcess readers[numReaders];
    QLocalServer readerServers[numReaders];
    QLocalSocket *readerSockets[numReaders];

    QString connectionName = "multipleReadersBlockRelease";

    QStringList args;
    args << "ReadLockReleaseable";

    for( int i=0; i < numReaders; ++i) {
        QString readerConnectionName = connectionName.append("_reader_").append(QString::number(i));
        QVERIFY(QLocalServer::removeServer(readerConnectionName));
        QVERIFY(readerServers[i].listen(readerConnectionName));
        args.push_front(readerConnectionName);

        readers[i].setReadChannel(QProcess::StandardError);
        readers[i].start("./lackey", args);
        QVERIFY(readers[i].waitForStarted());

        QVERIFY(readerServers[i].waitForNewConnection(5000));
        readerSockets[i] = readerServers[i].nextPendingConnection();

        QVERIFY(waitForLine(readerSockets[i], Lackey::BeforeLockForRead, print));
        QVERIFY(waitForLine(readerSockets[i], Lackey::AfterLockForRead, print));

        args.pop_front();
    }

    QLocalServer server;
    QString writerConnectionName = connectionName.append("_writer");
    QVERIFY(QLocalServer::removeServer(writerConnectionName));
    QVERIFY(server.listen(writerConnectionName));

    QProcess writer;
    writer.setReadChannel(QProcess::StandardError);
    args.clear();
    args << writerConnectionName;
    args << "WriteLock";
    writer.start("./lackey", args);
    QVERIFY(writer.waitForStarted());

    QVERIFY(server.waitForNewConnection(5000));
    QLocalSocket *writerSocket = server.nextPendingConnection();

    QVERIFY(waitForLine(writerSocket, Lackey::BeforeLockForWrite, print));
    QVERIFY(!waitForLine(writerSocket, Lackey::AfterLockForWrite, false, 1000));

    for (int i = 0; i < numReaders; ++i) {
        readerSockets[i]->write("release\n");
        QVERIFY(waitForLine(readerSockets[i], Lackey::AfterUnlockForRead, print));
        if ( i < (numReaders - 1))
            QVERIFY(!waitForLine(writerSocket, Lackey::AfterLockForWrite, false, 1000));
    }
    QVERIFY(waitForLine(writerSocket, Lackey::AfterLockForWrite, print));
    QVERIFY(waitForLine(writerSocket, Lackey::AfterUnlockForWrite, print));

    for(int i = 0; i < numReaders; ++i) {
        QVERIFY(readers[i].waitForFinished());
    }
    QVERIFY(writer.waitForFinished());
}

/*
    Multiple readers locks and unlocks a lock.
*/
void tst_QSystemReadWriteLock_oop::multipleReadersLoop()
{
    //ensure synchronization primitives are reset to a "clean" state
    QSystemReadWriteLock testLock("Viper", QSystemReadWriteLock::Create);

    int runTime = 10;
    int holdTime = 10;
    int waitTime = 0;

    const int numReaders = 10;
    QProcess readers[numReaders];
    QStringList args;
    args << "NoComms" // no communications between test and lackey
        << "ReadLockLoop"
        << QString::number(runTime)
        << QString::number(holdTime)
        << QString::number(waitTime);

    for (int i = 0; i < numReaders; ++i) {
        readers[i].setProcessChannelMode(QProcess::ForwardedChannels);
        readers[i].start("./lackey", args);
    }

    for (int i=0; i < numReaders; ++i) {
        QVERIFY(readers[i].waitForFinished(5000));
        QCOMPARE(readers[i].exitCode(), 0);
        QCOMPARE(readers[i].exitStatus(), QProcess::NormalExit);
    }
}

/*
    Multiple writers lock and unlock a lock
*/
void tst_QSystemReadWriteLock_oop::multipleWritersLoop()
{
    //ensure synchronization primitives are reset to a "clean" state
    QSystemReadWriteLock testLock("Viper", QSystemReadWriteLock::Create);

    int runTime = 10;
    int holdTime = 10;
    int waitTime = 0;

    const int numWriters = 10;
    QProcess writers[numWriters];
    QStringList args;
    args << "NoComms" // no communications between test and lackey
        << "WriteLockLoop"
        << QString::number(runTime)
        << QString::number(holdTime)
        << QString::number(waitTime);

    for (int i = 0; i < numWriters; ++i) {
        writers[i].setProcessChannelMode(QProcess::ForwardedChannels);
        writers[i].start("./lackey", args);
    }

    for (int i = 0; i < numWriters; ++i) {
        QVERIFY(writers[i].waitForFinished(5000));
        QCOMPARE(writers[i].exitCode(), 0);
        QCOMPARE(writers[i].exitStatus(), QProcess::NormalExit);
    }
}

/*
    Writers create an exclusive file while they have a write lock
    and remove it on unlock.  Readers verify that that the file
    does not exist and writers verify that the file does not
    already exist when they obtain a lock.
*/
void tst_QSystemReadWriteLock_oop::exclusiveWriteTest()
{
    //ensure synchronization primitives are reset to a "clean" state
    QSystemReadWriteLock testLock("Viper", QSystemReadWriteLock::Create);

    int runTime=10000;
#if !defined(Q_OS_WINCE)
    const int numReaders = 20;
#else
    const int numReaders = 10;
#endif

    int readerHoldTime = 0;
    int readerWaitTime = 1;

    const int numWriters = 3;
    int writerHoldTime = 20;
    int writerWaitTime = 150;

    QProcess readers[numReaders];
    QProcess writers[numWriters];
    QStringList args;
    args << "NoComms" // no communications between test and lackey
        << "ReadLockExcl"
        << QString::number(runTime)
        << QString::number(readerHoldTime)
        << QString::number(readerWaitTime);

    for (int i = 0; i < numReaders; ++i) {
        readers[i].setProcessChannelMode(QProcess::ForwardedChannels);
        readers[i].start("./lackey", args);
    }

    args.clear();
    args << "NoComms" // no communications between test and lackey
        << "WriteLockExcl"
        << QString::number(runTime)
        << QString::number(writerHoldTime)
        << QString::number(writerWaitTime);
    for (int i = 0; i < numWriters; ++i) {
        writers[i].setProcessChannelMode(QProcess::ForwardedChannels);
        writers[i].start("./lackey", args);
    }

    // Finding the existence of the exclusive file will
    // result in a qFatal() in the lackey process
    // The exit status checks below will fail if this
    // is the case
    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(readers[i].waitForFinished(2 * runTime));
        QCOMPARE(readers[i].exitCode(), 0);
        QCOMPARE(readers[i].exitStatus(), QProcess::NormalExit);
     }

    for (int i = 0; i < numWriters; ++i) {
        QVERIFY(writers[i].waitForFinished(2 * runTime));
        QCOMPARE(writers[i].exitCode(), 0);
        QCOMPARE(writers[i].exitStatus(), QProcess::NormalExit);
     }
}

/*
    Writer1(main process) acquires a write-lock
    Readers try to acquire read-locks but are blocked
    Writer2(lackey) tries to acquire a write-lock, but is blocked
    Writer1 releases the lock, writer2 gets the lock, readers still blocked
    Writer2 releases the lock, readers are unblocked and get their read-locks
*/
void tst_QSystemReadWriteLock_oop::writerPrecedence()
{
    bool print = false;
    QSystemReadWriteLock testRwLock("Viper", QSystemReadWriteLock::Create);
    if (print)
        qDebug() << "Main process: About to lock for writing";
    testRwLock.lockForWrite();
     if (print)
        qDebug() << "Main process: After lock for writing";

    const int numReaders = 5;
    QProcess readers[numReaders];
    QLocalServer readerServers[numReaders];
    QLocalSocket *readerSockets[numReaders];

    QString connectionName = "writePrecedence";

    QStringList args;
    args << "ReadLock";
    for (int i = 0; i < numReaders; ++i) {
        QString readerConnectionName = connectionName.append("_reader_").append(QString::number(i));
        QVERIFY(QLocalServer::removeServer(readerConnectionName));
        QVERIFY(readerServers[i].listen(readerConnectionName));
        args.push_front(readerConnectionName);

        readers[i].setReadChannel(QProcess::StandardError);
        readers[i].start("./lackey", args);
        QVERIFY(readers[i].waitForStarted());

        QVERIFY(readerServers[i].waitForNewConnection(5000));
        readerSockets[i] = readerServers[i].nextPendingConnection();

        QVERIFY(waitForLine(readerSockets[i], Lackey::BeforeLockForRead, print));
        QVERIFY(!waitForLine(readerSockets[i], Lackey::AfterLockForRead, false, 1000));

        args.pop_front();
    }

    QLocalServer server;
    QString writerConnectionName = connectionName.append("_writer");
    QVERIFY(QLocalServer::removeServer(writerConnectionName));
    QVERIFY(server.listen(writerConnectionName));

    QProcess writer;
    writer.setReadChannel(QProcess::StandardError);
    args.clear();
    args << writerConnectionName;
    args << "WriteLockReleaseable";
    writer.start("./lackey", args);

    QVERIFY(server.waitForNewConnection(5000));
    QLocalSocket *writerSocket = server.nextPendingConnection();

    QVERIFY(waitForLine(writerSocket, Lackey::BeforeLockForWrite, print));
    QVERIFY(!waitForLine(writerSocket, Lackey::AfterLockForWrite, false, 1000));

    testRwLock.unlock();
    QVERIFY(waitForLine(writerSocket, Lackey::AfterLockForWrite, print));
     if (print)
        qDebug() << "Main process: After unlock(write)";

   //verify the reader processes are still blocked
    QTest::qSleep(1000);
    for (int i = 0; i < numReaders; ++i) {
        QCOMPARE(readers[i].state(), QProcess::Running);
    }

    writerSocket->write("release\n");
    QVERIFY(waitForLine(writerSocket, Lackey::AfterUnlockForWrite, print));

    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(waitForLine(readerSockets[i], Lackey::AfterLockForRead, print));
    }//Note: 2 loops (ie above and below) are used due to timing issues
    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(waitForLine(readerSockets[i], Lackey::AfterUnlockForRead, print));
    }

    for (int i = 0; i < numReaders; ++i) {
        QVERIFY(readers[i].waitForFinished(5000));
    }
    QVERIFY(writer.waitForFinished(5000));
}


/*
   Waits for a given \a expectedLine to be written by \a process
   for a given \a timeout(msecs).  The line written by the process
   is printed to console if \a print is true.
*/
bool tst_QSystemReadWriteLock_oop::waitForLine(QIODevice * device,const QString &expectedLine, bool print, int timeout)
{
    if (!device->waitForReadyRead(timeout))
    {
        if (print)
            qWarning() << "Wait for ready read returned false";
        return false;
    }

    QString line = device->readLine();
    line = line.trimmed();
    if (print)
        qDebug() << qPrintable(line);
    if (line.compare(expectedLine) !=0) {
        qWarning() << "Expected Line: \t" << qPrintable(expectedLine);
        qWarning() << "Actual Line: \t" << qPrintable(line);
        return false;
    }

    return true;
}

QTEST_MAIN(tst_QSystemReadWriteLock_oop)
#include "tst_qsystemreadwritelock_oop.moc"