Beispiel #1
0
 def test_run_sync(self):
     """Test helpers.async.run_sync by running an async function from this synchronous test"""
     x, y = helpers.run_sync(self._tst_async, 5, 10)
     d, e = helpers.run_sync(self._tst_async, 1, 2)
     self.assertEqual(x, 10)
     self.assertEqual(y, 30)
     self.assertEqual(d, 2)
     self.assertEqual(e, 6)
Beispiel #2
0
    def test_async_cache(self):
        """Test :func:`.r_cache_async` with an async function"""
        @r_cache_async('privex_tests:some_func', cache_time=2)
        async def some_func(some: int, args: int = 2):
            return some + args

        self.assertEqual(helpers.run_sync(some_func, 5, 10), 15)
        self.assertEqual(helpers.run_sync(some_func, 10, 20), 15)
        sleep(2)
        self.assertEqual(helpers.run_sync(some_func, 10, 30), 40)
 def test_get_current_tag(self):
     run_sync(self._commit_async)
     self.git.tag('1.0.0')
     self.assertEqual(self.git.get_current_tag(), '1.0.0')
     # Checkout testing and make a new commit so we can confirm checking tags between branches works
     self.git.checkout('testing', new=True)
     self.git.add('testfile2')
     self.git.commit('added testfile2')
     self.git.tag('1.0.1')
     self.assertEqual(self.git.get_current_tag(), '1.0.1')
     self.assertEqual(self.git.get_current_tag('master'), '1.0.0')
Beispiel #4
0
 def __getitem__(self, item):
     if type(item) is int:
         if item == 0:
             # async with await self.fetch(query_mode=QueryMode.ROW_DICT) as r:
             return run_sync(self.fetch, query_mode=QueryMode.ROW_DICT)
         # async with await self.all() as rows:
         return list(run_sync(async_iterate_list, self.all()))[item]
     if type(item) is str:
         # async with await self.fetch(query_mode=QueryMode.ROW_DICT) as r:
         #     return r[item]
         return run_sync(self.fetch, query_mode=QueryMode.ROW_DICT)[item]
Beispiel #5
0
def import_block(block: int) -> dict:
    with LockMgr(f'eoshist_impblock:{block}'):
        log.info('Importing block %d via _import_block...', block)
        with transaction.atomic():
            db_block, raw_block = run_sync(_import_block, block)

            raw_block: eos.EOSBlock
            db_block: EOSBlock

            total_txs = 0
            for tx in raw_block.transactions:  # type: eos.EOSTransaction
                if total_txs % 10 == 0 or total_txs == 0 or total_txs == len(
                        raw_block.transactions) - 1:
                    log.debug('Importing transaction %d out of %d',
                              total_txs + 1, len(raw_block.transactions))
                try:
                    # Import the current TX into the DB
                    run_sync(loader.import_transaction, block=db_block, tx=tx)

                    if total_txs % 10 == 0 or total_txs == 0 or total_txs == len(
                            raw_block.transactions) - 1:
                        log.debug(
                            'Importing actions contained in transaction %d',
                            total_txs + 1)
                    # Import the all actions contained in this TX
                    run_sync(loader.import_actions, tx)
                except InvalidTransaction as e:
                    log.warning(
                        "Skipping transaction %d out of %d on block %d due to InvalidTransaction: %s",
                        total_txs + 1, len(raw_block.transactions), block,
                        str(e))
                except (IntegrityError, errors.UniqueViolation) as e:
                    if 'duplicate key value' in str(e):
                        log.warning(
                            'WARNING: (Block Import: %d) Transaction ID "%s" already exists... '
                            'Exception: %s %s', block, tx.id, type(e), str(e))
                    else:
                        log.error(
                            'An unknown IntegrityError/UniqueViolation occurred while importing TX %s - '
                            'Exception: %s %s', tx.id, type(e), str(e))
                except (Exception, BaseException):
                    log.error(
                        'An unknown exception occurred while importing TX %s - Skipping TX. - '
                        'Exception: %s %s', tx.id, type(e), str(e))
                total_txs += 1

        return dict(block_num=db_block.number,
                    timestamp=str(db_block.timestamp),
                    txs_imported=total_txs)
Beispiel #6
0
    def test_async_cache_key(self):
        """Test :func:`.r_cache_async` with an async function and async cache key"""

        async def mk_key(some: int, *args, **kwargs):
            return f'privex_tests:some_func:{some}'

        @r_cache_async(mk_key, cache_time=2)
        async def some_func(some: int, args: int = 2):
            return some + args
    
        # We cache based on the first argument. If we pass 5,10 and 5,20 then 5,20 should get the cached 5,10 result.
        self.assertEqual(helpers.run_sync(some_func, 5, 10), 15)
        self.assertEqual(helpers.run_sync(some_func, 5, 20), 15)
        # But 10,20 should get an up to date result.
        self.assertEqual(helpers.run_sync(some_func, 10, 20), 30)
        sleep(2)
        # Confirm the cache key for some=5 expired by calling with 5,30
        self.assertEqual(helpers.run_sync(some_func, 5, 30), 35)
    def test_commit_async(self):
        # async def _commit_async(g):
        #     await g.init()
        #     await g.add("testfile")
        #     await g.commit("added testfile")
        #     _git_log = await g.log()
        #     return _git_log.split('\n')

        git_log = run_sync(self._commit_async)
        self.assertIn("added testfile", git_log[0])
Beispiel #8
0
def import_block(block: int) -> dict:
    with LockMgr(f'eoshist_impblock:{block}'):
        log.debug('Importing block %d via _import_block...', block)
        with transaction.atomic():
            _b = run_sync(_import_block, block)
            if type(_b) not in [tuple, list] or len(_b) == 1:
                return dict(block_num=_b.number,
                            timestamp=str(_b.timestamp),
                            txs_imported=0)
            db_block, raw_block = _b

            raw_block: eos.EOSBlock
            db_block: EOSBlock

            total_txs = len(raw_block.transactions)
            run_sync(import_block_transactions, raw_block, db_block)

        return dict(block_num=db_block.number,
                    timestamp=str(db_block.timestamp),
                    txs_imported=total_txs)
Beispiel #9
0
 def test_awaitable(self):
     """Test that :func:`.awaitable` allows us to run an async function synchronously, without breaking async await"""
     async def example_func_async(a, b): return a + b
     
     @helpers.awaitable
     def example_func(a, b): return example_func_async(a, b)
     
     async def call_example_async():
         return await example_func("hello", " world")
     
     self.assertEqual(helpers.run_sync(call_example_async), "hello world")
     self.assertEqual(example_func("hello", " world"), "hello world")
 def test_get_current_branch(self):
     run_sync(self._commit_async)
     self.assertEqual(self.git.get_current_branch(), 'master')
     run_sync(self._checkout_async, 'testing', new=True)
     self.assertEqual(self.git.get_current_branch(), 'testing')
     run_sync(self._checkout_async, 'master')
     self.assertEqual(self.git.get_current_branch(), 'master')
    def test_add_async(self):
        async def _add_async(g):
            await g.init()
            await g.add("testfile")
            return await g.status()

        status = run_sync(_add_async, self.git)
        found = False
        for s in status.split('\n'):
            s_status, s_file = s[0:2], s[2:].strip()
            # log.info("s_status: '%s'   s_file: '%s'", s_status, s_file)
            if s_status == "A " and s_file == "testfile":
                found = True
        self.assertTrue(found)
Beispiel #12
0
    def test_async_aobject(self):
        """Test :class:`.aobject` sub-classes with async constructors can be constructed and used correctly"""
        class ExampleAsyncObject(helpers.aobject):
            async def _init(self):
                self.example = await self.get_example()
            
            async def get_example(self):
                return "hello world"

            __init__ = _init
        
        async def setup_async_object():
            # noinspection PyUnresolvedReferences
            o = await ExampleAsyncObject()
            return o.example
        
        self.assertEqual(helpers.run_sync(setup_async_object), "hello world")
 def test_get_current_commit(self):
     git_log = run_sync(self._commit_async)
     last_commit = git_log[0].split()[0]
     current_commit = self.git.get_current_commit()
     self.assertIn(last_commit, current_commit)
 def test_checkout_async(self):
     run_sync(self._commit_async)
     b = run_sync(self._checkout_async, 'test', new=True)
     self.assertIn("Switched to a new branch 'test'", b)
     b = run_sync(self._checkout_async, 'master')
     self.assertIn("Switched to branch 'master'", b)
    def test_init_async(self):
        async def _test_init():
            return await self.git.init()

        self.assertIn("Initialized empty Git repository", run_sync(_test_init))