You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It contains several useful additions to the standard thread synchronization tools, such as lock protocols and locks with advanced functionality.
15
16
@@ -19,6 +20,7 @@ It contains several useful additions to the standard thread synchronization tool
19
20
-[**Installation**](#installation)
20
21
-[**Lock protocols**](#lock-protocols)
21
22
-[**SmartLock - deadlock is impossible with it**](#smartlock---deadlock-is-impossible-with-it)
23
+
-[**Test your locks**](#test-your-locks)
22
24
23
25
24
26
## Installation
@@ -170,3 +172,42 @@ If you want to catch the exception, import this from the `locklib` too:
170
172
```python
171
173
from locklib import DeadLockError
172
174
```
175
+
176
+
177
+
## Test your locks
178
+
179
+
Sometimes, when testing a code, you may need to detect if some action is taking place inside the lock. How to do this with a minimum of code? There is the `LockTraceWrapper` for this. It is a wrapper around a regular lock, which records it every time the code takes a lock or releases it. At the same time, the functionality of the wrapped lock is fully preserved.
180
+
181
+
It's easy to create an object of such a lock. Just pass any other lock to the class constructor:
182
+
183
+
```python
184
+
from threading import Lock
185
+
from locklib import LockTraceWrapper
186
+
187
+
lock = LockTraceWrapper(Lock())
188
+
```
189
+
190
+
You can use it in the same way as the wrapped lock:
191
+
192
+
```python
193
+
with lock:
194
+
...
195
+
```
196
+
197
+
Anywhere in your program, you can "inform" the lock that the action you need is being performed here:
198
+
199
+
```python
200
+
lock.notify('event_name')
201
+
```
202
+
203
+
And! Now you can easily identify if there were cases when an event with this identifier did not occur under the mutex. To do this, use the `was_event_locked` method:
204
+
205
+
```python
206
+
lock.was_event_locked('event_name')
207
+
```
208
+
209
+
If the `notify` method was called with the same parameter only when the lock activated, it will return `True`. If not, that is, if there was at least one case when the c method was called with such an identifier without an activated mutex, `False` will be returned.
210
+
211
+
How does it work? A modified [algorithm for determining the correct parenthesis sequence](https://ru.wikipedia.org/wiki/%D0%9F%D1%80%D0%B0%D0%B2%D0%B8%D0%BB%D1%8C%D0%BD%D0%B0%D1%8F_%D1%81%D0%BA%D0%BE%D0%B1%D0%BE%D1%87%D0%BD%D0%B0%D1%8F_%D0%BF%D0%BE%D1%81%D0%BB%D0%B5%D0%B4%D0%BE%D0%B2%D0%B0%D1%82%D0%B5%D0%BB%D1%8C%D0%BD%D0%BE%D1%81%D1%82%D1%8C) is used here. For each thread for which any events were registered (taking the mutex, releasing the mutex, and also calling the `notify` method), the check takes place separately, that is, we determine that it was the same thread that held the mutex when `notify` was called, and not some other one.
212
+
213
+
> ⚠️ The thread id is used to identify the streams. This id may be reused if the current thread ends, which in some cases may lead to incorrect identification of lock coverage for operations that were not actually covered by the lock. Make sure that this cannot happen during your test.
0 commit comments