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')
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))
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()
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))
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
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)
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))