From 12bb19789fa9e969025c2c5505098ba419719aa1 Mon Sep 17 00:00:00 2001 From: Hannes Diedrich Date: Tue, 24 Nov 2020 12:06:03 +0100 Subject: [PATCH] deleted-area-redis_external_connection.py --- .../models/area/redis_external_connection.py | 76 -------------- tests/test_external_connection.py | 98 ------------------- 2 files changed, 174 deletions(-) delete mode 100644 src/d3a/models/area/redis_external_connection.py delete mode 100644 tests/test_external_connection.py diff --git a/src/d3a/models/area/redis_external_connection.py b/src/d3a/models/area/redis_external_connection.py deleted file mode 100644 index 1c837325f..000000000 --- a/src/d3a/models/area/redis_external_connection.py +++ /dev/null @@ -1,76 +0,0 @@ -from redis import StrictRedis -import json -from d3a.d3a_core.redis_connections.redis_communication import REDIS_URL -from d3a.models.strategy.external_strategy import ExternalStrategy - - -class RedisAreaExternalConnection: - def __init__(self, area): - self.area = area - self.redis_db = StrictRedis.from_url(REDIS_URL, retry_on_timeout=True) - self.pubsub = self.redis_db.pubsub() - self.sub_to_area_event() - self.areas_to_register = [] - self.areas_to_unregister = [] - - def market_cycle_event(self): - self.unregister_pending_areas() - self.register_new_areas() - - def unregister_pending_areas(self): - if not self.areas_to_unregister: - return - - for area in self.areas_to_unregister: - try: - area_object = next(child for child in self.area.children if child.name == area) - area_object.deactivate() - self.area.children.remove(area_object) - except Exception as e: - self.area.log.error(f"Unsubscribing of area {area} failed with error {str(e)}.") - self.publish(f"{self.area.slug}/unregister_participant/response", - json.dumps({"response": "failed"})) - else: - self.publish(f"{self.area.slug}/unregister_participant/response", - json.dumps({"response": "success"})) - area_object.strategy.shutdown() - - self.areas_to_unregister = [] - - def register_new_areas(self): - if not self.areas_to_register: - return - for new_area in self.areas_to_register: - area_object = self.area.__class__(name=new_area) - area_object.parent = self.area - self.area.children.append(area_object) - area_object.strategy = ExternalStrategy(area_object) - area_object.activate() - - self.publish(f"{self.area.slug}/register_participant/response", - self._subscribe_channel_list(area_object)) - self.areas_to_register = [] - - def _subscribe_channel_list(self, new_area): - return json.dumps(new_area.strategy.get_channel_list()) - - def publish(self, channel, data): - self.redis_db.publish(channel, data) - - def channel_register_callback(self, payload): - payload_data = json.loads(payload["data"]) - area_name = payload_data["name"] - self.areas_to_register.append(area_name) - - def channel_unregister_callback(self, payload): - payload_data = json.loads(payload["data"]) - area_name = payload_data["name"] - self.areas_to_unregister.append(area_name) - - def sub_to_area_event(self): - channel = f"{self.area.slug}/register_participant" - channel_unregister = f"{self.area.slug}/unregister_participant" - - self.pubsub.subscribe(**{channel: self.channel_register_callback, - channel_unregister: self.channel_unregister_callback}) - self.pubsub.run_in_thread(daemon=True) diff --git a/tests/test_external_connection.py b/tests/test_external_connection.py deleted file mode 100644 index 0ac8269b2..000000000 --- a/tests/test_external_connection.py +++ /dev/null @@ -1,98 +0,0 @@ -import unittest -from unittest.mock import MagicMock -import json -import d3a.models.area.redis_external_connection -from d3a.models.area import Area -from d3a.models.area.redis_external_connection import RedisAreaExternalConnection - - -class TestExternalConnectionRedis(unittest.TestCase): - - def setUp(self): - self.ext_strategy_mock = MagicMock - self.ext_strategy_mock.get_channel_list = lambda s: {} - d3a.models.area.redis_external_connection.ExternalStrategy = self.ext_strategy_mock - d3a.models.area.redis_external_connection.StrictRedis = MagicMock() - redis_db_object = MagicMock() - redis_db_object.pubsub = MagicMock - d3a.models.area.redis_external_connection.StrictRedis.from_url = \ - MagicMock(return_value=redis_db_object) - self.area = Area(name="base_area") - self.external_connection = RedisAreaExternalConnection(self.area) - - def tearDown(self): - pass - - def add_external_connections_to_area(self): - area_list = ["kreuzberg", "friedrichschain", "prenzlauer berg"] - self.external_connection.areas_to_register = area_list - self.external_connection.register_new_areas() - return area_list - - def test_external_connection_subscribes_to_register_unregister(self): - self.external_connection.pubsub.subscribe.assert_called_once_with( - **{ - "base-area/register_participant": - self.external_connection.channel_register_callback, - "base-area/unregister_participant": - self.external_connection.channel_unregister_callback - } - ) - - def test_register_message_adds_area_to_buffer(self): - self.external_connection.channel_register_callback( - {"data": json.dumps({"name": "berlin"})} - ) - assert self.external_connection.areas_to_register == ["berlin"] - - def test_unregister_message_adds_area_to_buffer(self): - self.external_connection.channel_unregister_callback( - {"data": json.dumps({"name": "berlin"})} - ) - assert self.external_connection.areas_to_unregister == ["berlin"] - - def test_register_new_areas_creates_new_child_areas_from_buffer(self): - area_list = self.add_external_connections_to_area() - assert len(self.area.children) == 3 - assert not self.external_connection.areas_to_register - assert set(ch.name for ch in self.area.children) == set(area_list) - assert all(isinstance(ch.strategy, self.ext_strategy_mock) for ch in self.area.children) - assert all(ch.parent == self.area for ch in self.area.children) - assert all(ch.active for ch in self.area.children) - assert self.external_connection.redis_db.publish.call_count == 3 - for i, created_area in enumerate(area_list): - assert self.external_connection.redis_db.publish.call_args_list[i][0][0] == \ - "base-area/register_participant/response" - - def test_unregister_areas_deletes_children_from_area(self): - area_list = self.add_external_connections_to_area() - assert set(ch.name for ch in self.area.children) == set(area_list) - # Keep track how many times the publish method was called when setting up the areas - call_count = self.external_connection.redis_db.publish.call_count - # Monkeypatch shutdown method of ExternalStrategy class - for ch in self.area.children: - ch.strategy.shutdown = MagicMock() - - self.external_connection.areas_to_unregister = ["prenzlauer berg"] - self.external_connection.unregister_pending_areas() - assert len(self.area.children) == 2 - assert self.external_connection.redis_db.publish.call_count == call_count + 1 - self.external_connection.redis_db.publish.assert_called_with( - "base-area/unregister_participant/response", json.dumps({"response": "success"})) - - self.external_connection.areas_to_unregister = ["friedrichschain"] - self.external_connection.unregister_pending_areas() - assert len(self.area.children) == 1 - assert self.external_connection.redis_db.publish.call_count == call_count + 2 - self.external_connection.redis_db.publish.assert_called_with( - "base-area/unregister_participant/response", json.dumps({"response": "success"})) - - def test_unregister_area_that_does_not_exist_returns_an_error(self): - area_list = self.add_external_connections_to_area() - assert set(ch.name for ch in self.area.children) == set(area_list) - - self.external_connection.areas_to_unregister = ["schoneberg"] - self.external_connection.unregister_pending_areas() - assert len(self.area.children) == 3 - self.external_connection.redis_db.publish.assert_called_with( - "base-area/unregister_participant/response", json.dumps({"response": "failed"}))