diff --git a/hathor/cli/events_simulator/event_forwarding_websocket_factory.py b/hathor/cli/events_simulator/event_forwarding_websocket_factory.py index 5b1dad9b3..f07a5b8e2 100644 --- a/hathor/cli/events_simulator/event_forwarding_websocket_factory.py +++ b/hathor/cli/events_simulator/event_forwarding_websocket_factory.py @@ -29,6 +29,7 @@ def __init__(self, simulator: 'Simulator', *args: Any, **kwargs: Any) -> None: super().__init__(*args, **kwargs) def buildProtocol(self, _: IAddress) -> 'EventForwardingWebsocketProtocol': + from hathor.cli.events_simulator.event_forwarding_websocket_protocol import EventForwardingWebsocketProtocol protocol = EventForwardingWebsocketProtocol(self._simulator) protocol.factory = self return protocol diff --git a/hathor/cli/events_simulator/events_simulator.py b/hathor/cli/events_simulator/events_simulator.py index 9db73db66..135a95296 100644 --- a/hathor/cli/events_simulator/events_simulator.py +++ b/hathor/cli/events_simulator/events_simulator.py @@ -14,12 +14,16 @@ import os from argparse import ArgumentParser, Namespace +from typing import TYPE_CHECKING from autobahn.twisted.resource import WebSocketResource from structlog import get_logger from twisted.web.resource import Resource from twisted.web.server import Site +if TYPE_CHECKING: + from hathor.reactor import ReactorProtocol + DEFAULT_PORT = 8080 logger = get_logger() @@ -39,12 +43,11 @@ def create_parser() -> ArgumentParser: return parser -def execute(args: Namespace) -> None: +def execute(args: Namespace, reactor: 'ReactorProtocol') -> None: from hathor.conf import UNITTESTS_SETTINGS_FILEPATH os.environ['HATHOR_CONFIG_YAML'] = UNITTESTS_SETTINGS_FILEPATH from hathor.cli.events_simulator.event_forwarding_websocket_factory import EventForwardingWebsocketFactory from hathor.cli.events_simulator.scenario import Scenario - from hathor.reactor import initialize_global_reactor from hathor.simulator import Simulator try: @@ -53,7 +56,6 @@ def execute(args: Namespace) -> None: possible_scenarios = [scenario.name for scenario in Scenario] raise ValueError(f'Invalid scenario "{args.scenario}". Choose one of {possible_scenarios}') from e - reactor = initialize_global_reactor() log = logger.new() simulator = Simulator(args.seed) simulator.start() @@ -90,6 +92,8 @@ def execute(args: Namespace) -> None: def main(): + from hathor.reactor import initialize_global_reactor parser = create_parser() args = parser.parse_args() - execute(args) + reactor = initialize_global_reactor() + execute(args, reactor) diff --git a/tests/cli/test_events_simulator.py b/tests/cli/test_events_simulator.py new file mode 100644 index 000000000..83f6049e9 --- /dev/null +++ b/tests/cli/test_events_simulator.py @@ -0,0 +1,38 @@ +# Copyright 2023 Hathor Labs +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from unittest.mock import Mock + +from hathor.cli.events_simulator.event_forwarding_websocket_factory import EventForwardingWebsocketFactory +from hathor.cli.events_simulator.events_simulator import create_parser, execute +from tests.test_memory_reactor_clock import TestMemoryReactorClock + + +def test_events_simulator() -> None: + parser = create_parser() + args = parser.parse_args(['--scenario', 'ONLY_LOAD']) + reactor = TestMemoryReactorClock() + + execute(args, reactor) + reactor.advance(1) + + factory = EventForwardingWebsocketFactory( + simulator=Mock(), + peer_id='test_peer_id', + network='test_network', + reactor=reactor, + event_storage=Mock() + ) + protocol = factory.buildProtocol(Mock()) + + assert protocol is not None