Example #1
0
    def Concurrent(_functions: List):
        set_mode(mode=RunningMode.Concurrent)

        _strategy_adapter = ExecutorStrategyAdapter(mode=RunningMode.Concurrent, executors=_Worker_Size)
        _strategy = _strategy_adapter.get_simple()

        MapByStrategy._map_with_multiple_workers(_strategy, _functions)
Example #2
0
    def test_reconnect(self, mode: RunningMode,
                       connection_pool_strategy: MySQLDriverConnectionPool):
        set_mode(mode=mode)

        try:
            connection_pool_strategy.current_pool_name = Test_Pool_Name
            connection_pool_strategy.update_database_config(key="host",
                                                            value="1.1.1.1")
            connection_pool_strategy.update_database_config(key="port",
                                                            value=3306)
            try:
                _connection = connection_pool_strategy.reconnect(timeout=1)
            except ConnectionError as ce:
                assert "Cannot reconnect to database" in str(
                    ce), "It should raise an exception about retry timeout."
            except Exception as ce:
                assert "Can't connect to MySQL server on '1.1.1.1:3306'" in str(
                    ce
                ), "It should raise some exceptions which be annotated by database package."
            else:
                assert False, "It should raise something exceptions because the IP is invalid."

        except Exception as e:
            raise e

        finally:
            connection_pool_strategy.update_database_configs(
                config=Database_Config)

        _connection = connection_pool_strategy.reconnect()
        assert _connection is not None, "It should return a database connection instance."
        connection_pool_strategy.close_connection(conn=_connection)
Example #3
0
def file_saver(request) -> FileSaver:
    set_mode(mode=request.param)

    TestFileSaver._init_flag()

    fs = FileSaver(file=_ExampleTestingFile())
    return fs
Example #4
0
    def Parallel(_function):
        set_mode(mode=RunningMode.Parallel)

        _strategy_adapter = ExecutorStrategyAdapter(mode=RunningMode.Parallel, executors=_Worker_Size)
        _strategy = _strategy_adapter.get_simple()

        RunByStrategy._run_with_multiple_workers(_strategy, _function)
Example #5
0
    def CoroutineWithGreenThread(_functions: List):
        set_mode(mode=RunningMode.GreenThread)

        _strategy_adapter = ExecutorStrategyAdapter(mode=RunningMode.GreenThread, executors=_Worker_Size)
        _strategy = _strategy_adapter.get_simple()

        MapByStrategy._map_with_multiple_workers(_strategy, _functions)
Example #6
0
def opts_with_conn_pool_strategy(request) -> MySQLOperator:
    global _Pool_Strategy
    _Pool_Strategy = MySQLDriverConnectionPool(initial=False,
                                               **Database_Pool_Config)

    set_mode(mode=request.param)

    return MySQLOperator(conn_strategy=_Pool_Strategy,
                         db_config=Database_Pool_Config)
Example #7
0
 def test_instantiate_without_running_mode(self):
     set_mode(mode=None)
     try:
         TargetPoolDao()
     except ValueError as ve:
         assert "The RunningMode cannot be None object if it works persistence process as 'BaseConnectionPool'" in str(
             ve
         ), "It should raise an exception if it instantiates object without any RunningMode."
     else:
         assert False, "It should raise an exception if it instantiates object without any RunningMode."
Example #8
0
 def test_instantiate_without_running_mode(self):
     try:
         set_mode(mode=None)
         SavingMediator()
     except ValueError as ve:
         assert "The RunningMode in context cannot be None object if option *context* is None" in str(
             ve
         ), "It should raise an exception about RunningMode cannot be None if its option *context* is None."
     else:
         assert False, "It should raise an exception about RunningMode cannot be None if its option *context* is None."
Example #9
0
    def test_get_parent_worker(self, mode):
        if mode is RunningMode.Parallel and (PYTHON_MAJOR_VERSION,
                                             PYTHON_MINOR_VERSION) <= (3, 7):
            raise NotImplementedError
        else:
            _test_spec = _Function(mode=mode)

            set_mode(mode=mode)
            TestAdapterContext._run_test(
                mode=mode, testing=_test_spec.get_parent_worker_func)
Example #10
0
    def test_commit(self, mode: RunningMode,
                    connection_pool_strategy: MySQLDriverConnectionPool):
        set_mode(mode=mode)

        # # Insert data but doesn't commit it and close cursor and connection.
        _connection = connection_pool_strategy.get_one_connection(
            pool_name=Test_Pool_Name)
        _cursor = _connection.cursor()

        TestPersistenceDatabaseConnectionPool._insert_data(conn=_connection,
                                                           cursor=_cursor,
                                                           commit=False)
        TestPersistenceDatabaseConnectionPool._close_instance(
            _strategy=connection_pool_strategy,
            _conn=_connection,
            _cursor=_cursor)

        # # Select data to check. Insert data and commit it.
        _new_connection = connection_pool_strategy.reconnect()
        _new_cursor = _new_connection.cursor()

        _data = _select_data(_cursor=_new_cursor)
        assert _data == [], "It should get an empty dataset right now."
        TestPersistenceDatabaseConnectionPool._insert_data(
            conn=_new_connection,
            cursor=_new_cursor,
            commit=True,
            connection_strategy=connection_pool_strategy)
        TestPersistenceDatabaseConnectionPool._close_instance(
            _strategy=connection_pool_strategy,
            _conn=_new_connection,
            _cursor=_new_cursor)

        # # Select data to check. Delete testing data and commit it.
        _latest_connection = connection_pool_strategy.reconnect()
        _latest_cursor = _latest_connection.cursor()

        _data = _select_data(_cursor=_latest_cursor)
        assert _data != [] and len(
            _data) == 1, "It should get an empty dataset right now."
        TestPersistenceDatabaseConnectionPool._delete_data(
            conn=_latest_connection,
            cursor=_latest_cursor,
            connection_strategy=connection_pool_strategy)
        TestPersistenceDatabaseConnectionPool._close_instance(
            _strategy=connection_pool_strategy,
            _conn=_latest_connection,
            _cursor=_latest_cursor)
Example #11
0
    def CoroutineWithAsynchronous(_function, event_loop=None, _feature=None):
        set_mode(mode=RunningMode.Asynchronous)

        _strategy_adapter = ExecutorStrategyAdapter(mode=RunningMode.Asynchronous, executors=_Worker_Size)
        _strategy = _strategy_adapter.get_simple()

        async def __process():
            await _strategy.initialization(queue_tasks=None, features=_feature, event_loop=event_loop)
            _ps = [_strategy.generate_worker(_function) for _ in range(_Worker_Size)]
            await _strategy.activate_workers(_ps)

        if (PYTHON_MAJOR_VERSION, PYTHON_MINOR_VERSION) > (3, 6):
            asyncio.run(__process())
        else:
            _event_loop = asyncio.get_event_loop()
            _event_loop.run_until_complete(__process())
Example #12
0
    def test_close_connection(
            self, mode: RunningMode,
            connection_pool_strategy: MySQLDriverConnectionPool):
        set_mode(mode=mode)

        _conn = connection_pool_strategy.get_one_connection(
            pool_name=Test_Pool_Name)
        _cursor = _conn.cursor()
        assert _cursor is not None, "It should get the cursor instance without any issue."

        _cursor.close()
        connection_pool_strategy.close_connection(conn=_conn)

        try:
            _cursor = _conn.cursor()
        except AttributeError as ae:
            assert "'NoneType' object has no attribute 'cursor'" in str(ae), ""
Example #13
0
    def test_get_one_connection(
            self, mode: RunningMode,
            connection_pool_strategy: MySQLDriverConnectionPool):
        set_mode(mode=mode)

        _pools = database_connection_pools()

        try:
            _conn = connection_pool_strategy.get_one_connection()
        except ValueError as ce:
            assert "Cannot get the one connection instance from connection pool because it doesn't exist the connection pool with the name" in str(ce), \
                "It should raise an exception about cannot find the connection pool with the target pool name."
        else:
            assert False, "It should find nothing by the pool name ''."

        _conn = connection_pool_strategy.get_one_connection(
            pool_name=Test_Pool_Name)
        assert _conn is not None, f"It should get a connection instance from the pool with pool name '{Test_Pool_Name}'."
        # # Release connection instance back to pool
        connection_pool_strategy.close_connection(conn=_conn)
Example #14
0
    def test_get_activate_count(self, mode):
        _test_spec = _Function(mode=mode)

        set_mode(mode=mode)
        TestAdapterContext._run_test(mode=mode,
                                     testing=_test_spec.activate_count_func)
Example #15
0
def saving_mediator(request) -> SavingMediator:
    set_mode(mode=request.param)
    return SavingMediator()
Example #16
0
def adapter_pool() -> AdapterPool:
    set_mode(RunningMode.Concurrent)
    return AdapterPool(strategy=ThreadPoolStrategy(
        pool_size=_Worker_Pool_Size))
Example #17
0
def db_opt_pool(request) -> TargetPoolDao:
    set_mode(mode=request.param)
    return TargetPoolDao()
Example #18
0
    def CoroutineWithAsynchronous(_functions: List, _feature=None):
        set_mode(mode=RunningMode.Asynchronous)

        _strategy_adapter = ExecutorStrategyAdapter(mode=RunningMode.Asynchronous, executors=_Worker_Size)
        _strategy = _strategy_adapter.get_simple()
        _strategy.map_with_function(functions=_functions, features=_feature)
Example #19
0
def instantiate_adapter_executor() -> AdapterExecutor:
    set_mode(mode=RunningMode.Concurrent)
    _executor = AdapterExecutor(strategy=ThreadStrategy(executors=_Worker_Size))
    initial_lock()
    return _executor
Example #20
0
    def test_get_children_workers(self, mode):
        _test_spec = _Function(mode=mode)

        set_mode(mode=mode)
        TestAdapterContext._run_test(mode=mode,
                                     testing=_test_spec.children_workers_func)
Example #21
0
    def test_get_current_worker(self, mode):
        _test_spec = _Function(mode=mode)

        set_mode(mode=mode)
        TestAdapterContext._run_test(
            mode=mode, testing=_test_spec.get_current_worker_func)
Example #22
0
_Worker_Size = 7
_Semaphore_Value = 2


def target_function_with_bsmp() -> str:
    return retry_success_with_bsmp()


@retry.function
@RunWith.Bounded_Semaphore
def retry_success_with_bsmp() -> str:
    print("Running function ...")
    _worker_name = context.get_current_worker_name()
    return f"{_worker_name} Running Result"


if __name__ == '__main__':

    set_mode(RunningMode.Parallel)

    _bsmp_factory = BoundedSemaphoreFactory(value=_Semaphore_Value)
    _sexor = SimpleExecutor(executors=_Worker_Size)
    _sexor.run(function=target_function_with_bsmp, features=_bsmp_factory)
    _result = _sexor.result()

    for _r in _result:
        print("++++++++++++++++++++++++++++")
        print(f"worker_name: {_r.worker_name}")
        print(f"data: {_r.data}")
        print(f"exception: {_r.exception}")
Example #23
0
def instantiate_executor(request) -> SimpleExecutor:
    set_mode(mode=request.param)
    _executor = SimpleExecutor(mode=request.param, executors=_Worker_Size)
    initial_lock()
    return _executor