| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
from datetime import datetime, timedelta |
| 4 |
import pytest |
| 5 |
|
| 6 |
from aiohttp_sse_client2 import client as sse_client |
| 7 |
|
| 8 |
from .const import WPT_SERVER |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.asyncio |
| 12 |
async def test_eventsource_reconnect(): |
| 13 |
"""Test EventSource: reconnection. |
| 14 |
|
| 15 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 16 |
eventsource/eventsource-reconnect.htm |
| 17 |
""" |
| 18 |
source = sse_client.EventSource( |
| 19 |
WPT_SERVER + 'resources/status-reconnect.py?status=200') |
| 20 |
await source.connect() |
| 21 |
async for e in source: |
| 22 |
assert e.data == 'data' |
| 23 |
break |
| 24 |
await source.close() |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.asyncio |
| 28 |
async def test_eventsource_reconnect_event(): |
| 29 |
"""Test EventSource: reconnection event. |
| 30 |
|
| 31 |
..seealso: https://github.com/web-platform-tests/wpt/blob/master/ |
| 32 |
eventsource/eventsource-reconnect.htm |
| 33 |
""" |
| 34 |
opened = False |
| 35 |
reconnected = False |
| 36 |
|
| 37 |
def on_error(): |
| 38 |
nonlocal reconnected |
| 39 |
assert source.ready_state == sse_client.READY_STATE_CONNECTING |
| 40 |
assert opened is True |
| 41 |
reconnected = True |
| 42 |
|
| 43 |
async with sse_client.EventSource( |
| 44 |
WPT_SERVER + 'resources/status-reconnect.py?status=200&ok_first&id=2', |
| 45 |
reconnection_time=timedelta(milliseconds=2), |
| 46 |
on_error=on_error |
| 47 |
) as source: |
| 48 |
async for e in source: |
| 49 |
if not opened: |
| 50 |
opened = True |
| 51 |
assert reconnected is False |
| 52 |
assert e.data == "ok" |
| 53 |
else: |
| 54 |
assert reconnected is True |
| 55 |
assert e.data == "data" |
| 56 |
break |