-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReaderWriterLockUnitTest.py
172 lines (155 loc) · 6.02 KB
/
ReaderWriterLockUnitTest.py
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
import unittest
import threading
import time
from ReaderWriterLock import ReaderWriterLock
class WriterThread(threading.Thread):
def __init__(self, rw_lock: ReaderWriterLock, lock_timeout: float, wait_timeout: float, working_list: list,
new_value):
self.write_acquired = False
self.write_lock_held = False
self._rw_lock = rw_lock
self._lock_timeout = lock_timeout
self._wait_timeout = wait_timeout
self.working_list = working_list
self._new_value = new_value
threading.Thread.__init__(self)
def run(self):
self.write_acquired = self._rw_lock.enter_write_lock(self._lock_timeout)
self.write_lock_held = self._rw_lock.is_write_lock_held
if not self.write_acquired:
return
time.sleep(self._wait_timeout)
self.working_list[0] = self._new_value
self._rw_lock.exit_write_lock()
class ReaderThread(threading.Thread):
def __init__(self, rw_lock: ReaderWriterLock, lock_timeout: float, wait_timeout: float, working_list: list):
self.read_acquired = False
self.read_lock_held = False
self._rw_lock = rw_lock
self._lock_timeout = lock_timeout
self._wait_timeout = wait_timeout
self.working_list = working_list
self.value_read = None
threading.Thread.__init__(self)
def run(self):
self.read_acquired = self._rw_lock.enter_read_lock(self._lock_timeout)
self.read_lock_held = self._rw_lock.is_read_lock_held
if not self.read_acquired:
return
time.sleep(self._wait_timeout)
self.value_read = self.working_list[0]
self._rw_lock.exit_read_lock()
class TestReaderWriterLock(unittest.TestCase):
def test_reading_before_writing(self):
rw_lock = ReaderWriterLock()
working_list = [1]
writer1 = WriterThread(rw_lock, -1, 0, working_list, 2)
writer1.start()
writer1.join()
self.assertTrue(writer1.write_acquired)
self.assertEqual(working_list[0], 2)
reader1 = ReaderThread(rw_lock, -1, 3, working_list)
reader1.start()
time.sleep(1)
writer2 = WriterThread(rw_lock, -1, 0, working_list, 3)
writer2.start()
reader1.join()
writer2.join()
self.assertTrue(reader1.read_acquired)
self.assertEqual(reader1.value_read, 2)
self.assertTrue(writer2.write_acquired)
self.assertEqual(working_list[0], 3)
def test_writing_before_reading(self):
rw_lock = ReaderWriterLock()
working_list = [1]
writer1 = WriterThread(rw_lock, -1, 3, working_list, 2)
writer1.start()
time.sleep(1)
reader1 = ReaderThread(rw_lock, -1, 0, working_list)
reader1.start()
reader1.join()
writer1.join()
self.assertTrue(reader1.read_acquired)
self.assertTrue(writer1.write_acquired)
self.assertEqual(reader1.value_read, 2)
self.assertEqual(working_list[0], 2)
def test_write_starvation(self):
rw_lock = ReaderWriterLock()
working_list = [1]
reader1 = ReaderThread(rw_lock, -1, 5, working_list)
reader1.start()
time.sleep(1)
writer1 = WriterThread(rw_lock, -1, 0, working_list, 2)
writer1.start()
time.sleep(1)
reader2 = ReaderThread(rw_lock, -1, 0, working_list)
reader2.start()
reader1.join()
reader2.join()
writer1.join()
self.assertEqual(reader1.value_read, 1)
self.assertEqual(reader2.value_read, 2)
self.assertEqual(working_list[0], 2)
def test_enter_timeout(self):
rw_lock = ReaderWriterLock()
working_list = [1]
writer1 = WriterThread(rw_lock, -1, 3, working_list, 2)
writer1.start()
time.sleep(1)
reader1 = ReaderThread(rw_lock, 0.1, 0, working_list)
reader1.start()
writer2 = WriterThread(rw_lock, 0.1, 0, working_list, 3)
writer2.start()
reader1.join()
writer2.join()
writer1.join()
self.assertTrue(writer1.write_acquired)
self.assertFalse(reader1.read_acquired)
self.assertFalse(writer2.write_acquired)
self.assertEqual(working_list[0], 2)
def test_waiting_writer_timeout(self):
rw_lock = ReaderWriterLock()
working_list = [1]
reader1 = ReaderThread(rw_lock, -1, 5, working_list)
reader1.start()
time.sleep(1)
writer1 = WriterThread(rw_lock, -1, 0, working_list, 2)
writer1.start()
time.sleep(1)
reader2 = ReaderThread(rw_lock, 0.1, 0, working_list)
reader2.start()
reader1.join()
reader2.join()
writer1.join()
self.assertTrue(reader1.read_acquired)
self.assertFalse(reader2.read_acquired)
self.assertTrue(writer1.write_acquired)
self.assertEqual(reader1.value_read, 1)
self.assertEqual(working_list[0], 2)
def test_properties(self):
rw_lock = ReaderWriterLock()
working_list = [1]
self.assertEqual(rw_lock.current_read_count, 0)
reader1 = ReaderThread(rw_lock, -1, 3, working_list)
reader1.start()
time.sleep(1)
self.assertFalse(rw_lock.is_read_lock_held)
self.assertEqual(rw_lock.current_read_count, 1)
writer1 = WriterThread(rw_lock, -1, 3, working_list, 2)
writer1.start()
time.sleep(1)
self.assertFalse(rw_lock.is_write_lock_held)
reader1.join()
writer1.join()
self.assertTrue(reader1.read_lock_held)
self.assertTrue(writer1.write_lock_held)
def test_upgrade_read_to_write_lock(self):
rw_lock = ReaderWriterLock()
rw_lock.enter_read_lock()
self.assertTrue(rw_lock.is_read_lock_held)
rw_lock.upgrade_read_to_write_lock()
self.assertTrue(rw_lock.is_write_lock_held)
self.assertFalse(rw_lock.is_read_lock_held)
rw_lock.downgrade_write_to_read_lock()
self.assertTrue(rw_lock.is_read_lock_held)
self.assertFalse(rw_lock.is_write_lock_held)