def test_globalize_instance(self, mr_lock: LockFactory): from multirunnable.api.manage import Running_Lock assert Running_Lock is None, "It should be None before we do anything." mr_lock.feature_mode = FeatureMode.Parallel _lock = mr_lock.get_instance() mr_lock.globalize_instance(_lock) from multirunnable.api.manage import Running_Lock assert Running_Lock is _lock, "It should be the instance we instantiated."
def test_get_instance_with_concurrent_mode(self, mr_lock: LockFactory): try: _lock = mr_lock.get_instance() except ValueError as ve: assert "FeatureMode is None. Please configure it as one of 'multirunnable.mode.FeatureMode'." in str( ve), "It should set the FeatureMode first." mr_lock.feature_mode = FeatureMode.Concurrent _lock = mr_lock.get_instance() from threading import Lock assert _lock is not None and isinstance(_lock, type(Lock( ))) is True, "This type of Lock instance should be 'threading.Lock'."
def test_get_instance_with_parallel_mode(self, mr_lock: LockFactory): try: _lock = mr_lock.get_instance() except ValueError as ve: assert "FeatureMode is None. Please configure it as one of 'multirunnable.mode.FeatureMode'." in str( ve), "It should set the FeatureMode first." mr_lock.feature_mode = FeatureMode.Parallel _lock = mr_lock.get_instance() from multiprocessing.synchronize import Lock assert _lock is not None and isinstance( _lock, Lock ) is True, "This type of Lock instance should be 'multiprocessing.synchronize.Lock'."
def test_get_instance_with_asynchronous_mode(self, mr_lock: LockFactory): from asyncio.locks import Lock from asyncio import new_event_loop try: _lock = mr_lock.get_instance() except ValueError as ve: assert "FeatureMode is None. Please configure it as one of 'multirunnable.mode.FeatureMode'." in str( ve), "It should set the FeatureMode first." mr_lock.feature_mode = FeatureMode.Asynchronous _lock = mr_lock.get_instance(event_loop=new_event_loop()) assert _lock is not None and isinstance( _lock, Lock ) is True, "This type of Lock instance should be 'asyncio.locks.Lock'."
def test_feature_by_pykeyword_with_in_asynchronous_tasks(self): lock_opts = LockAsyncOperator() LockTestSpec._async_feature_testing_by_pykeyword_with( mode=FeatureMode.Asynchronous, _lock=lock_opts, running_function=RunByStrategy.CoroutineWithAsynchronous, factory=LockFactory())
def test__repr__(self, mr_lock: LockFactory): _testing_mode = FeatureMode.Parallel mr_lock.feature_mode = _testing_mode _lock_repr = repr(mr_lock) _chksum = re.search( r"<Lock\(\) object with FeatureMode\.[a-zA-Z]{4,32} mode at \w{10,30}>", _lock_repr) assert _chksum is not None, f"The '__repr__' format is incorrect. Please check its value. \n" \ f"Its format should be like *<Lock() object with <Feature Mode> mode at <ID of instance>>*. \n" \ f"But it got *{_lock_repr}*."
def test_feature_mode(self, mr_lock: LockFactory): _testing_mode = FeatureMode.Parallel assert mr_lock.feature_mode is None, "The default value of FeatureMode of Lock instance should be None." try: mr_lock.feature_mode = _testing_mode except Exception as e: assert False, "It should set the FeatureMode into Lock instance without any issue." else: _feature_mode = mr_lock.feature_mode assert _feature_mode is _testing_mode, f"The mode we got from Lock instance should be the same as we set '{_testing_mode}'."
def test_lock_decorator(self): _event_loop = asyncio.new_event_loop() asyncio.set_event_loop(loop=_event_loop) _done_timestamp = {} instantiate_lock(FeatureMode.Asynchronous, event_loop=_event_loop) @AsyncRunWith.Lock async def _target_testing(): # Save a timestamp into list await asyncio.sleep(_Sleep_Time) if (PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION) > (3, 6): _current_task = asyncio.current_task() else: _current_task = asyncio.Task.current_task() _current_task_id = id(_current_task) _time = float(time.time()) _done_timestamp[_current_task_id] = _time # # # # Run multiple workers and save something info at the right time run_async(_function=_target_testing, _feature=LockFactory()) TestAsyncFeaturesDecorator._chk_done_timestamp_by_lock(_done_timestamp)
def instantiate_lock(_mode, **kwargs): _lock = LockFactory() return _initial(_lock, _mode, **kwargs)
def mr_lock() -> LockFactory: return LockFactory()
def instantiate_lock(_mode: Union[RunningMode, FeatureMode], **kwargs): _lock = LockFactory() return _initial(_lock, _mode, **kwargs)