Skip to content

Commit

Permalink
Fixes from expeimentation
Browse files Browse the repository at this point in the history
import of parseJson changed to absolute path
subscriptions done separately so none can be handled
  • Loading branch information
asgibson committed Oct 6, 2023
1 parent a428921 commit 530b5a3
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
11 changes: 8 additions & 3 deletions onair/data_handling/redis_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import json

from onair.data_handling.on_air_data_source import OnAirDataSource
from .tlm_json_parser import parseJson
from onair.data_handling.tlm_json_parser import parseJson
from onair.src.util.print_io import *
from onair.data_handling.parser_util import *

Expand Down Expand Up @@ -50,12 +50,17 @@ def connect(self):

def subscribe(self, subscriptions):
"""Subscribe to REDIS message channel(s) and launch listener thread."""
if self.server.ping():
if len(subscriptions) != 0 and self.server.ping():
self.pubsub = self.server.pubsub()
self.pubsub.subscribe(*subscriptions)

for s in subscriptions:
self.pubsub.subscribe(s)
print_msg(f"Subscribing to channel {s}")

listen_thread = threading.Thread(target=self.message_listener)
listen_thread.start()
else:
print_msg(f"No subscriptions given!")

def parse_meta_data_file(self, meta_data_file, ss_breakdown):
configs = extract_meta_data_handle_ss_breakdown(meta_data_file, ss_breakdown)
Expand Down
48 changes: 41 additions & 7 deletions test/onair/data_handling/test_redis_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ def test_redis_adapter_DataSource_fails_to_connect_to_server(mocker):
assert cut.server == fake_server

# subscribe_message tests
def test_redis_adapter_DataSource_subscribe_sends_given_subscriptions_as_set_and_starts_listening_when_server_available(mocker):
def test_redis_adapter_DataSource_subscribe_subscribes_to_each_given_subscription_and_starts_listening_when_server_available(mocker):
# Arrange
arg_subscriptions = [MagicMock()] * pytest.gen.randint(0, 10) # 0 to 10 arbitrary

fake_server = MagicMock()
fake_pubsub = MagicMock()
fake_subscription = MagicMock()
fake_thread = MagicMock()

cut = DataSource.__new__(DataSource)
cut.server = fake_server

mocker.patch.object(fake_server, 'ping', return_value=True)
mocker.patch.object(fake_server, 'pubsub', return_value=fake_pubsub)
mocker.patch.object(fake_pubsub, 'subscribe', return_value=fake_subscription)
mocker.patch.object(fake_pubsub, 'subscribe')
mocker.patch(redis_adapter.__name__ + '.print_msg')
mocker.patch('threading.Thread', return_value=fake_thread)
mocker.patch.object(fake_thread, 'start')

Expand All @@ -138,16 +138,18 @@ def test_redis_adapter_DataSource_subscribe_sends_given_subscriptions_as_set_and
# Assert
assert fake_server.ping.call_count == 1
assert fake_server.pubsub.call_count == 1
assert fake_pubsub.subscribe.call_count == 1
assert fake_pubsub.subscribe.call_args_list[0].args == (*arg_subscriptions,)
assert fake_pubsub.subscribe.call_count == len(arg_subscriptions)
for i in range(len(arg_subscriptions)):
assert fake_pubsub.subscribe.call_args_list[i].args == (arg_subscriptions[i],)
assert redis_adapter.print_msg.call_args_list[i].args == (f"Subscribing to channel {arg_subscriptions[i]}",)
assert threading.Thread.call_count == 1
assert threading.Thread.call_args_list[0].kwargs == ({'target': cut.message_listener})
assert fake_thread.start.call_count == 1
assert cut.pubsub == fake_pubsub

def test_redis_adapter_DataSource_subscribe_does_nothing_when_server_does_not_respond_to_ping(mocker):
def test_redis_adapter_DataSource_subscribe_states_no_subscriptions_given_when_empty(mocker):
# Arrange
arg_channel = str(MagicMock())
arg_subscriptions = []
fake_server = MagicMock()
initial_pubsub = MagicMock()
fake_subscription = MagicMock()
Expand All @@ -157,6 +159,37 @@ def test_redis_adapter_DataSource_subscribe_does_nothing_when_server_does_not_re
cut.pubsub = initial_pubsub

mocker.patch.object(fake_server, 'ping', return_value=False)
mocker.patch(redis_adapter.__name__ + '.print_msg')
mocker.patch.object(fake_server, 'pubsub')
mocker.patch('threading.Thread')
mocker.patch.object(fake_thread, 'start')

# Act
cut.subscribe(arg_subscriptions)

# Assert
assert fake_server.ping.call_count == 0
assert fake_server.pubsub.call_count == 0
assert threading.Thread.call_count == 0
assert fake_thread.start.call_count == 0
assert cut.pubsub == initial_pubsub
assert redis_adapter.print_msg.call_args_list[0].args == ("No subscriptions given!",)

# Note the self.server.ping during runtime will error, not actually return False, but that means code will never run
# this unit test is for completeness of coverage
def test_redis_adapter_DataSource_subscribe_states_no_subscriptions_given_when_server_does_not_respond_to_ping(mocker):
# Arrange
arg_channel = [MagicMock()]
fake_server = MagicMock()
initial_pubsub = MagicMock()
fake_subscription = MagicMock()
fake_thread = MagicMock()
cut = DataSource.__new__(DataSource)
cut.server = fake_server
cut.pubsub = initial_pubsub

mocker.patch.object(fake_server, 'ping', return_value=False)
mocker.patch(redis_adapter.__name__ + '.print_msg')
mocker.patch.object(fake_server, 'pubsub')
mocker.patch('threading.Thread')
mocker.patch.object(fake_thread, 'start')
Expand All @@ -170,6 +203,7 @@ def test_redis_adapter_DataSource_subscribe_does_nothing_when_server_does_not_re
assert threading.Thread.call_count == 0
assert fake_thread.start.call_count == 0
assert cut.pubsub == initial_pubsub
assert redis_adapter.print_msg.call_args_list[0].args == ("No subscriptions given!",)

# get_next tests

Expand Down

0 comments on commit 530b5a3

Please sign in to comment.