async def test_getters(lock: Lock) -> None: assert lock.scene assert lock.project ap = next(ap for ap in lock.project.action_points if ap.name == "ap") ap2 = next(ap for ap in lock.project.action_points if ap.name == "ap2") ap_ap = next(ap for ap in lock.project.action_points if ap.name == "ap_ap") ap_ap_ap = next(ap for ap in lock.project.action_points if ap.name == "ap_ap_ap") ori = lock.project.ap_orientations(ap_ap_ap.id)[0] scene_object = next(iter(lock.scene.objects)) # test object getter for obj in (ori, ap_ap_ap, ap_ap, ap, ap2, scene_object): assert lock.get_by_id(obj.id) == obj # test parents getter parents = lock.get_all_parents(ori.id) test_parents = (ap_ap_ap.id, ap_ap.id, ap.id) assert len(parents) == len(test_parents) for item in test_parents: assert item in parents # for scene only lock.project = None assert not lock.get_all_parents(scene_object.id)
async def test_whole_lock_yield(lock: Lock) -> None: assert lock.scene assert lock.project test = "test" async with lock.get_lock() as val: assert isinstance(val, asyncio.Lock) assert lock._lock.locked() async with lock.get_lock(dry_run=True) as val: assert val is None assert not lock._lock.locked() await lock.write_lock(lock.scene.id, test) with pytest.raises(CannotLock): async with lock.get_lock(): pass await lock.write_unlock(lock.scene.id, test)
def lock() -> Lock: """Creates lock with initialized scene and project.""" test = "test" lock = Lock({}) scene = UpdateableCachedScene(cmn.Scene(test, description=test)) lock.scene = scene project = UpdateableCachedProject(cmn.Project(test, lock.scene.id, description=test, has_logic=True)) lock.project = project assert lock.scene == scene assert lock.scene_or_exception() == scene assert lock.project == project assert lock.project_or_exception() == project # add some scene and project objects test_object = cmn.SceneObject(test, "TestType") lock.scene.upsert_object(test_object) ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap", cmn.Position(0, 0, 0)) ap_ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap", cmn.Position(0, 0, 1), ap.id) ap_ap_ap = lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap_ap", cmn.Position(0, 0, 2), ap_ap.id) lock.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap2", cmn.Position(0, 1, 0)) ori = cmn.NamedOrientation("ori", cmn.Orientation()) lock.project.upsert_orientation(ap_ap_ap.id, ori) action = cmn.Action("action", "test/type", parameters=[], flows=[]) lock.project.upsert_action(ap_ap_ap.id, action) return lock
async def test_root_getter(lock: Lock) -> None: assert lock.scene assert lock.project ap = next(ap for ap in lock.project.action_points if ap.name == "ap") ap_ap = next(ap for ap in lock.project.action_points if ap.name == "ap_ap") scene_object_id = next(iter(lock.scene.object_ids)) assert await lock.get_root_id(lock.scene.id) == lock.scene.id assert await lock.get_root_id(lock.project.id) == lock.project.id assert await lock.get_root_id(scene_object_id) == scene_object_id assert await lock.get_root_id(ap_ap.id) == ap.id assert await lock.get_root_id(ap.id) == ap.id # root getter for scene only lock.project = None assert await lock.get_root_id(scene_object_id) == scene_object_id
async def test_auto_release(lock: Lock) -> None: assert lock.project test = "test" ap = next(ap for ap in lock.project.action_points if ap.name == "ap") ap_ap = next(ap for ap in lock.project.action_points if ap.name == "ap_ap") ap_ap_ap = next(ap for ap in lock.project.action_points if ap.name == "ap_ap_ap") ap2 = next(ap for ap in lock.project.action_points if ap.name == "ap2") ori = lock.project.ap_orientations(ap_ap_ap.id)[0] action = lock.project.ap_actions(ap_ap_ap.id)[0] # Test auto-release of locks and auto locking of child in tree lock.LOCK_TIMEOUT = 2 await lock.write_lock(ap.id, test, True, True) assert await lock.is_write_locked(ap_ap_ap.id, test) assert await lock.is_write_locked(ap_ap.id, test) await check_notification_content( lock, test, [ap.id, ap_ap.id, ap_ap_ap.id, ori.id, action.id]) await lock.read_lock(ap2.id, test) await lock.schedule_auto_release(test) await lock.cancel_auto_release(test) assert await lock.is_write_locked(ap.id, test) assert await lock.is_read_locked(ap2.id, test) read, write = await lock.get_owner_locks(test) assert len(read) == 1 assert ap2.id in read assert len(write) == 1 assert ap.id in write await lock.schedule_auto_release(test) await asyncio.sleep(lock.LOCK_TIMEOUT + 0.5) assert await lock.get_write_locks_count() == 0 assert not await lock.is_read_locked(ap2.id, test) await check_notification_content( lock, test, [ap.id, ap_ap.id, ap_ap_ap.id, ori.id, action.id], False)
async def test_ctx_read_lock() -> None: test = "test" user = "******" glob.LOCK = Lock({}) assert await glob.LOCK.get_locked_roots_count() == 0 glob.LOCK.scene = UpdateableCachedScene(cmn.Scene(test, description=test)) glob.LOCK.project = UpdateableCachedProject(cmn.Project(test, glob.LOCK.scene.id, description=test, has_logic=True)) async def patch() -> set[str]: return {glob.LOCK.project_or_exception().id, glob.LOCK.scene_or_exception().id} storage.get_project_ids = storage.get_scene_ids = patch # add some scene and project objects test_object = cmn.SceneObject(test, "TestType") glob.LOCK.scene.upsert_object(test_object) ap = glob.LOCK.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap", cmn.Position(0, 0, 0), test_object.id) ap_ap = glob.LOCK.project.upsert_action_point(cmn.BareActionPoint.uid(), "ap_ap", cmn.Position(0, 0, 1), ap.id) assert await glob.LOCK.get_locked_roots_count() == 0 await glob.LOCK.write_lock(ap_ap.id, user, True) assert await glob.LOCK.is_write_locked(test_object.id, user) assert await glob.LOCK.is_write_locked(ap.id, user) assert await glob.LOCK.is_write_locked(ap_ap.id, user) async with ctx_read_lock(test_object.id, user): pass assert await glob.LOCK.is_write_locked(test_object.id, user) assert await glob.LOCK.is_write_locked(ap.id, user) assert await glob.LOCK.is_write_locked(ap_ap.id, user)
async def test_root_getter_without_scene_and_project(lock: Lock) -> None: assert lock.scene assert lock.project scene = lock.scene project = lock.project lock.project = None lock.scene = None with pytest.raises(Arcor2Exception): lock.scene_or_exception() with pytest.raises(Arcor2Exception): lock.project_or_exception() async def patch() -> set[str]: return {project.id, scene.id} storage.get_project_ids = storage.get_scene_ids = patch assert await lock.get_root_id(project.id) == project.id assert await lock.get_root_id(scene.id) == scene.id