From 056e6b28dabfc2c386de801dee44a7967a4f5016 Mon Sep 17 00:00:00 2001 From: Magnus Wolffelt Date: Wed, 18 Aug 2021 14:46:55 +0200 Subject: [PATCH] Simplify and rename TACAN registry reserve function (#1559) * Simplify and rename TACAN registry reserve function * Remove unused tacan error --- game/operation/operation.py | 6 +--- gen/tacan.py | 20 +------------ tests/test_tacan.py | 57 +++---------------------------------- 3 files changed, 6 insertions(+), 77 deletions(-) diff --git a/game/operation/operation.py b/game/operation/operation.py index b717a775..29ce0532 100644 --- a/game/operation/operation.py +++ b/game/operation/operation.py @@ -228,11 +228,7 @@ class Operation: if beacon.channel is None: logging.error(f"TACAN beacon has no channel: {beacon.callsign}") else: - cls.tacan_registry.reserve( - beacon.tacan_channel, - TacanUsage.TransmitReceive, - ignore_rules=True, - ) + cls.tacan_registry.mark_unavailable(beacon.tacan_channel) @classmethod def _create_radio_registry( diff --git a/gen/tacan.py b/gen/tacan.py index 127ad86b..96683be7 100644 --- a/gen/tacan.py +++ b/gen/tacan.py @@ -60,13 +60,6 @@ class TacanChannelInUseError(RuntimeError): super().__init__(f"{channel} is already in use") -class TacanChannelForbiddenError(RuntimeError): - """Raised when attempting to reserve a, for technical reasons, forbidden channel.""" - - def __init__(self, channel: TacanChannel) -> None: - super().__init__(f"{channel} is forbidden") - - class TacanRegistry: """Manages allocation of TACAN channels.""" @@ -103,28 +96,17 @@ class TacanRegistry: except StopIteration: raise OutOfTacanChannelsError(band) - def reserve( - self, - channel: TacanChannel, - intended_usage: TacanUsage, - ignore_rules: bool = False, - ) -> None: + def mark_unavailable(self, channel: TacanChannel) -> None: """Reserves the given channel. Reserving a channel ensures that it will not be allocated in the future. Args: channel: The channel to reserve. - intended_usage: What the caller intends to use the tacan channel for. - ignore_rules: Whether to reserve regardless of recommended rules. Raises: TacanChannelInUseError: The given channel is already in use. - TacanChannelForbiddenError: The given channel is forbidden. """ - if not ignore_rules: - if channel.number in UNAVAILABLE[intended_usage][channel.band]: - raise TacanChannelForbiddenError(channel) if channel in self.allocated_channels: raise TacanChannelInUseError(channel) self.allocated_channels.add(channel) diff --git a/tests/test_tacan.py b/tests/test_tacan.py index 92c11f49..d1afa346 100644 --- a/tests/test_tacan.py +++ b/tests/test_tacan.py @@ -2,7 +2,6 @@ from gen.tacan import ( OutOfTacanChannelsError, TacanBand, TacanChannel, - TacanChannelForbiddenError, TacanChannelInUseError, TacanRegistry, TacanUsage, @@ -24,15 +23,6 @@ def test_allocate_first_few_channels() -> None: assert chan3 == TacanChannel(32, TacanBand.X) -def test_reserve_ignoring_rules() -> None: - registry = TacanRegistry() - with pytest.raises(TacanChannelForbiddenError): - registry.reserve(TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive) - registry.reserve( - TacanChannel(16, TacanBand.X), TacanUsage.TransmitReceive, ignore_rules=True - ) - - def test_allocate_different_usages() -> None: """Make sure unallocated channels for one use don't make channels unavailable for other usage""" registry = TacanRegistry() @@ -53,7 +43,7 @@ def test_reserve_all_valid_transmit_receive() -> None: print("All valid x", ALL_VALID_X_TR) for num in ALL_VALID_X_TR: - registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.TransmitReceive) + registry.mark_unavailable(TacanChannel(num, TacanBand.X)) with pytest.raises(OutOfTacanChannelsError): registry.alloc_for_band(TacanBand.X, TacanUsage.TransmitReceive) @@ -69,7 +59,7 @@ def test_reserve_all_valid_a2a() -> None: print("All valid x", ALL_VALID_X_A2A) for num in ALL_VALID_X_A2A: - registry.reserve(TacanChannel(num, TacanBand.X), TacanUsage.AirToAir) + registry.mark_unavailable(TacanChannel(num, TacanBand.X)) with pytest.raises(OutOfTacanChannelsError): registry.alloc_for_band(TacanBand.X, TacanUsage.AirToAir) @@ -80,47 +70,8 @@ def test_reserve_all_valid_a2a() -> None: assert chanTR == TacanChannel(1, TacanBand.X) -@pytest.mark.skip(reason="TODO") -def test_allocate_all() -> None: - pass - - -def test_reserve_invalid_tr_channels() -> None: - registry = TacanRegistry() - some_invalid_channels = [ - TacanChannel(2, TacanBand.X), - TacanChannel(30, TacanBand.X), - TacanChannel(47, TacanBand.X), - TacanChannel(63, TacanBand.X), - TacanChannel(2, TacanBand.Y), - TacanChannel(30, TacanBand.Y), - TacanChannel(64, TacanBand.Y), - TacanChannel(92, TacanBand.Y), - ] - for chan in some_invalid_channels: - with pytest.raises(TacanChannelForbiddenError): - registry.reserve(chan, TacanUsage.TransmitReceive) - - -def test_reserve_invalid_a2a_channels() -> None: - registry = TacanRegistry() - some_invalid_channels = [ - TacanChannel(1, TacanBand.X), - TacanChannel(36, TacanBand.X), - TacanChannel(64, TacanBand.X), - TacanChannel(99, TacanBand.X), - TacanChannel(1, TacanBand.Y), - TacanChannel(36, TacanBand.Y), - TacanChannel(64, TacanBand.Y), - TacanChannel(99, TacanBand.Y), - ] - for chan in some_invalid_channels: - with pytest.raises(TacanChannelForbiddenError): - registry.reserve(chan, TacanUsage.AirToAir) - - def test_reserve_again() -> None: registry = TacanRegistry() with pytest.raises(TacanChannelInUseError): - registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive) - registry.reserve(TacanChannel(1, TacanBand.X), TacanUsage.TransmitReceive) + registry.mark_unavailable(TacanChannel(1, TacanBand.X)) + registry.mark_unavailable(TacanChannel(1, TacanBand.X))