def test_globalize_instance(self, mr_bounded_semaphore: BoundedSemaphoreFactory): from multirunnable.api.manage import Running_Bounded_Semaphore assert Running_Bounded_Semaphore is None, "It should be None before we do anything." mr_bounded_semaphore.feature_mode = FeatureMode.Parallel _bounded_semaphore = mr_bounded_semaphore.get_instance() mr_bounded_semaphore.globalize_instance(_bounded_semaphore) from multirunnable.api.manage import Running_Bounded_Semaphore assert Running_Bounded_Semaphore is _bounded_semaphore, "It should be the instance we instantiated."
def test_get_instance_with_coroutine_mode( self, mr_bounded_semaphore: BoundedSemaphoreFactory): try: _bounded_semaphore = mr_bounded_semaphore.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_bounded_semaphore.feature_mode = FeatureMode.GreenThread _bounded_semaphore = mr_bounded_semaphore.get_instance() from gevent.lock import BoundedSemaphore assert _bounded_semaphore is not None and isinstance( _bounded_semaphore, BoundedSemaphore ) is True, "This type of Semaphore instance should be 'gevent.lock.BoundedSemaphore'."
def test_get_instance_with_parallel_mode( self, mr_bounded_semaphore: BoundedSemaphoreFactory): try: _bounded_semaphore = mr_bounded_semaphore.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_bounded_semaphore.feature_mode = FeatureMode.Parallel _bounded_semaphore = mr_bounded_semaphore.get_instance() from multiprocessing.synchronize import BoundedSemaphore assert _bounded_semaphore is not None and isinstance( _bounded_semaphore, BoundedSemaphore ) is True, "This type of BoundedSemaphore instance should be 'multiprocessing.synchronize.BoundedSemaphore'."
def test_bounded_semaphore_decorator(self): _event_loop = asyncio.new_event_loop() asyncio.set_event_loop(loop=_event_loop) _done_timestamp = {} instantiate_bounded_semaphore(FeatureMode.Asynchronous, event_loop=_event_loop) @AsyncRunWith.Bounded_Semaphore async def _target_testing(): # Save a time stamp into list try: 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 except Exception as e: assert False, f"Occur something unexpected issue. Please check it. Exception: {e}" else: assert True, "Testing code successfully." # # # # Run multiple workers and save something info at the right time run_async(_function=_target_testing, _feature=BoundedSemaphoreFactory(value=_Semaphore_Value)) TestAsyncFeaturesDecorator._chk_done_timestamp_by_semaphore(_done_timestamp)
def test_get_instance_with_asynchronous_mode( self, mr_bounded_semaphore: BoundedSemaphoreFactory): from asyncio.locks import BoundedSemaphore from asyncio import new_event_loop try: _bounded_semaphore = mr_bounded_semaphore.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_bounded_semaphore.feature_mode = FeatureMode.Asynchronous _bounded_semaphore = mr_bounded_semaphore.get_instance( event_loop=new_event_loop()) assert _bounded_semaphore is not None and isinstance( _bounded_semaphore, BoundedSemaphore ) is True, "This type of BoundedSemaphore instance should be 'asyncio.locks.BoundedSemaphore'."
def test__repr__(self, mr_bounded_semaphore: BoundedSemaphoreFactory): _testing_mode = FeatureMode.Parallel mr_bounded_semaphore.feature_mode = _testing_mode _bounded_semaphore_repr = repr(mr_bounded_semaphore) _chksum = re.search( r"<BoundedSemaphore\(value=[0-9]{1,4}\) object with FeatureMode\.[a-zA-Z]{4,32} mode at \w{10,30}>", _bounded_semaphore_repr) assert _chksum is not None, f"The '__repr__' format is incorrect. Please check its value. \n" \ f"Its format should be like *<BoundedSemaphore(value=<Semaphore mount>) object with <Feature Mode> mode at <ID of instance>>*. \n" \ f"But it got *{_bounded_semaphore_repr}*."
def test_feature_mode(self, mr_bounded_semaphore: BoundedSemaphoreFactory): _testing_mode = FeatureMode.Asynchronous assert mr_bounded_semaphore.feature_mode is None, "The default value of FeatureMode of BoundedSemaphore instance should be None." try: mr_bounded_semaphore.feature_mode = _testing_mode except Exception as e: assert False, "It should set the FeatureMode into BoundedSemaphore instance without any issue." else: _feature_mode = mr_bounded_semaphore.feature_mode assert _feature_mode is _testing_mode, f"The mode we got from BoundedSemaphore instance should be the same as we set '{_testing_mode}'."
def instantiate_bounded_semaphore(_mode, **kwargs): _bounded_semaphore = BoundedSemaphoreFactory(value=_Semaphore_Value) return _initial(_bounded_semaphore, _mode, **kwargs)
def test_feature_by_pykeyword_with_in_asynchronous_tasks(self): bounded_semaphore_opts = BoundedSemaphoreAsyncOperator() BoundedSemaphoreTestSpec._async_feature_testing_by_pykeyword_with( _lock=bounded_semaphore_opts, running_function=RunByStrategy.CoroutineWithAsynchronous, factory=BoundedSemaphoreFactory(value=_Semaphore_Value))
def mr_bounded_semaphore() -> BoundedSemaphoreFactory: return BoundedSemaphoreFactory(value=_Semaphore_Value)
def instantiate_bounded_semaphore(_mode: Union[RunningMode, FeatureMode], **kwargs): _bounded_semaphore = BoundedSemaphoreFactory(value=_Semaphore_Value) return _initial(_bounded_semaphore, _mode, **kwargs)