def test_wait_stop_streaming_errors(self): response = EventSourceResponse() with self.assertRaisesRegex(RuntimeError, 'Response is not started'): response.wait() with self.assertRaisesRegex(RuntimeError, 'Response is not started'): response.stop_streaming()
def hello(request): resp = EventSourceResponse() resp.start(request) for i in range(0, 100): print('foo') yield from asyncio.sleep(1, loop=loop) resp.send('foo {}'.format(i)) resp.stop_streaming() return resp
async def func(request): resp = EventSourceResponse() await resp.prepare(request) with pytest.raises(TypeError): resp.send('foo', retry='one') resp.send('foo', retry=1) resp.stop_streaming() await resp.wait() return resp
async def test_wait_stop_streaming_errors(): response = EventSourceResponse() with pytest.raises(RuntimeError) as ctx: await response.wait() assert str(ctx.value) == 'Response is not started' with pytest.raises(RuntimeError) as ctx: response.stop_streaming() assert str(ctx.value) == 'Response is not started'
async def wrapped(request): response = EventSourceResponse(headers={'X-SSE': 'aiohttp_sse'}) await response.prepare(request) while True: temp = await co_func() data = {'value': temp, 'time': datetime.now().isoformat()} await response.send(data=json.dumps(data), event=event_name) await asyncio.sleep(period) response.stop_streaming() await response.wait() return response
async def func(request): if with_sse_response: resp = await sse_response(request, headers={'X-SSE': 'aiohttp_sse'}) else: resp = EventSourceResponse(headers={'X-SSE': 'aiohttp_sse'}) await resp.prepare(request) resp.send('foo') resp.send('foo', event='bar') resp.send('foo', event='bar', id='xyz') resp.send('foo', event='bar', id='xyz', retry=1) resp.stop_streaming() await resp.wait() return resp
async def stream(self, request): response = EventSourceResponse(headers={'X-SSE': 'aiohttp_sse'}) await response.prepare(request) while True: # Wait for a new event to send event = await self._queue.get() event_name = event[0] data = event[1] await response.send(data=json.dumps(data), event=event_name, id=datetime.now().isoformat()) response.stop_streaming() await response.wait() return response
def func(request): if with_sse_response: resp = yield from sse_response(request, headers={'X-SSE': 'aiohttp_sse'}) else: resp = EventSourceResponse(headers={'X-SSE': 'aiohttp_sse'}) if with_deprecated_start: resp.start(request) else: yield from resp.prepare(request) resp.send('foo') resp.send('foo', event='bar') resp.send('foo', event='bar', id='xyz') resp.send('foo', event='bar', id='xyz', retry=1) resp.stop_streaming() yield from resp.wait() return resp