def test_send_sendSampleCommandToSingleAgent_commandSentToSingleAgent( context: BrokerContext, mapper: CommandMapper): given_agents = ( ConnectionSettings('9.9.9.9', 1000), ConnectionSettings('9.9.9.8', 1000), ConnectionSettings('9.9.9.7', 1000), ) context.register_agents(given_agents, mapper, given_agents[0]) context.wait_some() context.dump_outgoing_packets() expected_topology = (ConnectionSettings('0.1.2.3', 123), ) given_response_command = NetTopologyCommand(expected_topology) context.broker.send(Payload(given_response_command, given_agents[0])) context.wait_some() outgoing_packets = context.dump_outgoing_packets() def meets_expectation(packets, address): properly_addressed = [p.data for p in packets if p.address == address] commands = [mapper.map_from_bytes(data) for data in properly_addressed] return any(command.agents == expected_topology for command in commands) assert meets_expectation(outgoing_packets, given_agents[0]) assert not meets_expectation(outgoing_packets, given_agents[1]) assert not meets_expectation(outgoing_packets, given_agents[2])
def test_fromTuple_sampleAddress_validResult(): given_address = '0.0.0.0' given_port = 12345 given_tuple = (given_address, given_port) expected_result = ConnectionSettings(given_address, given_port) actual_result = ConnectionSettings.from_tuple(given_tuple) assert actual_result == expected_result
def test_toTuple_sampleAddress_validTuple(): given_address = '0.0.0.0' given_port = 12345 expected_result = (given_address, given_port) sut = ConnectionSettings(given_address, given_port) actual_result = sut.to_tuple() assert actual_result == expected_result
def test_addOrUpdateMany_sampleAddresses_initialAndReturnedAddressesAreEqual(): given_addresses = [ ConnectionSettings('1.2.3.4', 1000), ConnectionSettings('1.2.3.5', 2000) ] sut = Topology(TimeoutService(123)) sut.add_or_update_many(given_addresses) assert set(sut.get_all_addresses()) == set(given_addresses)
def test_getAddresses_none_allRegisteredAddresses(): given_addresses = [ ConnectionSettings('1.2.3.4', 1000), ConnectionSettings('1.2.3.5', 2000) ] sut = Topology(TimeoutService(123)) sut.add_or_update_many(given_addresses) assert all(addr in sut.get_addresses(None) for addr in given_addresses)
def test_packet_sampleData_expectedPacket(): given_data = b'12345678' given_address = ConnectionSettings('0.0.0.0', 12345) actual_packet = Packet(given_data, given_address) assert actual_packet.address == given_address assert actual_packet.data == given_data
def test_registerCommand_invokeSubproblemInProgressOnDifferentAgent_returnsEmptyList( ): given_id = TestId(4) given_pool = TestPool() given_command = RegisterCommand(given_id) given_pool.register(given_id, ConnectionSettings('1.2.3.4', 9999)) register_invoke_output = given_command.invoke(given_pool) assert len(register_invoke_output) == 0 assert given_id in given_pool.in_progress_pool
def test_periodicalImalive_wait_brokerSendsMultipleImalivePackets( context: BrokerContext, mapper: CommandMapper): context.set_imalive_interval(0.1) given_agent = ConnectionSettings('1.2.3.4', 1234) context.send_to_broker(get_imalive_packet(given_agent)) sleep(0.6) outgoing_packets = context.dump_outgoing_packets() commands = [mapper.map_from_bytes(p.data) for p in outgoing_packets] imalive_commands = [c for c in commands if isinstance(c, ImAliveCommand)] assert 4 <= len(imalive_commands) <= 6
def test_resultCommand_invokeResultOfInProgressSubproblemReceived_subproblemIsCompleted( ): given_id = TestId(4) given_result = TestResult(42) given_pool = TestPool() given_command = ResultCommand(given_id, given_result) given_pool.register(given_id, ConnectionSettings('1.1.1.1', 9999)) given_command.invoke(given_pool) assert given_id in given_pool.results assert given_pool.results[given_id] == given_result
def test_prune_sampleTopology_expectedAgentsAreReturnedFromPruned(): '''This test verifies values returned from prune().''' given_addresses = [ ConnectionSettings('1.1.1.1', 1234), ConnectionSettings('1.1.1.2', 1234), ConnectionSettings('1.1.1.3', 1234) ] given_timeout = 5.0 time_provider = TimeProviderMock(given_timeout) sut = Topology(time_provider) time_provider.set_time(0.0) sut.add_or_update_many(given_addresses) time_provider.set_time(2.5) sut.add_or_update(given_addresses[0]) time_provider.set_time(5.0) pruned_agents = sut.prune() assert set(pruned_agents) == {given_addresses[1], given_addresses[2]}
def test_resultCommand_invokeResultOfSubproblemAlreadyInResultsAndReceivedResultIsDifferent_nothingHappens( ): given_id = TestId(4) given_result = TestResult(42) current_result = TestResult(16) given_pool = TestPool() given_command = ResultCommand(given_id, given_result) given_pool.register(given_id, ConnectionSettings('1.1.1.1', 9999)) given_pool.complete(given_id, current_result) given_command.invoke(given_pool) assert given_id in given_pool.results assert given_pool.results[given_id] == current_result
def test_prune_sampleTopology_expectedAgentsAreRemovedFromTopology(): '''This test verifies if agents are actually removed from a topology.''' given_addresses = [ ConnectionSettings('1.1.1.1', 1234), ConnectionSettings('1.1.1.2', 1234), ConnectionSettings('1.1.1.3', 1234) ] given_timeout = 5.0 time_provider = TimeProviderMock(given_timeout) sut = Topology(time_provider) time_provider.set_time(0.0) sut.add_or_update_many(given_addresses) time_provider.set_time(2.5) sut.add_or_update(given_addresses[0]) time_provider.set_time(5.0) _ = sut.prune() all_addresses = set(sut.get_all_addresses()) assert all_addresses == {given_addresses[0]}
def higher_priority_address(): return ConnectionSettings('127.127.127.127', 40000)
def test_getAddresses_unexpectedTopology_raise(): given_address = ConnectionSettings('1.2.3.4', 1000) sut = Topology(TimeoutService(123)) with pytest.raises(RecipientNotRegisteredError): sut.get_addresses(given_address)
def test_getAddresses_sampleTopology_iterableWithGivenTopology(): given_address = ConnectionSettings('1.2.3.4', 1000) sut = Topology(TimeoutService(123)) sut.add_or_update(given_address) assert given_address in sut.get_addresses(given_address)
def test_withForbidden_addForbiddenAddress_addressNotAdded(): given_address = ConnectionSettings('1.2.3.4', 1000) sut = Topology(TimeoutService(123)).with_forbidden(given_address) assert given_address not in sut.get_all_addresses()
def lower_priority_address(): return ConnectionSettings('127.127.127.126', 40000)
def sample(): return ConnectionSettings('1.2.3.4', 6789)
def test_fromTuple_sampleAddress_validResult(): given_address = '0.0.0.0' given_port = 12345 given_tuple = (given_address, given_port) expected_result = ConnectionSettings(given_address, given_port) actual_result = ConnectionSettings.from_tuple(given_tuple) assert actual_result == expected_result def test_packet_sampleData_expectedPacket(): given_data = b'12345678' given_address = ConnectionSettings('0.0.0.0', 12345) actual_packet = Packet(given_data, given_address) assert actual_packet.address == given_address assert actual_packet.data == given_data @pytest.mark.parametrize('first,other', [ (ConnectionSettings('1.2.3.4', 101), ConnectionSettings('1.2.3.4', 100)), (ConnectionSettings('1.2.3.5', 100), ConnectionSettings('1.2.3.4', 100)), (ConnectionSettings('2.1.1.1', 100), ConnectionSettings('1.1.1.2', 100)), ]) def test_getPriority_sampleAddresses_firstGreaterThanOther( first: ConnectionSettings, other: ConnectionSettings): assert first.get_priority() > other.get_priority()
def forbid_local_interfaces_addresses(self, port: int): local_interfaces_ips = get_local_interfaces_ip_addresses() for address in local_interfaces_ips: self.with_forbidden(ConnectionSettings(address, port)) return self
def get_address(self) -> ConnectionSettings: return ConnectionSettings('412.412.412.412', 99999)
def test_addOrUpdate_sampleAddress_addressPresentInReturnedAddresses(): given_address = ConnectionSettings('1.2.3.4', 1234) sut = Topology(TimeoutService(123)) sut.add_or_update(given_address) assert given_address in sut.get_all_addresses()
def test_getPriority_sampleAddresses_firstGreaterThanOther( first: ConnectionSettings, other: ConnectionSettings): assert first.get_priority() > other.get_priority()
def create() -> NetTopologyCommand: network_connection_1 = ConnectionSettings('192.168.192.1', 2137) network_connection_2 = ConnectionSettings('192.168.192.2', 2137) agents = (network_connection_1, network_connection_2) command = NetTopologyCommand(agents) return command
def discover_network(self): command = ImAliveCommand() port = self._connection.get_address().port LAN_broadcast_settings = ConnectionSettings('<broadcast>', port) self.send(Payload(command, LAN_broadcast_settings))
def other(): return ConnectionSettings('1.2.3.5', 6789)