Debian Codegraph @

tests/test_coverup_50.py

raw symbols SCIP
1 # file scalene/replacement_sem_lock.py:9-33
2 # lines [9, 10, 12, 13, 14, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 29, 30, 32, 33]
3 # branches ['12->13', '12->14', '19->20', '24->25', '24->27']
4
5 import multiprocessing
6 import pytest
7 import random
8 import sys
9 import threading
10 from scalene.replacement_sem_lock import ReplacementSemLock
11 from scalene.scalene_profiler import Scalene
12
13 # Mock Scalene methods used in ReplacementSemLock
14 Scalene.set_thread_sleeping = lambda x: None
15 Scalene.reset_thread_sleeping = lambda x: None
16
17
18 @pytest.fixture
19 def replacement_sem_lock():
20 lock = ReplacementSemLock()
21 yield lock
22 if lock._semlock._is_zero():
23 lock.release()
24
25
26 def test_replacement_sem_lock_enter_exit(replacement_sem_lock):
27 # Test the __enter__ method with a forced timeout
28 original_random = random.random
29 original_interval = sys.getswitchinterval()
30 sys.setswitchinterval(0.000001) # Set a very small interval to force a timeout
31 random.random = lambda: 1 # Force the maximum timeout
32
33 try:
34 with replacement_sem_lock:
35 pass # This block should be executed
36 finally:
37 # Restore the original functions
38 random.random = original_random
39 sys.setswitchinterval(original_interval)
40
41 # Test the __exit__ method
42 assert (
43 not replacement_sem_lock._semlock._is_zero()
44 ), "Lock should not be acquired yet"
45 with replacement_sem_lock:
46 assert replacement_sem_lock._semlock._is_zero(), "Lock should be acquired"
47 assert (
48 not replacement_sem_lock._semlock._is_zero()
49 ), "Lock should be released after the block"
50
51
52 def test_replacement_sem_lock_reduce(replacement_sem_lock):
53 # Test the __reduce__ method
54 reduced = replacement_sem_lock.__reduce__()
55 assert callable(
56 reduced[0]
57 ), "__reduce__ should return a callable as the first element"
58 assert (
59 reduced[1] == ()
60 ), "__reduce__ should return an empty tuple as the second element"