async def emitter(): yield SSEvent(data=b'ketchup') yield SSEvent(data=b'mustard', event='condiment') yield SSEvent(data=b'mayo', event='condiment', event_id='1234') yield SSEvent(data=b'onions', event='topping', event_id='5678', retry=100) yield SSEvent(text='guacamole \u1F951', retry=100, comment='Serve with chips.') yield SSEvent(json={'condiment': 'salsa'}, retry=100)
async def events(self): try: yield SSEvent(text='SSE CONNECTED') while True: try: event = await asyncio.wait_for(self._queue.get(), timeout=self.POLL_TIMEOUT) yield event except asyncio.TimeoutError: # NOTE(vytas): Keep the connection alive. yield None finally: # TODO(vytas): Is there a more elegant way to detect a disconnect? self._done = True
async def emitter(): for event in [ SSEvent(data=b'ketchup'), SSEvent(data=b'mustard', event='condiment'), SSEvent(data=b'mayo', event='condiment', event_id='1234'), SSEvent(data=b'onions', event='topping', event_id='5678', retry=100), SSEvent(text='guacamole \u1F951', retry=100, comment='Serve with chips.'), SSEvent(json={'condiment': 'salsa'}, retry=100), ]: yield event await asyncio.sleep(0.001)
async def enqueue(self, message): event = SSEvent(text=message, event_id=str(uuid.uuid4())) await self._queue.put(event)
async def emitter(): yield SSEvent()
def test_invalid_event_values(): with pytest.raises(TypeError): SSEvent(data='notbytes') with pytest.raises(TypeError): SSEvent(data=12345) with pytest.raises(TypeError): SSEvent(data=0) with pytest.raises(TypeError): SSEvent(text=b'notbytes') with pytest.raises(TypeError): SSEvent(text=23455) with pytest.raises(TypeError): SSEvent(text=0) with pytest.raises(TypeError): SSEvent(json=set()).serialize() with pytest.raises(TypeError): SSEvent(event=b'name') with pytest.raises(TypeError): SSEvent(event=1234) with pytest.raises(TypeError): SSEvent(event=0) with pytest.raises(TypeError): SSEvent(event_id=b'idbytes') with pytest.raises(TypeError): SSEvent(event_id=52085) with pytest.raises(TypeError): SSEvent(event_id=0) with pytest.raises(TypeError): SSEvent(retry='5808.25') with pytest.raises(TypeError): SSEvent(retry='') with pytest.raises(TypeError): SSEvent(retry=5808.25) with pytest.raises(TypeError): SSEvent(comment=b'somebytes') with pytest.raises(TypeError): SSEvent(comment=1234) with pytest.raises(TypeError): SSEvent(comment=0)
async def emitter(): yield SSEvent(json={'foo': 'bar'}) yield SSEvent(json={'bar': 'baz'})
async def emitter(): while True: yield SSEvent(data=b'whassup') await asyncio.sleep(0.01)