async def test_repeat_kwargs(autojump_clock): results = [] async with Pipeline.create(Repeat( 1, default='a')) as pipeline, pipeline.tap() as aiter: start_time = trio.current_time() async for item in aiter: results.append((item, trio.current_time() - start_time)) if len(results) == 5: break assert results == [('a', 0), ('a', 1), ('a', 2), ('a', 3), ('a', 4)]
async def test_repeat_input(autojump_clock): results = [] async with Pipeline.create(produce_alphabet(1.5, max=3, delay=1), Repeat(1)) as pipeline, pipeline.tap() as aiter: start_time = trio.current_time() async for item in aiter: results.append((item, trio.current_time() - start_time)) if len(results) == 5: break assert results == [('a', 1), ('a', 2), ('b', 2.5), ('b', 3.5), ('c', 4)]
async def test_repeat_valid_args(): with pytest.raises(RuntimeError): async with Pipeline.create( Repeat(1)) as pipeline, pipeline.tap() as aiter: async for item in aiter: assert False, 'No items should be emitted due to invalid arguments provided.'
async def _connect(self, network, api_key, api_secret, dead_mans_switch): """Open a BitMEX websocket connection.""" try: if network == 'mainnet': url = 'wss://ws.bitmex.com/realtime' else: url = 'wss://ws.testnet.bitmex.com/realtime' log.debug('Generating authentication headers.') # To auth to the WS using an API key, we generate a signature of a nonce and # the WS API endpoint. headers = None if api_key and api_secret: nonce = generate_expires() headers = [('api-expires', str(nonce)), ('api-signature', generate_signature(api_secret, 'GET', '/realtime', nonce, '')), ('api-key', api_key)] send_channel, receive_channel = trio.open_memory_channel(math.inf) self._send_channel = send_channel if dead_mans_switch: sections = [ Merge( receive_channel, Repeat(15, default={ 'op': 'cancelAllAfter', 'args': 60000 })) ] else: sections = [receive_channel] self._websocket = Websocket(url, extra_headers=headers) parser = Parser() sections.append(self._websocket) sections.append(parser) sections.append(self.storage) async with Pipeline.create(*sections) as pipeline: self._pipeline = pipeline # Force the websocket to connect pipeline._enabled.set() await parser._connected.wait() log.info('BitMEXWebsocket open.') yield self log.debug( 'BitMEXWebsocket context exit. Cancelling running tasks.') pipeline.nursery.cancel_scope.cancel() except ConnectionClosed as cls: log.warning('BitMEXWebsocket closed (%d) %s.', cls.reason.code, cls.reason.name) raise except OSError as ose: log.error('Connection attempt failed: %s', type(ose).__name__) log.info('BitMEXWebsocket closed.')