@@ -20,46 +20,47 @@ def test_get_unclaimed_lock():
2020 pass
2121
2222
23- def background_task (django_db_blocker ):
24- # HACK: as a thread the pytest.mark.django_db will not work
25- django_db_blocker .unblock ()
26- with advisory_lock ('background_task_lock' ):
27- time .sleep (0.1 )
28-
29-
30- @pytest .mark .django_db
31- def test_determine_lock_is_held (django_db_blocker ):
32- if connection .vendor == 'sqlite' :
33- pytest .skip ('Advisory lock is not written for sqlite' )
34- thread = threading .Thread (target = background_task , args = (django_db_blocker ,))
35- thread .start ()
36- for _ in range (5 ):
37- with advisory_lock ('background_task_lock' , wait = False ) as held :
38- if held is False :
39- break
40- time .sleep (0.01 )
41- else :
42- raise RuntimeError ('Other thread never obtained lock' )
43- thread .join ()
44-
45-
46- @pytest .mark .django_db
47- def test_tuple_lock ():
48- with advisory_lock ([1234 , 4321 ]):
49- pass
50-
51-
52- @pytest .mark .django_db
53- def test_invalid_tuple_name ():
54- with pytest .raises (ValueError ):
55- with advisory_lock (['test_invalid_tuple_name' , 'foo' ]):
23+ class TestAdvisoryLock :
24+ @pytest .fixture (autouse = True )
25+ def skip_if_sqlite (self ):
26+ if connection .vendor == 'sqlite' :
27+ pytest .skip ('Advisory lock is not written for sqlite' )
28+
29+ @staticmethod
30+ def background_task (django_db_blocker ):
31+ # HACK: as a thread the pytest.mark.django_db will not work
32+ django_db_blocker .unblock ()
33+ with advisory_lock ('background_task_lock' ):
34+ time .sleep (0.1 )
35+
36+ @pytest .mark .django_db
37+ def test_determine_lock_is_held (self , django_db_blocker ):
38+ thread = threading .Thread (target = TestAdvisoryLock .background_task , args = (django_db_blocker ,))
39+ thread .start ()
40+ for _ in range (5 ):
41+ with advisory_lock ('background_task_lock' , wait = False ) as held :
42+ if held is False :
43+ break
44+ time .sleep (0.01 )
45+ else :
46+ raise RuntimeError ('Other thread never obtained lock' )
47+ thread .join ()
48+
49+ @pytest .mark .django_db
50+ def test_tuple_lock (self ):
51+ with advisory_lock ([1234 , 4321 ]):
5652 pass
5753
58-
59- @pytest .mark .django_db
60- def test_lock_session_timeout_milliseconds ():
61- with pytest .raises (OperationalError ) as exc :
62- # uses miliseconds units
63- with advisory_lock ('test_lock_session_timeout_milliseconds' , lock_session_timeout_milliseconds = 2 ):
64- time .sleep (3 )
65- assert 'the connection is lost' in str (exc )
54+ @pytest .mark .django_db
55+ def test_invalid_tuple_name (self ):
56+ with pytest .raises (ValueError ):
57+ with advisory_lock (['test_invalid_tuple_name' , 'foo' ]):
58+ pass
59+
60+ @pytest .mark .django_db
61+ def test_lock_session_timeout_milliseconds (self ):
62+ with pytest .raises (OperationalError ) as exc :
63+ # uses miliseconds units
64+ with advisory_lock ('test_lock_session_timeout_milliseconds' , lock_session_timeout_milliseconds = 2 ):
65+ time .sleep (3 )
66+ assert 'the connection is lost' in str (exc )
0 commit comments