33import threading
44
55from xarray .backends import locks
6+ from xarray .backends .locks import CombinedLock , SerializableLock
67
78
89def test_threaded_lock () -> None :
@@ -13,3 +14,77 @@ def test_threaded_lock() -> None:
1314
1415 lock3 = locks ._get_threaded_lock ("bar" )
1516 assert lock1 is not lock3
17+
18+
19+ def test_combined_lock_locked_returns_false_when_no_locks_acquired () -> None :
20+ """CombinedLock.locked() should return False when no locks are held."""
21+ lock1 = threading .Lock ()
22+ lock2 = threading .Lock ()
23+ combined = CombinedLock ([lock1 , lock2 ])
24+
25+ assert combined .locked () is False
26+ assert lock1 .locked () is False
27+ assert lock2 .locked () is False
28+
29+
30+ def test_combined_lock_locked_returns_true_when_one_lock_acquired () -> None :
31+ """CombinedLock.locked() should return True when any lock is held."""
32+ lock1 = threading .Lock ()
33+ lock2 = threading .Lock ()
34+ combined = CombinedLock ([lock1 , lock2 ])
35+
36+ lock1 .acquire ()
37+ try :
38+ assert combined .locked () is True
39+ finally :
40+ lock1 .release ()
41+
42+ assert combined .locked () is False
43+
44+
45+ def test_combined_lock_locked_returns_true_when_all_locks_acquired () -> None :
46+ """CombinedLock.locked() should return True when all locks are held."""
47+ lock1 = threading .Lock ()
48+ lock2 = threading .Lock ()
49+ combined = CombinedLock ([lock1 , lock2 ])
50+
51+ lock1 .acquire ()
52+ lock2 .acquire ()
53+ try :
54+ assert combined .locked () is True
55+ finally :
56+ lock1 .release ()
57+ lock2 .release ()
58+
59+ assert combined .locked () is False
60+
61+
62+ def test_combined_lock_locked_with_serializable_locks () -> None :
63+ """CombinedLock.locked() should work with SerializableLock instances."""
64+ lock1 = SerializableLock ()
65+ lock2 = SerializableLock ()
66+ combined = CombinedLock ([lock1 , lock2 ])
67+
68+ assert combined .locked () is False
69+
70+ lock1 .acquire ()
71+ try :
72+ assert combined .locked () is True
73+ finally :
74+ lock1 .release ()
75+
76+ assert combined .locked () is False
77+
78+
79+ def test_combined_lock_locked_with_context_manager () -> None :
80+ """CombinedLock.locked() should reflect state when using context manager."""
81+ lock1 = threading .Lock ()
82+ lock2 = threading .Lock ()
83+ combined = CombinedLock ([lock1 , lock2 ])
84+
85+ assert combined .locked () is False
86+
87+ with combined :
88+ assert combined .locked () is True
89+
90+ assert combined .locked () is False
0 commit comments