示例#1
0
    def test_globalize_instance(self, mr_semaphore: SemaphoreFactory):
        from multirunnable.api.manage import Running_Semaphore
        assert Running_Semaphore is None, "It should be None before we do anything."

        mr_semaphore.feature_mode = FeatureMode.Parallel
        _semaphore = mr_semaphore.get_instance()
        mr_semaphore.globalize_instance(_semaphore)

        from multirunnable.api.manage import Running_Semaphore
        assert Running_Semaphore is _semaphore, "It should be the instance we instantiated."
示例#2
0
    def test_get_instance_with_coroutine_mode(self,
                                              mr_semaphore: SemaphoreFactory):
        try:
            _semaphore = mr_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_semaphore.feature_mode = FeatureMode.GreenThread
        _semaphore = mr_semaphore.get_instance()
        from gevent.lock import Semaphore
        assert _semaphore is not None and isinstance(
            _semaphore, Semaphore
        ) is True, "This type of Semaphore instance should be 'gevent.lock.Semaphore'."
示例#3
0
    def test_get_instance_with_parallel_mode(self,
                                             mr_semaphore: SemaphoreFactory):
        try:
            _semaphore = mr_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_semaphore.feature_mode = FeatureMode.Parallel
        _semaphore = mr_semaphore.get_instance()
        from multiprocessing.synchronize import Semaphore
        assert _semaphore is not None and isinstance(
            _semaphore, Semaphore
        ) is True, "This type of Semaphore instance should be 'multiprocessing.synchronize.Semaphore'."
示例#4
0
    def test_semaphore_decorator(self):
        _event_loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop=_event_loop)

        _done_timestamp = {}
        instantiate_semaphore(RunningMode.Asynchronous, event_loop=_event_loop)

        @AsyncRunWith.Semaphore
        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
        RunByStrategy.CoroutineWithAsynchronous(
            _function=_target_testing,
            _feature=SemaphoreFactory(value=_Semaphore_Value))
        TestAsyncFeaturesDecorator._chk_done_timestamp_by_semaphore(
            _done_timestamp)
示例#5
0
    def test_get_instance_with_asynchronous_mode(
            self, mr_semaphore: SemaphoreFactory):
        from asyncio.locks import Semaphore
        from asyncio import new_event_loop

        try:
            _semaphore = mr_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_semaphore.feature_mode = FeatureMode.Asynchronous
        _semaphore = mr_semaphore.get_instance(event_loop=new_event_loop())
        assert _semaphore is not None and isinstance(
            _semaphore, Semaphore
        ) is True, "This type of Semaphore instance should be 'asyncio.locks.Semaphore'."
示例#6
0
 def test__repr__(self, mr_semaphore: SemaphoreFactory):
     _testing_mode = FeatureMode.Parallel
     mr_semaphore.feature_mode = _testing_mode
     _semaphore_repr = repr(mr_semaphore)
     _chksum = re.search(
         r"<Semaphore\(value=[0-9]{1,4}\) object with FeatureMode\.[a-zA-Z]{4,32} mode at \w{10,30}>",
         _semaphore_repr)
     assert _chksum is not None, f"The '__repr__' format is incorrect. Please check its value. \n" \
                                 f"Its format should be like *<Semaphore(value=<Semaphore mount>) object with <Feature Mode> mode at <ID of instance>>*. \n" \
                                 f"But it got *{_semaphore_repr}*."
示例#7
0
    def test_feature_mode(self, mr_semaphore: SemaphoreFactory):
        _testing_mode = FeatureMode.GreenThread

        assert mr_semaphore.feature_mode is None, "The default value of FeatureMode of Semaphore instance should be None."
        try:
            mr_semaphore.feature_mode = _testing_mode
        except Exception as e:
            assert False, "It should set the FeatureMode into Semaphore instance without any issue."
        else:
            _feature_mode = mr_semaphore.feature_mode
            assert _feature_mode is _testing_mode, f"The mode we got from Semaphore instance should be the same as we set '{_testing_mode}'."
示例#8
0
def instantiate_semaphore(_mode, **kwargs):
    _semaphore = SemaphoreFactory(value=_Semaphore_Value)
    return _initial(_semaphore, _mode, **kwargs)
示例#9
0
 def test_feature_by_pykeyword_with_in_asynchronous_tasks(self):
     semaphore_opts = SemaphoreAsyncOperator()
     SemaphoreTestSpec._async_feature_testing_by_pykeyword_with(
         factory=SemaphoreFactory(value=_Semaphore_Value),
         _lock=semaphore_opts,
         running_function=RunByStrategy.CoroutineWithAsynchronous)
示例#10
0
def mr_semaphore() -> SemaphoreFactory:
    return SemaphoreFactory(value=_Semaphore_Value)
def instantiate_semaphore(_mode: Union[RunningMode, FeatureMode], **kwargs):
    _semaphore = SemaphoreFactory(value=_Semaphore_Value)
    return _initial(_semaphore, _mode, **kwargs)