|
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 |
|
45 #include <QCoreApplication> |
|
46 #include <QReadLocker> |
|
47 #include <QSemaphore> |
|
48 #include <QThread> |
|
49 |
|
50 //TESTED_CLASS= |
|
51 //TESTED_FILES= |
|
52 |
|
53 class tst_QReadLockerThread : public QThread |
|
54 { |
|
55 public: |
|
56 QReadWriteLock lock; |
|
57 QSemaphore semaphore, testSemaphore; |
|
58 |
|
59 void waitForTest() |
|
60 { |
|
61 semaphore.release(); |
|
62 testSemaphore.acquire(); |
|
63 } |
|
64 }; |
|
65 |
|
66 class tst_QReadLocker : public QObject |
|
67 { |
|
68 Q_OBJECT |
|
69 |
|
70 public: |
|
71 tst_QReadLocker(); |
|
72 ~tst_QReadLocker(); |
|
73 |
|
74 tst_QReadLockerThread *thread; |
|
75 |
|
76 void waitForThread() |
|
77 { |
|
78 thread->semaphore.acquire(); |
|
79 } |
|
80 void releaseThread() |
|
81 { |
|
82 thread->testSemaphore.release(); |
|
83 } |
|
84 |
|
85 private slots: |
|
86 void scopeTest(); |
|
87 void unlockAndRelockTest(); |
|
88 void lockerStateTest(); |
|
89 }; |
|
90 |
|
91 tst_QReadLocker::tst_QReadLocker() |
|
92 { |
|
93 } |
|
94 |
|
95 tst_QReadLocker::~tst_QReadLocker() |
|
96 { |
|
97 } |
|
98 |
|
99 void tst_QReadLocker::scopeTest() |
|
100 { |
|
101 class ScopeTestThread : public tst_QReadLockerThread |
|
102 { |
|
103 public: |
|
104 void run() |
|
105 { |
|
106 waitForTest(); |
|
107 |
|
108 { |
|
109 QReadLocker locker(&lock); |
|
110 waitForTest(); |
|
111 } |
|
112 |
|
113 waitForTest(); |
|
114 } |
|
115 }; |
|
116 |
|
117 thread = new ScopeTestThread; |
|
118 thread->start(); |
|
119 |
|
120 waitForThread(); |
|
121 // lock should be unlocked before entering the scope that creates the QReadLocker |
|
122 QVERIFY(thread->lock.tryLockForWrite()); |
|
123 thread->lock.unlock(); |
|
124 releaseThread(); |
|
125 |
|
126 waitForThread(); |
|
127 // lock should be locked by the QReadLocker |
|
128 QVERIFY(!thread->lock.tryLockForWrite()); |
|
129 releaseThread(); |
|
130 |
|
131 waitForThread(); |
|
132 // lock should be unlocked when the QReadLocker goes out of scope |
|
133 QVERIFY(thread->lock.tryLockForWrite()); |
|
134 thread->lock.unlock(); |
|
135 releaseThread(); |
|
136 |
|
137 QVERIFY(thread->wait()); |
|
138 |
|
139 delete thread; |
|
140 thread = 0; |
|
141 } |
|
142 |
|
143 |
|
144 void tst_QReadLocker::unlockAndRelockTest() |
|
145 { |
|
146 class UnlockAndRelockThread : public tst_QReadLockerThread |
|
147 { |
|
148 public: |
|
149 void run() |
|
150 { |
|
151 QReadLocker locker(&lock); |
|
152 |
|
153 waitForTest(); |
|
154 |
|
155 locker.unlock(); |
|
156 |
|
157 waitForTest(); |
|
158 |
|
159 locker.relock(); |
|
160 |
|
161 waitForTest(); |
|
162 } |
|
163 }; |
|
164 |
|
165 thread = new UnlockAndRelockThread; |
|
166 thread->start(); |
|
167 |
|
168 waitForThread(); |
|
169 // lock should be locked by the QReadLocker |
|
170 QVERIFY(!thread->lock.tryLockForWrite()); |
|
171 releaseThread(); |
|
172 |
|
173 waitForThread(); |
|
174 // lock has been explicitly unlocked via QReadLocker |
|
175 QVERIFY(thread->lock.tryLockForWrite()); |
|
176 thread->lock.unlock(); |
|
177 releaseThread(); |
|
178 |
|
179 waitForThread(); |
|
180 // lock has been explicity relocked via QReadLocker |
|
181 QVERIFY(!thread->lock.tryLockForWrite()); |
|
182 releaseThread(); |
|
183 |
|
184 QVERIFY(thread->wait()); |
|
185 |
|
186 delete thread; |
|
187 thread = 0; |
|
188 } |
|
189 |
|
190 void tst_QReadLocker::lockerStateTest() |
|
191 { |
|
192 class LockerStateThread : public tst_QReadLockerThread |
|
193 { |
|
194 public: |
|
195 void run() |
|
196 { |
|
197 { |
|
198 QReadLocker locker(&lock); |
|
199 locker.relock(); |
|
200 locker.unlock(); |
|
201 |
|
202 waitForTest(); |
|
203 } |
|
204 |
|
205 waitForTest(); |
|
206 } |
|
207 }; |
|
208 |
|
209 thread = new LockerStateThread; |
|
210 thread->start(); |
|
211 |
|
212 waitForThread(); |
|
213 // even though we relock() after creating the QReadLocker, it shouldn't lock the lock more than once |
|
214 QVERIFY(thread->lock.tryLockForWrite()); |
|
215 thread->lock.unlock(); |
|
216 releaseThread(); |
|
217 |
|
218 waitForThread(); |
|
219 // if we call QReadLocker::unlock(), its destructor should do nothing |
|
220 QVERIFY(thread->lock.tryLockForWrite()); |
|
221 thread->lock.unlock(); |
|
222 releaseThread(); |
|
223 |
|
224 QVERIFY(thread->wait()); |
|
225 |
|
226 delete thread; |
|
227 thread = 0; |
|
228 } |
|
229 |
|
230 QTEST_MAIN(tst_QReadLocker) |
|
231 #include "tst_qreadlocker.moc" |