Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions simulation_bridge/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Convenience utilities for the Simulation Bridge package."""

from .src.protocol_adapters.inmemory.inmemory_adapter import SimulationBridge

__all__ = ["SimulationBridge"]
8 changes: 0 additions & 8 deletions simulation_bridge/src/protocol_adapters/adapters_signal.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@
"message_received_result_rest": "RESTAdapter.publish_result_message_rest"
},
"class": ".rest.rest_adapter.RESTAdapter"
},
"inmemory": {
"enabled": true,
"signals": {
"message_received_input_inmemory": "BridgeCore.handle_input_message",
"message_received_result_inmemory": "InMemoryAdapter._handle_result"
},
"class": ".inmemory.inmemory_adapter.InMemoryAdapter"
}
}
}
21 changes: 21 additions & 0 deletions simulation_bridge/src/protocol_adapters/inmemory_signal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"protocols": {
"rabbitmq": {
"enabled": true,
"signals": {
"message_received_input_rabbitmq": "BridgeCore.handle_input_message",
"message_received_result_rabbitmq": "BridgeCore.handle_result_rabbitmq_message",
"message_received_result_unknown": "BridgeCore.handle_result_unknown_message"
},
"class": ".rabbitmq.rabbitmq_adapter.RabbitMQAdapter"
},
"inmemory": {
"enabled": true,
"signals": {
"message_received_input_inmemory": "BridgeCore.handle_input_message",
"message_received_result_inmemory": "InMemoryAdapter._handle_result"
},
"class": ".inmemory.inmemory_adapter.InMemoryAdapter"
}
}
}
21 changes: 19 additions & 2 deletions simulation_bridge/src/utils/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
# Configure logger
logger = get_logger()

# In-memory mode flag
__inmemory_mode = False


def set_inmemory_mode(enabled: bool) -> None:
"""Enable or disable loading of the in-memory protocol configuration.

The loader defaults to using the standard protocol configuration file.
Call this function with ``True`` only when running entirely in-memory.
"""
global __inmemory_mode
__inmemory_mode = enabled


def load_config(
config_path: Optional[Union[str, Path]] = None) -> Dict[str, Any]:
Expand Down Expand Up @@ -92,7 +105,11 @@ def load_protocol_config() -> Dict[str, list]:
"""
Load the protocol configuration from a JSON file.
"""
config_file = Path(__file__).parent.parent / \
"protocol_adapters/adapters_signal.json"
base_path = Path(__file__).parent.parent / "protocol_adapters"

if __inmemory_mode:
config_file = base_path / "inmemory_signal.json"
else:
config_file = base_path / "adapters_signal.json"
with open(config_file, 'r', encoding='utf-8') as f:
return json.load(f)["protocols"]
21 changes: 19 additions & 2 deletions simulation_bridge/test/unit/test_config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,32 @@ def test_substitute_env_vars_empty_default(self):
class TestLoadProtocolConfig: # pylint: disable=too-few-public-methods
"""Test cases for load_protocol_config function."""

def test_load_protocol_config_success(self, protocol_json):
"""Test successful loading and parsing of protocol config JSON file."""
def test_load_protocol_config_default(self, protocol_json):
"""Load configuration in default (file) mode."""
json_str = json.dumps(protocol_json)
mock_open = mock.mock_open(read_data=json_str)

config_file = Path(config_loader.__file__).parent.parent / \
"protocol_adapters/adapters_signal.json"

with mock.patch("builtins.open", mock_open) as m_open:
config_loader.set_inmemory_mode(False)
protocols = config_loader.load_protocol_config()

m_open.assert_called_once_with(config_file, 'r', encoding='utf-8')
assert isinstance(protocols, list)
assert protocols == protocol_json["protocols"]

def test_load_protocol_config_inmemory(self, protocol_json):
"""Load configuration when in-memory mode is enabled."""
json_str = json.dumps(protocol_json)
mock_open = mock.mock_open(read_data=json_str)

config_file = Path(config_loader.__file__).parent.parent / \
"protocol_adapters/inmemory_signal.json"

with mock.patch("builtins.open", mock_open) as m_open:
config_loader.set_inmemory_mode(True)
protocols = config_loader.load_protocol_config()

m_open.assert_called_once_with(config_file, 'r', encoding='utf-8')
Expand Down
Loading