Skip to content

Commit 9cb1679

Browse files
committed
Strengthen release assertion and add cleanup guards in lock tests
1 parent fc10369 commit 9cb1679

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

openwisp_controller/connection/tests/test_models.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,34 +1066,35 @@ def test_device_update_config_not_in_progress(
10661066
conf.save()
10671067
mocked_get_working_connection.assert_called_once()
10681068
mocked_update_config.assert_called_once()
1069-
mocked_release.assert_called_once()
1069+
mocked_release.assert_called_once_with(
1070+
str(conf.device.pk), "fake-lock-token"
1071+
)
10701072

10711073
def test_acquire_update_config_lock(self):
10721074
"""Test that the lock can be acquired and prevents duplicate acquisition."""
10731075
device_id = "test-device-id"
10741076
# First acquisition should succeed and return a token
10751077
token = _acquire_update_config_lock(device_id)
1078+
self.addCleanup(_release_update_config_lock, device_id, token)
10761079
self.assertIsNotNone(token)
10771080
# Second acquisition should fail (lock already held)
10781081
self.assertIsNone(_acquire_update_config_lock(device_id))
10791082
# After releasing with correct token, acquisition should succeed again
10801083
_release_update_config_lock(device_id, token)
10811084
token2 = _acquire_update_config_lock(device_id)
1085+
self.addCleanup(_release_update_config_lock, device_id, token2)
10821086
self.assertIsNotNone(token2)
1083-
# Cleanup
1084-
_release_update_config_lock(device_id, token2)
10851087

10861088
def test_release_update_config_lock_wrong_token(self):
10871089
"""Only the lock owner can release the lock."""
10881090
device_id = "test-device-id"
10891091
token = _acquire_update_config_lock(device_id)
1092+
self.addCleanup(_release_update_config_lock, device_id, token)
10901093
self.assertIsNotNone(token)
10911094
# Releasing with wrong token should not delete the lock
10921095
_release_update_config_lock(device_id, "wrong-token")
10931096
# Lock should still be held
10941097
self.assertIsNone(_acquire_update_config_lock(device_id))
1095-
# Releasing with correct token should work
1096-
_release_update_config_lock(device_id, token)
10971098

10981099
@mock.patch(_connect_path)
10991100
def test_schedule_command_called(self, connect_mocked):

0 commit comments

Comments
 (0)