async def test_drain_lost(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.connection_lost(None) with pytest.raises(ConnectionResetError): await pr._drain_helper()
async def test_drain_not_paused(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert pr._drain_waiter is None await pr._drain_helper() assert pr._drain_waiter is None
async def test_connection_made() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() assert pr.transport is None pr.connection_made(tr) assert pr.transport is not None
async def test_connection_made() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() assert pr.transport is None pr.connection_made(tr) assert pr.transport is not None
async def test_drain_not_paused() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert pr._drain_waiter is None await pr._drain_helper() assert pr._drain_waiter is None
def test_connection_lost_not_paused(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert not pr._connection_lost pr.connection_lost(None) assert pr.transport is None assert pr._connection_lost
async def test_drain_not_paused() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert pr._drain_waiter is None await pr._drain_helper() assert pr._drain_waiter is None
async def test_connection_lost_not_paused() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert not pr._connection_lost pr.connection_lost(None) assert pr.transport is None assert pr._connection_lost
async def test_connection_lost_not_paused() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert not pr._connection_lost pr.connection_lost(None) assert pr.transport is None assert pr._connection_lost
def test_connection_lost_paused_without_waiter(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) assert not pr._connection_lost pr.pause_writing() pr.connection_lost(None) assert pr.transport is None assert pr._connection_lost
async def test_lost_drain_waited_ok(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() t = loop.create_task(pr._drain_helper()) await asyncio.sleep(0) assert pr._drain_waiter is not None pr.connection_lost(None) assert (await t) is None assert pr._drain_waiter is None
async def test_resume_drain_waited() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() t = loop.create_task(pr._drain_helper()) await asyncio.sleep(0) assert pr._drain_waiter is not None pr.resume_writing() assert (await t) is None assert pr._drain_waiter is None
async def test_resume_drain_waited() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() t = loop.create_task(pr._drain_helper()) await asyncio.sleep(0) assert pr._drain_waiter is not None pr.resume_writing() assert (await t) is None assert pr._drain_waiter is None
async def test_parallel_drain_race_condition() -> None: loop = asyncio.get_event_loop() pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() ts = [loop.create_task(pr._drain_helper()) for _ in range(5)] assert not (await asyncio.wait( ts, timeout=0.5))[0], "All draining tasks must be pending" assert pr._drain_waiter is not None pr.resume_writing() await asyncio.gather(*ts) assert pr._drain_waiter is None
async def test_lost_drain_waited_exception(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() t = loop.create_task(pr._drain_helper()) await asyncio.sleep(0) assert pr._drain_waiter is not None exc = RuntimeError() pr.connection_lost(exc) with pytest.raises(RuntimeError) as cm: await t assert cm.value is exc assert pr._drain_waiter is None
async def test_resume_drain_cancelled(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() pr.connection_made(tr) pr.pause_writing() fut = loop.create_future() async def wait(): fut.set_result(None) await pr._drain_helper() t = loop.create_task(wait()) await fut t.cancel() assert pr._drain_waiter is not None pr.resume_writing() with suppress(asyncio.CancelledError): await t assert pr._drain_waiter is None
def test_connection_made(loop): pr = BaseProtocol(loop=loop) tr = mock.Mock() assert pr.transport is None pr.connection_made(tr) assert pr.transport is not None