コード例 #1
0
ファイル: test_peer.py プロジェクト: JiuCaiBC/ToyChain
 def test_send(self):
     peer = Peer('api.huobipro.com')
     sendfunc = mock.Mock('sendfunc')
     mock_conn = mock.Mock(side_effect=coroutine(sendfunc))
     mock_conn.send = mock.Mock(side_effect=coroutine(sendfunc))
     peer.get_connection = mock.Mock(
         side_effect=coroutine(lambda: mock_conn))
     run_async(peer.send, 'msg')
     sendfunc.assert_called_with('msg')
コード例 #2
0
 def send(self, packet: dict):
     packet['from'] = self._host_id
     func = getattr(self, '_' + packet['type'] + '_sender')
     wrapper_func = func
     if not iscoroutine(func):
         wrapper_func = coroutine(func)
     asyncio.ensure_future(wrapper_func(packet))
コード例 #3
0
ファイル: bus.py プロジェクト: technomaniac/trellio
 def send(self, packet: dict):
     packet['from'] = self._host_id
     func = getattr(self, '_' + packet['type'] + '_sender')
     wrapper_func = func
     if not iscoroutine(func):
         wrapper_func = coroutine(func)
     asyncio.ensure_future(wrapper_func(packet))
コード例 #4
0
 def test_on_getaddr(self):
     sendfunc = mock.Mock()
     mock_send = mock.Mock(side_effect=coroutine(sendfunc))
     mock_peer = mock.Mock()
     mock_peer.send = mock_send
     run_async(handler.on_getaddr, {'peer': mock_peer})
     sendfunc.assert_called_once()
コード例 #5
0
    def _run_coroutine(self, coro, *args, **kwargs):
        print(locals())
        args = list(args)
        result = None
        if len(args) > 0 and type(args[-1]) is Task:
            task = args.pop()
            result = task.result()

        wrapper_coro = coro
        if not iscoroutinefunction(coro):
            wrapper_coro = coroutine(coro)

        ensure_future(wrapper_coro(result, *args, **kwargs))
コード例 #6
0
    def _run_signals(self, name, func, *args, **kwargs):
        pre_signal = getattr(self, 'pre_{}'.format(name), None)
        post_signal = getattr(self, 'post_{}'.format(name), None)

        if pre_signal:
            self._run_coroutine(pre_signal, *args, **kwargs)

        wrapped_func = func
        if not iscoroutinefunction(func):
            wrapped_func = coroutine(func)

        future = ensure_future(wrapped_func(*args, **kwargs))
        if post_signal:
            future.add_done_callback(
                partial(self._run_coroutine, post_signal, *args, **kwargs))
        return future
コード例 #7
0
ファイル: decorators.py プロジェクト: technomaniac/trellio
        async def f_retry(self, *args, **kwargs):
            if not iscoroutine(func):
                f = coroutine(func)
            else:
                f = func

            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return await f(self, *args, **kwargs)
                except exceptions:
                    if logger:
                        logger.info('Retrying %s after %s seconds', f.__name__, mdelay)
                    sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return await f(self, *args, **kwargs)
コード例 #8
0
 def setUp(self):
     self.mgr = ChainManager(Chain())
     self.mock_peer = mock.Mock()
     self.mock_sendfunc = mock.Mock()
     self.mock_peer.send = mock.Mock(side_effect=coroutine(self.mock_sendfunc))