Skip to content

Commit

Permalink
Add unit test for queue consumer (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
nerdai authored Jun 7, 2024
1 parent d1b9bf3 commit d81ab33
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions tests/message_queue_consumers/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import asyncio
import pytest
from typing import Any, List
from llama_index.core.bridge.pydantic import PrivateAttr
from agentfile.message_consumers.base import BaseMessageQueueConsumer
from agentfile.message_queues.simple import SimpleMessageQueue
from agentfile.messages.base import QueueMessage


class MockMessageConsumer(BaseMessageQueueConsumer):
processed_messages: List[QueueMessage] = []
_lock: asyncio.Lock = PrivateAttr(default_factory=asyncio.Lock)

async def _process_message(self, message: QueueMessage, **kwargs: Any) -> None:
async with self._lock:
self.processed_messages.append(message)


@pytest.mark.asyncio()
async def test_consumer_consumes_messages() -> None:
# Arrange
consumer_one = MockMessageConsumer()
mq = SimpleMessageQueue()
task = asyncio.create_task(mq.start())

# Act
await consumer_one.start_consuming(message_queue=mq)
await asyncio.sleep(0.1)
await mq.publish(QueueMessage(id_="1"))
await mq.publish(QueueMessage(id_="2"))

# Give some time for last message to get published and sent to consumers
await asyncio.sleep(0.5)
task.cancel()

# Assert
assert consumer_one.id_ in [
c.id_ for c in await mq.get_consumers(consumer_one.message_type)
]
assert ["1", "2"] == [m.id_ for m in consumer_one.processed_messages]

0 comments on commit d81ab33

Please sign in to comment.