Fix bug 686 - "Request to make Q[Mutex|Read|Write]Locker context managers"

Reviewer: Marcelo Lira <marcelo.lira@openbossa.org>
          Luciano Wolf <luciano.wolf@openbossa.org>
This commit is contained in:
Hugo Parente Lima 2011-05-17 16:34:08 -03:00
commit 952f9f0bda
3 changed files with 120 additions and 2 deletions

View file

@ -7,6 +7,7 @@ PYSIDE_TEST(bug_505.py)
PYSIDE_TEST(bug_515.py)
PYSIDE_TEST(bug_606.py)
PYSIDE_TEST(bug_656.py)
PYSIDE_TEST(bug_686.py)
PYSIDE_TEST(bug_699.py)
PYSIDE_TEST(bug_706.py)
PYSIDE_TEST(bug_723.py)

85
tests/QtCore/bug_686.py Normal file
View file

@ -0,0 +1,85 @@
import unittest
from PySide.QtCore import *
class MyWriteThread(QThread):
def __init__(self, lock):
QThread.__init__(self)
self.lock = lock
self.started = False
self.canQuit = False
def run(self):
self.started = True
while not self.lock.tryLockForWrite():
pass
self.canQuit = True
class MyReadThread(QThread):
def __init__(self, lock):
QThread.__init__(self)
self.lock = lock
self.started = False
self.canQuit = False
def run(self):
self.started = True
while not self.lock.tryLockForRead():
pass
self.canQuit = True
class MyMutexedThread(QThread):
def __init__(self, mutex):
QThread.__init__(self)
self.mutex = mutex
self.started = False
self.canQuit = False
def run(self):
self.started = True
while not self.mutex.tryLock():
pass
self.canQuit = True
class TestQMutex (unittest.TestCase):
def testReadLocker(self):
lock = QReadWriteLock()
thread = MyWriteThread(lock)
with QReadLocker(lock):
thread.start()
while not thread.started:
pass
self.assertFalse(thread.canQuit)
thread.wait()
self.assertTrue(thread.canQuit)
def testWriteLocker(self):
lock = QReadWriteLock()
thread = MyReadThread(lock)
with QWriteLocker(lock):
thread.start()
while not thread.started:
pass
self.assertFalse(thread.canQuit)
thread.wait()
self.assertTrue(thread.canQuit)
def testMutexLocker(self):
mutex = QMutex()
thread = MyMutexedThread(mutex)
with QMutexLocker(mutex):
thread.start()
while not thread.started:
pass
self.assertFalse(thread.canQuit)
thread.wait()
self.assertTrue(thread.canQuit)
if __name__ == '__main__':
unittest.main()