|
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 <qsharedmemory.h> |
|
45 |
|
46 //TESTED_CLASS= |
|
47 //TESTED_FILES= |
|
48 |
|
49 #define EXISTING_SHARE "existing" |
|
50 #define EXISTING_SIZE 1024 |
|
51 |
|
52 #ifdef Q_OS_SYMBIAN |
|
53 #define SRCDIR "c:/data/qsharedmemorytemp/" |
|
54 #endif |
|
55 Q_DECLARE_METATYPE(QSharedMemory::SharedMemoryError) |
|
56 Q_DECLARE_METATYPE(QSharedMemory::AccessMode) |
|
57 |
|
58 class tst_QSharedMemory : public QObject |
|
59 { |
|
60 Q_OBJECT |
|
61 |
|
62 public: |
|
63 tst_QSharedMemory(); |
|
64 virtual ~tst_QSharedMemory(); |
|
65 |
|
66 public Q_SLOTS: |
|
67 void init(); |
|
68 void cleanup(); |
|
69 |
|
70 private slots: |
|
71 // basics |
|
72 void constructor(); |
|
73 void key_data(); |
|
74 void key(); |
|
75 void create_data(); |
|
76 void create(); |
|
77 void attach_data(); |
|
78 void attach(); |
|
79 void lock(); |
|
80 |
|
81 // custom edge cases |
|
82 void removeWhileAttached(); |
|
83 void emptyMemory(); |
|
84 void readOnly(); |
|
85 |
|
86 // basics all together |
|
87 void simpleProducerConsumer_data(); |
|
88 void simpleProducerConsumer(); |
|
89 void simpleDoubleProducerConsumer(); |
|
90 |
|
91 // with threads |
|
92 void simpleThreadedProducerConsumer_data(); |
|
93 void simpleThreadedProducerConsumer(); |
|
94 |
|
95 // with processes |
|
96 void simpleProcessProducerConsumer_data(); |
|
97 void simpleProcessProducerConsumer(); |
|
98 |
|
99 // extreme cases |
|
100 void useTooMuchMemory(); |
|
101 void attachTooMuch(); |
|
102 |
|
103 protected: |
|
104 int remove(const QString &key); |
|
105 |
|
106 QString rememberKey(const QString &key) |
|
107 { |
|
108 if (key == EXISTING_SHARE) |
|
109 return key; |
|
110 if (!keys.contains(key)) { |
|
111 keys.append(key); |
|
112 remove(key); |
|
113 } |
|
114 return key; |
|
115 } |
|
116 |
|
117 QStringList keys; |
|
118 QList<QSharedMemory*> jail; |
|
119 QSharedMemory *existingSharedMemory; |
|
120 }; |
|
121 |
|
122 tst_QSharedMemory::tst_QSharedMemory() : existingSharedMemory(0) |
|
123 { |
|
124 } |
|
125 |
|
126 tst_QSharedMemory::~tst_QSharedMemory() |
|
127 { |
|
128 } |
|
129 |
|
130 void tst_QSharedMemory::init() |
|
131 { |
|
132 existingSharedMemory = new QSharedMemory(EXISTING_SHARE); |
|
133 if (!existingSharedMemory->create(EXISTING_SIZE)) { |
|
134 QVERIFY(existingSharedMemory->error() == QSharedMemory::AlreadyExists); |
|
135 } |
|
136 } |
|
137 |
|
138 void tst_QSharedMemory::cleanup() |
|
139 { |
|
140 delete existingSharedMemory; |
|
141 qDeleteAll(jail.begin(), jail.end()); |
|
142 jail.clear(); |
|
143 |
|
144 keys.append(EXISTING_SHARE); |
|
145 for (int i = 0; i < keys.count(); ++i) { |
|
146 QSharedMemory sm(keys.at(i)); |
|
147 if (!sm.create(1024)) { |
|
148 //if(sm.error() != QSharedMemory::KeyError) |
|
149 // qWarning() << "test cleanup: remove failed:" << keys.at(i) << sm.error() << sm.errorString(); |
|
150 sm.attach(); |
|
151 sm.detach(); |
|
152 remove(keys.at(i)); |
|
153 } |
|
154 } |
|
155 } |
|
156 |
|
157 #ifndef Q_OS_WIN |
|
158 #include "private/qsharedmemory_p.h" |
|
159 #include <sys/types.h> |
|
160 #include <sys/ipc.h> |
|
161 #include <sys/shm.h> |
|
162 #endif |
|
163 |
|
164 int tst_QSharedMemory::remove(const QString &key) |
|
165 { |
|
166 #ifndef Q_OS_WIN |
|
167 // On unix the shared memory might exists from a previously failed test |
|
168 // or segfault, remove it it does |
|
169 if (key.isEmpty()) |
|
170 return -1; |
|
171 |
|
172 // ftok requires that an actual file exists somewhere |
|
173 QString fileName = QSharedMemoryPrivate::makePlatformSafeKey(key); |
|
174 if (!QFile::exists(fileName)) { |
|
175 //qDebug() << "exits failed"; |
|
176 return -2; |
|
177 } |
|
178 |
|
179 int unix_key = ftok(fileName.toLatin1().constData(), 'Q'); |
|
180 if (-1 == unix_key) { |
|
181 qDebug() << "ftok failed"; |
|
182 return -3; |
|
183 } |
|
184 |
|
185 int id = shmget(unix_key, 0, 0660); |
|
186 if (-1 == id) { |
|
187 qDebug() << "shmget failed"; |
|
188 return -4; |
|
189 } |
|
190 |
|
191 struct shmid_ds shmid_ds; |
|
192 if (-1 == shmctl(id, IPC_RMID, &shmid_ds)) { |
|
193 qDebug() << "shmctl failed"; |
|
194 return -5; |
|
195 } |
|
196 return QFile::remove(fileName); |
|
197 #else |
|
198 Q_UNUSED(key); |
|
199 return 0; |
|
200 #endif |
|
201 } |
|
202 |
|
203 /*! |
|
204 Tests the default values |
|
205 */ |
|
206 void tst_QSharedMemory::constructor() |
|
207 { |
|
208 QSharedMemory sm; |
|
209 QCOMPARE(sm.key(), QString()); |
|
210 QVERIFY(!sm.isAttached()); |
|
211 QVERIFY(sm.data() == 0); |
|
212 QCOMPARE(sm.size(), 0); |
|
213 QCOMPARE(sm.error(), QSharedMemory::NoError); |
|
214 QVERIFY(sm.errorString() == QString()); |
|
215 } |
|
216 |
|
217 void tst_QSharedMemory::key_data() |
|
218 { |
|
219 QTest::addColumn<QString>("constructorKey"); |
|
220 QTest::addColumn<QString>("setKey"); |
|
221 |
|
222 QTest::newRow("null, null") << QString() << QString(); |
|
223 QTest::newRow("null, one") << QString() << QString("one"); |
|
224 QTest::newRow("one, two") << QString("one") << QString("two"); |
|
225 QTest::newRow("invalid") << QString("o/e") << QString("t/o"); |
|
226 } |
|
227 |
|
228 /*! |
|
229 Basic key testing |
|
230 */ |
|
231 void tst_QSharedMemory::key() |
|
232 { |
|
233 QFETCH(QString, constructorKey); |
|
234 QFETCH(QString, setKey); |
|
235 |
|
236 QSharedMemory sm(constructorKey); |
|
237 QCOMPARE(sm.key(), constructorKey); |
|
238 sm.setKey(setKey); |
|
239 QCOMPARE(sm.key(), setKey); |
|
240 QCOMPARE(sm.isAttached(), false); |
|
241 |
|
242 QCOMPARE(sm.error(), QSharedMemory::NoError); |
|
243 QVERIFY(sm.errorString() == QString()); |
|
244 QVERIFY(sm.data() == 0); |
|
245 QCOMPARE(sm.size(), 0); |
|
246 |
|
247 QCOMPARE(sm.detach(), false); |
|
248 } |
|
249 |
|
250 void tst_QSharedMemory::create_data() |
|
251 { |
|
252 QTest::addColumn<QString>("key"); |
|
253 QTest::addColumn<int>("size"); |
|
254 QTest::addColumn<bool>("canCreate"); |
|
255 QTest::addColumn<QSharedMemory::SharedMemoryError>("error"); |
|
256 |
|
257 QTest::newRow("null key") << QString() << 1024 |
|
258 << false << QSharedMemory::LockError; |
|
259 QTest::newRow("-1 size") << QString("negsize") << -1 |
|
260 << false << QSharedMemory::InvalidSize; |
|
261 QTest::newRow("nor size") << QString("norsize") << 1024 |
|
262 << true << QSharedMemory::NoError; |
|
263 QTest::newRow("already exists") << QString(EXISTING_SHARE) << EXISTING_SIZE |
|
264 << false << QSharedMemory::AlreadyExists; |
|
265 } |
|
266 |
|
267 /*! |
|
268 Basic create testing |
|
269 */ |
|
270 void tst_QSharedMemory::create() |
|
271 { |
|
272 QFETCH(QString, key); |
|
273 QFETCH(int, size); |
|
274 QFETCH(bool, canCreate); |
|
275 QFETCH(QSharedMemory::SharedMemoryError, error); |
|
276 |
|
277 QSharedMemory sm(rememberKey(key)); |
|
278 QCOMPARE(sm.create(size), canCreate); |
|
279 if(sm.error() != error) |
|
280 qDebug() << sm.errorString(); |
|
281 QCOMPARE(sm.key(), key); |
|
282 if (canCreate) { |
|
283 QVERIFY(sm.errorString() == QString()); |
|
284 QVERIFY(sm.data() != 0); |
|
285 QVERIFY(sm.size() != 0); |
|
286 } else { |
|
287 QVERIFY(sm.data() == 0); |
|
288 QVERIFY(sm.errorString() != QString()); |
|
289 } |
|
290 } |
|
291 |
|
292 void tst_QSharedMemory::attach_data() |
|
293 { |
|
294 QTest::addColumn<QString>("key"); |
|
295 QTest::addColumn<bool>("exists"); |
|
296 QTest::addColumn<QSharedMemory::SharedMemoryError>("error"); |
|
297 |
|
298 QTest::newRow("null key") << QString() << false << QSharedMemory::LockError; |
|
299 QTest::newRow("doesn't exists") << QString("doesntexists") << false << QSharedMemory::NotFound; |
|
300 QTest::newRow("already exists") << QString(EXISTING_SHARE) << true << QSharedMemory::NoError; |
|
301 } |
|
302 |
|
303 /*! |
|
304 Basic attach/detach testing |
|
305 */ |
|
306 void tst_QSharedMemory::attach() |
|
307 { |
|
308 QFETCH(QString, key); |
|
309 QFETCH(bool, exists); |
|
310 QFETCH(QSharedMemory::SharedMemoryError, error); |
|
311 #ifdef Q_OS_HPUX |
|
312 if (QLatin1String(QTest::currentDataTag()) == QLatin1String("already exists")) { |
|
313 QSKIP("HPUX doesn't allow for multiple attaches per process", SkipSingle); |
|
314 } |
|
315 #endif |
|
316 QSharedMemory sm(key); |
|
317 QCOMPARE(sm.attach(), exists); |
|
318 QCOMPARE(sm.isAttached(), exists); |
|
319 QCOMPARE(sm.error(), error); |
|
320 QCOMPARE(sm.key(), key); |
|
321 if (exists) { |
|
322 QVERIFY(sm.data() != 0); |
|
323 QVERIFY(sm.size() != 0); |
|
324 QCOMPARE(sm.errorString(), QString()); |
|
325 QVERIFY(sm.detach()); |
|
326 // Make sure detach doesn't screw up something and we can't re-attach. |
|
327 QVERIFY(sm.attach()); |
|
328 QVERIFY(sm.detach()); |
|
329 QCOMPARE(sm.size(), 0); |
|
330 QVERIFY(sm.data() == 0); |
|
331 } else { |
|
332 QVERIFY(sm.data() == 0); |
|
333 QVERIFY(sm.size() == 0); |
|
334 QVERIFY(sm.errorString() != QString()); |
|
335 QVERIFY(!sm.detach()); |
|
336 } |
|
337 } |
|
338 |
|
339 void tst_QSharedMemory::lock() |
|
340 { |
|
341 QSharedMemory shm; |
|
342 QVERIFY(!shm.lock()); |
|
343 QCOMPARE(shm.error(), QSharedMemory::LockError); |
|
344 |
|
345 shm.setKey(QLatin1String("qsharedmemory")); |
|
346 |
|
347 QVERIFY(!shm.lock()); |
|
348 QCOMPARE(shm.error(), QSharedMemory::LockError); |
|
349 |
|
350 QVERIFY(shm.create(100)); |
|
351 QVERIFY(shm.lock()); |
|
352 QTest::ignoreMessage(QtWarningMsg, "QSharedMemory::lock: already locked"); |
|
353 QVERIFY(shm.lock()); |
|
354 // don't lock forever |
|
355 } |
|
356 |
|
357 /*! |
|
358 Other shared memory are allowed to be attached after we remove, |
|
359 but new shared memory are not allowed to attach after a remove. |
|
360 */ |
|
361 void tst_QSharedMemory::removeWhileAttached() |
|
362 { |
|
363 #ifdef Q_OS_HPUX |
|
364 QSKIP("HPUX doesn't allow for multiple attaches per process", SkipAll); |
|
365 #endif |
|
366 rememberKey("one"); |
|
367 |
|
368 // attach 1 |
|
369 QSharedMemory *smOne = new QSharedMemory(QLatin1String("one")); |
|
370 QVERIFY(smOne->create(1024)); |
|
371 QVERIFY(smOne->isAttached()); |
|
372 |
|
373 // attach 2 |
|
374 QSharedMemory *smTwo = new QSharedMemory(QLatin1String("one")); |
|
375 QVERIFY(smTwo->attach()); |
|
376 QVERIFY(smTwo->isAttached()); |
|
377 |
|
378 // detach 1 and remove, remove one first to catch another error. |
|
379 delete smOne; |
|
380 delete smTwo; |
|
381 |
|
382 // three shouldn't be able to attach |
|
383 QSharedMemory smThree(QLatin1String("one")); |
|
384 QVERIFY(!smThree.attach()); |
|
385 QCOMPARE(smThree.error(), QSharedMemory::NotFound); |
|
386 } |
|
387 |
|
388 /*! |
|
389 The memory should be set to 0 after created. |
|
390 */ |
|
391 void tst_QSharedMemory::emptyMemory() |
|
392 { |
|
393 QSharedMemory sm(rememberKey(QLatin1String("voidland"))); |
|
394 int size = 1024; |
|
395 QVERIFY(sm.create(size, QSharedMemory::ReadOnly)); |
|
396 char *get = (char*)sm.data(); |
|
397 char null = 0; |
|
398 for (int i = 0; i < size; ++i) |
|
399 QCOMPARE(get[i], null); |
|
400 } |
|
401 |
|
402 /*! |
|
403 Verify that attach with ReadOnly is actually read only |
|
404 by writing to data and causing a segfault. |
|
405 */ |
|
406 void tst_QSharedMemory::readOnly() |
|
407 { |
|
408 #ifdef Q_OS_WIN |
|
409 QSKIP("This test opens a crash dialog on Windows", SkipSingle); |
|
410 #endif |
|
411 #if defined (Q_OS_SYMBIAN) |
|
412 QSKIP("Readonly shared memory is not supported in symbian", SkipAll); |
|
413 #endif |
|
414 QString program = "./lackey/lackey"; |
|
415 QStringList arguments; |
|
416 rememberKey("readonly_segfault"); |
|
417 arguments << SRCDIR "lackey/scripts/readonly_segfault.js"; |
|
418 |
|
419 // ### on windows disable the popup somehow |
|
420 QProcess p; |
|
421 p.start(program, arguments); |
|
422 p.setProcessChannelMode(QProcess::ForwardedChannels); |
|
423 p.waitForFinished(); |
|
424 QCOMPARE(p.error(), QProcess::Crashed); |
|
425 } |
|
426 |
|
427 /*! |
|
428 Keep making shared memory until the kernel stops us. |
|
429 */ |
|
430 void tst_QSharedMemory::useTooMuchMemory() |
|
431 { |
|
432 #ifdef Q_OS_LINUX |
|
433 bool success = true; |
|
434 int count = 0; |
|
435 while (success) { |
|
436 QString key = QString("maxmemorytest_%1").arg(count++); |
|
437 QSharedMemory *sm = new QSharedMemory(rememberKey(key)); |
|
438 QVERIFY(sm); |
|
439 jail.append(sm); |
|
440 int size = 32768 * 1024; |
|
441 success = sm->create(size); |
|
442 if (!success && sm->error() == QSharedMemory::AlreadyExists) { |
|
443 // left over from a crash, clean it up |
|
444 sm->attach(); |
|
445 sm->detach(); |
|
446 success = sm->create(size); |
|
447 } |
|
448 |
|
449 if (!success) { |
|
450 QVERIFY(!sm->isAttached()); |
|
451 QCOMPARE(sm->key(), key); |
|
452 QCOMPARE(sm->size(), 0); |
|
453 QVERIFY(sm->data() == 0); |
|
454 if (sm->error() != QSharedMemory::OutOfResources) |
|
455 qDebug() << sm->error() << sm->errorString(); |
|
456 // ### Linux wont return OutOfResources if there are not enough semaphores to use. |
|
457 QVERIFY(sm->error() == QSharedMemory::OutOfResources |
|
458 || sm->error() == QSharedMemory::LockError); |
|
459 QVERIFY(sm->errorString() != QString()); |
|
460 QVERIFY(!sm->attach()); |
|
461 QVERIFY(!sm->detach()); |
|
462 } else { |
|
463 QVERIFY(sm->isAttached()); |
|
464 } |
|
465 } |
|
466 #endif |
|
467 } |
|
468 |
|
469 /*! |
|
470 Create one shared memory (government) and see how many other shared memories (wars) we can |
|
471 attach before the system runs out of resources. |
|
472 */ |
|
473 void tst_QSharedMemory::attachTooMuch() |
|
474 { |
|
475 QSKIP("disabled", SkipAll); |
|
476 #ifdef Q_OS_HPUX |
|
477 QSKIP("HPUX doesn't allow for multiple attaches per process", SkipAll); |
|
478 #endif |
|
479 #ifdef Q_OS_WINCE |
|
480 QSKIP("This nearly kills the system itself, so skip for Qt/WinCE", SkipAll); |
|
481 #endif |
|
482 QSharedMemory government(rememberKey("government")); |
|
483 QVERIFY(government.create(1024)); |
|
484 while (true) { |
|
485 QSharedMemory *war = new QSharedMemory(government.key()); |
|
486 QVERIFY(war); |
|
487 jail.append(war); |
|
488 if (!war->attach()) { |
|
489 QVERIFY(!war->isAttached()); |
|
490 QCOMPARE(war->key(), government.key()); |
|
491 QCOMPARE(war->size(), 0); |
|
492 QVERIFY(war->data() == 0); |
|
493 QCOMPARE(war->error(), QSharedMemory::OutOfResources); |
|
494 QVERIFY(war->errorString() != QString()); |
|
495 QVERIFY(!war->detach()); |
|
496 break; |
|
497 } else { |
|
498 QVERIFY(war->isAttached()); |
|
499 } |
|
500 } |
|
501 } |
|
502 |
|
503 void tst_QSharedMemory::simpleProducerConsumer_data() |
|
504 { |
|
505 QTest::addColumn<QSharedMemory::AccessMode>("mode"); |
|
506 |
|
507 QTest::newRow("readonly") << QSharedMemory::ReadOnly; |
|
508 QTest::newRow("readwrite") << QSharedMemory::ReadWrite; |
|
509 } |
|
510 |
|
511 /*! |
|
512 The basic consumer producer that rounds out the basic testing. |
|
513 If this fails then any muli-threading/process might fail (but be |
|
514 harder to debug) |
|
515 |
|
516 This doesn't require nor test any locking system. |
|
517 */ |
|
518 void tst_QSharedMemory::simpleProducerConsumer() |
|
519 { |
|
520 #ifdef Q_OS_HPUX |
|
521 QSKIP("HPUX doesn't allow for multiple attaches per process", SkipAll); |
|
522 #endif |
|
523 QFETCH(QSharedMemory::AccessMode, mode); |
|
524 |
|
525 rememberKey(QLatin1String("market")); |
|
526 QSharedMemory producer(QLatin1String("market")); |
|
527 QSharedMemory consumer(QLatin1String("market")); |
|
528 int size = 512; |
|
529 QVERIFY(producer.create(size)); |
|
530 QVERIFY(consumer.attach(mode)); |
|
531 |
|
532 char *put = (char*)producer.data(); |
|
533 char *get = (char*)consumer.data(); |
|
534 // On Windows CE you always have ReadWrite access. Thus |
|
535 // ViewMapOfFile returns the same pointer |
|
536 // On Symbian, the address will always be same, as |
|
537 // write protection of chunks is not currently supported by Symbian |
|
538 #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) |
|
539 QVERIFY(put != get); |
|
540 #endif |
|
541 for (int i = 0; i < size; ++i) { |
|
542 put[i] = 'Q'; |
|
543 QCOMPARE(get[i], 'Q'); |
|
544 } |
|
545 QVERIFY(consumer.detach()); |
|
546 } |
|
547 |
|
548 void tst_QSharedMemory::simpleDoubleProducerConsumer() |
|
549 { |
|
550 #ifdef Q_OS_HPUX |
|
551 QSKIP("HPUX doesn't allow for multiple attaches per process", SkipAll); |
|
552 #endif |
|
553 rememberKey(QLatin1String("market")); |
|
554 QSharedMemory producer(QLatin1String("market")); |
|
555 int size = 512; |
|
556 QVERIFY(producer.create(size)); |
|
557 QVERIFY(producer.detach()); |
|
558 QVERIFY(producer.create(size)); |
|
559 |
|
560 { |
|
561 QSharedMemory consumer(QLatin1String("market")); |
|
562 QVERIFY(consumer.attach()); |
|
563 } |
|
564 } |
|
565 |
|
566 class Consumer : public QThread |
|
567 { |
|
568 |
|
569 public: |
|
570 void run() |
|
571 { |
|
572 QSharedMemory consumer(QLatin1String("market")); |
|
573 while (!consumer.attach()) { |
|
574 if (consumer.error() != QSharedMemory::NotFound) |
|
575 qDebug() << "consumer: failed to connect" << consumer.error() << consumer.errorString(); |
|
576 QVERIFY(consumer.error() == QSharedMemory::NotFound || consumer.error() == QSharedMemory::KeyError); |
|
577 QTest::qWait(1); |
|
578 } |
|
579 |
|
580 char *memory = (char*)consumer.data(); |
|
581 |
|
582 int i = 0; |
|
583 while (true) { |
|
584 if(!consumer.lock()) |
|
585 break; |
|
586 if (memory[0] == 'Q') |
|
587 memory[0] = ++i; |
|
588 if (memory[0] == 'E') { |
|
589 memory[1]++; |
|
590 QVERIFY(consumer.unlock()); |
|
591 break; |
|
592 } |
|
593 QVERIFY(consumer.unlock()); |
|
594 QTest::qWait(1); |
|
595 } |
|
596 |
|
597 QVERIFY(consumer.detach()); |
|
598 } |
|
599 }; |
|
600 |
|
601 class Producer : public QThread |
|
602 { |
|
603 |
|
604 public: |
|
605 void run() |
|
606 { |
|
607 QSharedMemory producer(QLatin1String("market")); |
|
608 int size = 1024; |
|
609 if (!producer.create(size)) { |
|
610 // left over from a crash... |
|
611 if (producer.error() == QSharedMemory::AlreadyExists) { |
|
612 producer.attach(); |
|
613 producer.detach(); |
|
614 QVERIFY(producer.create(size)); |
|
615 } |
|
616 } |
|
617 QVERIFY(producer.isAttached()); |
|
618 char *memory = (char*)producer.data(); |
|
619 memory[1] = '0'; |
|
620 QTime timer; |
|
621 timer.start(); |
|
622 int i = 0; |
|
623 while (i < 5 && timer.elapsed() < 5000) { |
|
624 QVERIFY(producer.lock()); |
|
625 if (memory[0] == 'Q') { |
|
626 QVERIFY(producer.unlock()); |
|
627 QTest::qWait(1); |
|
628 continue; |
|
629 } |
|
630 ++i; |
|
631 memory[0] = 'Q'; |
|
632 QVERIFY(producer.unlock()); |
|
633 QTest::qWait(1); |
|
634 } |
|
635 |
|
636 // tell everyone to quit |
|
637 QVERIFY(producer.lock()); |
|
638 memory[0] = 'E'; |
|
639 QVERIFY(producer.unlock()); |
|
640 |
|
641 #if defined(Q_OS_SYMBIAN) |
|
642 // Sleep a while to ensure that consumers start properly |
|
643 QTest::qSleep(1000); |
|
644 #endif |
|
645 } |
|
646 private: |
|
647 |
|
648 }; |
|
649 |
|
650 void tst_QSharedMemory::simpleThreadedProducerConsumer_data() |
|
651 { |
|
652 QTest::addColumn<bool>("producerIsThread"); |
|
653 QTest::addColumn<int>("threads"); |
|
654 for (int i = 0; i < 5; ++i) { |
|
655 QTest::newRow("1 consumer, producer is thread") << true << 1; |
|
656 QTest::newRow("1 consumer, producer is this") << false << 1; |
|
657 QTest::newRow("5 consumers, producer is thread") << true << 5; |
|
658 QTest::newRow("5 consumers, producer is this") << false << 5; |
|
659 } |
|
660 } |
|
661 |
|
662 /*! |
|
663 The basic producer/consumer, but this time using threads. |
|
664 */ |
|
665 void tst_QSharedMemory::simpleThreadedProducerConsumer() |
|
666 { |
|
667 QFETCH(bool, producerIsThread); |
|
668 QFETCH(int, threads); |
|
669 rememberKey(QLatin1String("market")); |
|
670 |
|
671 #if defined Q_OS_HPUX && defined __ia64 |
|
672 QSKIP("This test locks up on gravlaks.troll.no", SkipSingle); |
|
673 #endif |
|
674 |
|
675 Producer p; |
|
676 #if defined(Q_OS_SYMBIAN) |
|
677 enum |
|
678 { |
|
679 /** |
|
680 * The maximum stack size. |
|
681 */ |
|
682 SymbianStackSize = 0x14000 |
|
683 }; |
|
684 p.setStackSize(SymbianStackSize); |
|
685 #endif |
|
686 if (producerIsThread) |
|
687 p.start(); |
|
688 |
|
689 QList<Consumer*> consumers; |
|
690 for (int i = 0; i < threads; ++i) { |
|
691 consumers.append(new Consumer()); |
|
692 #if defined(Q_OS_SYMBIAN) |
|
693 consumers.last()->setStackSize(SymbianStackSize); |
|
694 #endif |
|
695 consumers.last()->start(); |
|
696 } |
|
697 |
|
698 if (!producerIsThread) |
|
699 p.run(); |
|
700 |
|
701 p.wait(5000); |
|
702 while (!consumers.isEmpty()) { |
|
703 QVERIFY(consumers.first()->wait(5000)); |
|
704 delete consumers.takeFirst(); |
|
705 } |
|
706 } |
|
707 |
|
708 void tst_QSharedMemory::simpleProcessProducerConsumer_data() |
|
709 { |
|
710 QTest::addColumn<int>("processes"); |
|
711 int tries = 10; |
|
712 #ifdef Q_OS_WIN |
|
713 tries = 5; |
|
714 #endif |
|
715 for (int i = 0; i < tries; ++i) { |
|
716 QTest::newRow("1 process") << 1; |
|
717 QTest::newRow("5 processes") << 5; |
|
718 } |
|
719 } |
|
720 |
|
721 /*! |
|
722 Create external processes that produce and consume. |
|
723 */ |
|
724 void tst_QSharedMemory::simpleProcessProducerConsumer() |
|
725 { |
|
726 #if defined (Q_OS_SYMBIAN) && defined(Q_CC_NOKIAX86) |
|
727 QSKIP("Cannot launch multiple Qt processes in Symbian emulator", SkipAll); |
|
728 #endif |
|
729 QFETCH(int, processes); |
|
730 |
|
731 rememberKey("market"); |
|
732 |
|
733 #ifndef Q_OS_WINCE |
|
734 QStringList arguments = QStringList() << SRCDIR "lackey/scripts/producer.js"; |
|
735 #else |
|
736 QStringList arguments = QStringList() << QFileInfo(SRCDIR "lackey/scripts/producer.js").absoluteFilePath(); |
|
737 #endif |
|
738 QProcess producer; |
|
739 producer.setProcessChannelMode(QProcess::ForwardedChannels); |
|
740 producer.start( QFileInfo("./lackey/lackey.exe").absoluteFilePath(), arguments); |
|
741 producer.waitForStarted(); |
|
742 QVERIFY(producer.error() != QProcess::FailedToStart); |
|
743 |
|
744 QList<QProcess*> consumers; |
|
745 unsigned int failedProcesses = 0; |
|
746 for (int i = 0; i < processes; ++i) { |
|
747 #ifndef Q_OS_WINCE |
|
748 QStringList arguments = QStringList() << SRCDIR "lackey/scripts/consumer.js"; |
|
749 #else |
|
750 QStringList arguments = QStringList() << QFileInfo(SRCDIR "lackey/scripts/consumer.js").absoluteFilePath(); |
|
751 #endif |
|
752 QProcess *p = new QProcess; |
|
753 p->setProcessChannelMode(QProcess::ForwardedChannels); |
|
754 p->start("./lackey/lackey", arguments); |
|
755 |
|
756 if (p->waitForStarted(2000)) |
|
757 consumers.append(p); |
|
758 else |
|
759 ++failedProcesses; |
|
760 } |
|
761 |
|
762 producer.waitForFinished(5000); |
|
763 |
|
764 bool consumerFailed = false; |
|
765 |
|
766 while (!consumers.isEmpty()) { |
|
767 consumers.first()->waitForFinished(2000); |
|
768 if (consumers.first()->state() == QProcess::Running || |
|
769 consumers.first()->exitStatus() != QProcess::NormalExit || |
|
770 consumers.first()->exitCode() != 0) { |
|
771 consumerFailed = true; |
|
772 } |
|
773 delete consumers.takeFirst(); |
|
774 } |
|
775 QCOMPARE(consumerFailed, false); |
|
776 QCOMPARE(failedProcesses, (unsigned int)(0)); |
|
777 } |
|
778 |
|
779 QTEST_MAIN(tst_QSharedMemory) |
|
780 #include "tst_qsharedmemory.moc" |
|
781 |