Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 8 additions & 4 deletions hathor/cli/events_simulator/events_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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)
38 changes: 38 additions & 0 deletions tests/cli/test_events_simulator.py
Original file line number Diff line number Diff line change
@@ -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