aboutsummaryrefslogtreecommitdiffstats
path: root/tests/QtCore/bug_686.py
blob: 984e007125b26b91846bf6e0bd2919ef56c21721 (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
from __future__ import with_statement

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()