Skip to content

Commit

Permalink
Ensure proper cleanup in MockImapServer
Browse files Browse the repository at this point in the history
* Send responses to all ongoing commands.
* Then close the writer so that the server can be shutdown.
  This is required so `wait_closed` can finished which has been fixed
  in Python 3.12 to actually block, see
  python/cpython#79033
  • Loading branch information
jgosmann committed Jan 7, 2024
1 parent ad3926a commit f993a4b
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions dmarc_metrics_exporter/tests/test_imap_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
start_server,
wait_for,
)
from typing import Callable, Coroutine, Dict, Optional
from typing import Callable, Coroutine, Dict, List, Optional

import pytest

Expand Down Expand Up @@ -408,6 +408,7 @@ def __init__(
self.command_handlers = command_handlers or {}
self._server = None
self._write_lock = asyncio.Lock()
self._tasks: List[asyncio.Task] = []

@property
def connection_config(self) -> ConnectionConfig:
Expand Down Expand Up @@ -448,7 +449,12 @@ async def _client_connected_cb(self, reader: StreamReader, writer: StreamWriter)
await writer.drain()
remainder += await reader.readline()

asyncio.create_task(self._finish_command_handling(tag, command, writer))
self._tasks.append(
asyncio.create_task(self._finish_command_handling(tag, command, writer))
)

await asyncio.gather(*self._tasks)
writer.close()

async def _finish_command_handling(
self, tag: bytes, command: bytes, writer: StreamWriter
Expand Down

0 comments on commit f993a4b

Please sign in to comment.