async def test_worker_chat_not_started(event_loop): with mock.patch('blackhole.worker.Worker.start') as mock_start: worker = Worker('1', []) assert mock_start.called is True worker._started = False with mock.patch('blackhole.worker.Worker.start') as mock_chat_start, \ mock.patch('blackhole.worker.Worker.stop') as mock_chat_stop: await worker.chat(None) assert mock_chat_start.called is False assert mock_chat_stop.called is False
async def test_worker_heartbeat_started_bad_ping(event_loop): with mock.patch('blackhole.worker.Worker.start') as mock_start: worker = Worker('1', []) assert mock_start.called is True worker._started = True worker.ping = time.monotonic() - 120 with mock.patch('blackhole.worker.Worker.start') as mock_heartbeat_start, \ mock.patch('blackhole.worker.Worker.stop') as mock_heartbeat_stop: await worker.heartbeat(None) assert mock_heartbeat_start.called is True assert mock_heartbeat_stop.called is True
async def test_worker_chat_started_restart(event_loop): with mock.patch('blackhole.worker.Worker.start') as mock_start: worker = Worker('1', []) assert mock_start.called is True worker._started = True with mock.patch('blackhole.worker.Worker.start') as mock_chat_start, \ mock.patch('blackhole.worker.Worker.stop') as mock_chat_stop, \ mock.patch('asyncio.StreamReader.read', side_effect=Exception): reader = asyncio.StreamReader() await worker.chat(reader) assert mock_chat_start.called is True assert mock_chat_stop.called is True
async def test_worker_heartbeat_started_good_ping(event_loop): with mock.patch('blackhole.worker.Worker.start') as mock_start: worker = Worker('1', []) assert mock_start.called is True worker._started = True worker.ping = time.monotonic() + 120 writer = BytesIO() async def reset(): await asyncio.sleep(20) worker._started = False reset_task = asyncio.Task(reset()) await worker.heartbeat(writer) assert worker._started is False reset_task.cancel()
async def test_worker_chat_started_good_message(event_loop): with mock.patch('blackhole.worker.Worker.start') as mock_start: worker = Worker('1', [], loop=event_loop) assert mock_start.called is True worker._started = True worker.ping = 987654321 reader = asyncio.StreamReader() async def reset(): reader.feed_data(protocols.PONG) worker._started = False reset_task = asyncio.Task(reset()) with mock.patch('time.monotonic', return_value=123456789): await worker.chat(reader) assert worker._started is False assert worker.ping == 123456789 reset_task.cancel()