def test_extend_another_instance(conn): """It is possible to extend a lock using another instance of Lock with the same name. """ name = 'foobar' key_name = 'lock:' + name lock = Lock(conn, name, expire=100) lock.acquire() assert 0 <= conn.ttl(key_name) <= 100 another_lock = Lock(conn, name, id=lock.id) another_lock.extend(1000) assert conn.ttl(key_name) > 100
def test_given_id(conn): """It is possible to extend a lock using another instance of Lock with the same name. """ name = 'foobar' key_name = 'lock:' + name orig = Lock(conn, name, expire=100, id=b"a") orig.acquire() pytest.raises(TypeError, Lock, conn, name, id=object()) lock = Lock(conn, name, id=b"a") pytest.raises(AlreadyAcquired, lock.acquire) lock.extend(100) lock.release() # this works, note that this ain't the object that acquired the lock pytest.raises(NotAcquired, orig.release) # and this fails because lock was released above assert conn.ttl(key_name) == -2
def test_extend_another_instance_different_id_fail(conn): """It is impossible to extend a lock using another instance of Lock with the same name, but different id. """ name = 'foobar' key_name = 'lock:' + name lock = Lock(conn, name, expire=100) lock.acquire() assert 0 <= conn.ttl(key_name) <= 100 another_lock = Lock(conn, name) with pytest.raises(NotAcquired): another_lock.extend(1000) assert conn.ttl(key_name) <= 100 assert lock.id != another_lock.id