|
@@ -3,6 +3,7 @@ import time
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
+from ..helpers import daemonize
|
|
|
from ..platform import get_process_id, process_alive
|
|
|
from ..locking import TimeoutTimer, ExclusiveLock, Lock, LockRoster, \
|
|
|
ADD, REMOVE, SHARED, EXCLUSIVE, LockTimeout, NotLocked, NotMyLock
|
|
@@ -76,6 +77,19 @@ class TestExclusiveLock:
|
|
|
with pytest.raises(LockTimeout):
|
|
|
ExclusiveLock(lockpath, id=our_id, kill_stale_locks=True, timeout=0.1).acquire()
|
|
|
|
|
|
+ def test_migrate_lock(self, lockpath):
|
|
|
+ old_id, new_id = ID1, ID2
|
|
|
+ assert old_id[1] != new_id[1] # different PIDs (like when doing daemonize())
|
|
|
+ lock = ExclusiveLock(lockpath, id=old_id).acquire()
|
|
|
+ assert lock.id == old_id # lock is for old id / PID
|
|
|
+ old_unique_name = lock.unique_name
|
|
|
+ assert lock.by_me() # we have the lock
|
|
|
+ lock.migrate_lock(old_id, new_id) # fix the lock
|
|
|
+ assert lock.id == new_id # lock corresponds to the new id / PID
|
|
|
+ new_unique_name = lock.unique_name
|
|
|
+ assert lock.by_me() # we still have the lock
|
|
|
+ assert old_unique_name != new_unique_name # locking filename is different now
|
|
|
+
|
|
|
|
|
|
class TestLock:
|
|
|
def test_shared(self, lockpath):
|
|
@@ -155,6 +169,22 @@ class TestLock:
|
|
|
with pytest.raises(LockTimeout):
|
|
|
Lock(lockpath, id=our_id, kill_stale_locks=True, timeout=0.1).acquire()
|
|
|
|
|
|
+ def test_migrate_lock(self, lockpath):
|
|
|
+ old_id, new_id = ID1, ID2
|
|
|
+ assert old_id[1] != new_id[1] # different PIDs (like when doing daemonize())
|
|
|
+
|
|
|
+ lock = Lock(lockpath, id=old_id, exclusive=True).acquire()
|
|
|
+ assert lock.id == old_id
|
|
|
+ lock.migrate_lock(old_id, new_id) # fix the lock
|
|
|
+ assert lock.id == new_id
|
|
|
+ lock.release()
|
|
|
+
|
|
|
+ lock = Lock(lockpath, id=old_id, exclusive=False).acquire()
|
|
|
+ assert lock.id == old_id
|
|
|
+ lock.migrate_lock(old_id, new_id) # fix the lock
|
|
|
+ assert lock.id == new_id
|
|
|
+ lock.release()
|
|
|
+
|
|
|
|
|
|
@pytest.fixture()
|
|
|
def rosterpath(tmpdir):
|
|
@@ -207,3 +237,14 @@ class TestLockRoster:
|
|
|
other_killer_roster = LockRoster(rosterpath, kill_stale_locks=True)
|
|
|
# Did not kill us, since we're alive
|
|
|
assert other_killer_roster.get(SHARED) == {our_id, cant_know_if_dead_id}
|
|
|
+
|
|
|
+ def test_migrate_lock(self, rosterpath):
|
|
|
+ old_id, new_id = ID1, ID2
|
|
|
+ assert old_id[1] != new_id[1] # different PIDs (like when doing daemonize())
|
|
|
+ roster = LockRoster(rosterpath, id=old_id)
|
|
|
+ assert roster.id == old_id
|
|
|
+ roster.modify(SHARED, ADD)
|
|
|
+ assert roster.get(SHARED) == {old_id}
|
|
|
+ roster.migrate_lock(SHARED, old_id, new_id) # fix the lock
|
|
|
+ assert roster.id == new_id
|
|
|
+ assert roster.get(SHARED) == {new_id}
|