Example #1
0
    def test_set(self, mode):
        set_mode(mode=mode)
        _mode = get_current_mode()
        assert _mode is mode, "The mode we got should be the same as we set."

        try:
            _mode = get_current_mode()
        except Exception:
            assert False, f"It shouldn't occur any error when we try to get mode. Error: {traceback.format_exc()}"
        else:
            assert _mode is mode, "The mode we got should be the same as we set."
Example #2
0
 def test_get_current_mode_with_force(self):
     try:
         _mode = get_current_mode(force=True)
     except ValueError as ve:
         assert str(
             ve
         ) == "Current 'RUNNING_MODE' is None.", "The error message should tell developers the 'Running_Mode' is None currently."
     except Exception:
         assert False, f"It occur something unexpected error. Please check it. Error: {traceback.format_exc()}"
     else:
         assert False, "It should raise 'ValueError' if option *force* is True."
Example #3
0
def sleep(seconds: float, mode: RunningMode = None, **kwargs) -> None:
    import asyncio

    if mode is None:
        from multirunnable._config import get_current_mode
        mode = get_current_mode()

    if mode is RunningMode.Asynchronous:
        raise TypeError(
            "It doesn't accept 'Asynchronous' running mode in this function.")

    def __get_instn():
        __cls = _ImportMultiRunnable.get_class(
            pkg_path=".coroutine.utils",
            cls_name=f"{mode.value.get('class_key')}Waiter")
        __instance = __cls()
        return __instance

    def __sleep(instance, param: Dict) -> None:
        instance.sleep(**param)

    async def __await_sleep(instance, param: Dict) -> None:
        await instance.sleep(**param)

    if mode is RunningMode.GreenThread:
        __seconds = int(seconds)
        __ref = kwargs.get("ref", True)
        __param = {"seconds": __seconds, "ref": __ref}

        __waiter_instance = __get_instn()
        __sleep(instance=__waiter_instance, param=__param)
    elif mode is RunningMode.Asynchronous:
        __seconds = seconds
        __result = kwargs.get("result", None)
        __loop = kwargs.get("loop", None)
        __param = {"delay": __seconds, "result": __result, "loop": __loop}

        __waiter_instance = __get_instn()
        # # # Not finish yet
        # __current_running_loop = asyncio.get_running_loop()
        # if __current_running_loop is None:
        #     raise RuntimeError("It should be used in coroutine function.")
        # asyncio.run(__await_sleep(instance=__waiter_instance, param=__param))
    else:
        import time
        time.sleep(seconds)
Example #4
0
    def test_get_current_mode(self):
        _mode = get_current_mode()
        assert _mode is None, "The value which be get via method 'get_current_mode' should be None because we doesn't set the RunningMode currently."

        _mode = get_current_mode(force=False)
        assert _mode is None, "The value which be get via method 'get_current_mode' should be None because we doesn't set the RunningMode currently."