Skip to content

Commit

Permalink
Controller On Init Now Subscribes All Users To Group Chat Kafka Topic
Browse files Browse the repository at this point in the history
All participating users must be subscribers to the group chat kafka topic.
  • Loading branch information
alvindera97 committed Sep 1, 2024
1 parent 79bcc09 commit 9b1c767
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 7 additions & 2 deletions controller/controller_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import asyncio
import random
from typing import Optional
from typing import Optional, List

import websockets

Expand Down Expand Up @@ -34,7 +34,12 @@ def __init__(self, number_of_users: int, ws_url: str) -> None:
assert number_of_users > 0

self.ws_url = ws_url
self.participating_users = [User() for _ in range(number_of_users)]
self.participating_users: List[User] = [User() for i in range(number_of_users)]

for _, user in enumerate(self.participating_users):
chat_hash = ws_url.split("/")[-1]
user.consumer.subscribe([chat_hash])

self.first_publisher: User = random.choice(self.participating_users)

self.first_publisher.role = Role.PUBLISHER
Expand Down
13 changes: 13 additions & 0 deletions tests/controller/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,16 @@ def test_controller_method_for_connecting_to_websocket_can_send_messages_to_webs
pass
except Exception as e:
self.fail(f"Exception not expected from this test, raised exception is: {e}")

def test_that_on_init_of_controller_all_users_are_subscribed_to_group_chat_kafka_topic(self) -> None:
"""
Test that on initialisation of application controller, all users are subscribed to the group chat
kafka topic.
:return: None
"""
with patch("controller.controller_def.Controller.connect_ws", new_callable=lambda: AsyncMock()):
controller = Controller(random.randint(1, 10), self.ws_url)

for user in controller.participating_users:
self.assertEqual(len(user.consumer.subscription()), 1)
self.assertEqual(set(user.consumer.subscription()).pop(), os.getenv('TEST_CHAT_UUID').split("/")[-1])

0 comments on commit 9b1c767

Please sign in to comment.