Commit ad6105a2 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Log unexpected responses to unmade commands

parent bdcecc31
Loading
Loading
Loading
Loading
Loading
+15 −0
Original line number Diff line number Diff line
@@ -184,6 +184,21 @@ class SendMessageTests(unittest.TestCase):
			]
			assert await client.send_command("SOME_COMMAND") is None

	@anyio_mock.with_anyio()
	async def test_unexpected(self):
		"""
		Check that unexpected replies are logged cleanly
		"""
		async with self.client as client:
			client.sock.recv.side_effect = [
				b"OK",  # Response to "ATTACH"
				b"UNEXPECTED1",
				b"UNEXPECTED2",
				b"<2>SOME-MESSAGE",
				b"OK",  # Response to "DETACH"
			]
			assert await client.event("SOME-MESSAGE")


class EventTests(unittest.TestCase):
	"""
+9 −5
Original line number Diff line number Diff line
@@ -191,7 +191,10 @@ class BaseClient:
		match = self.event_regex.match(msg)
		if not match:
			# If it's not an event, it must be a reply to a sent message
			await self._reply.put(msg)
			if self._reply.queue:
				await self._reply.queue.put(msg)
			else:
				self.logger.warning("Unexpected response message: %s", msg)
			return

		prio, name, args = match.groups()
@@ -251,16 +254,17 @@ class ReplyManager:

	def __init__(self):
		self.lock = anyio.create_lock()
		self.queue = anyio.create_queue(1)
		self.queue = None

	def __getattr__(self, name):
		return getattr(self.queue, name)

	async def __aenter__(self):
		await self.lock.__aenter__()
		assert self.queue.empty()
		return self.queue
		self.queue = queue = anyio.create_queue(1)
		return queue

	async def __aexit__(self, *exc_info):
		assert self.queue.empty(), "Reply queue must be processed"
		self.queue, queue = None, self.queue
		await self.lock.__aexit__(*exc_info)
		assert queue.empty(), "Reply queue was not processed"