Ejemplo n.º 1
0
def test_imaliveMapping_sampleCommand_byteCommandContainsCommandId():
    command = ImAliveCommand()
    command_id = command.get_identifier()
    mapper = CommandMapper().register(ImAliveCommand)

    command_as_bytes = mapper.map_to_bytes(command)

    assert command_id.encode('UTF-8') in command_as_bytes
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
def test_discoverNetwork_initializeBroker_imAliveIsBroadcasted(
        context: BrokerContext, mapper: CommandMapper):
    context.broker.discover_network()

    context.wait_some()
    outgoing_packets = context.dump_outgoing_packets()

    packet_data_as_bytes = outgoing_packets[0].data
    packet_address = outgoing_packets[0].address
    packet_data = mapper.map_from_bytes(packet_data_as_bytes)

    assert isinstance(packet_data, ImAliveCommand)
    assert packet_address.address == '<broadcast>'
Ejemplo n.º 4
0
def test_mapToBytes_unregisteredCommandraise():
    given_command = CommandFactory.sample()
    given_mapper = CommandMapper()

    with pytest.raises(CommandTypeNotRegisteredError):
        given_mapper.map_to_bytes(given_command)
Ejemplo n.º 5
0
 def create() -> CommandMapper:
     return CommandMapper().register(SampleCommand)
Ejemplo n.º 6
0
 def create(type_to_register: type) -> CommandMapper:
     return CommandMapper().register(type_to_register)
Ejemplo n.º 7
0
def mapper():
    return CommandMapper() \
        .register(ImAliveCommand) \
        .register(NetTopologyCommand)
Ejemplo n.º 8
0
 def register_agents(self, agents: Tuple[ConnectionSettings],
                     command_mapper: CommandMapper,
                     sender: ConnectionSettings):
     topology_command = NetTopologyCommand(agents)
     topology_as_bytes = command_mapper.map_to_bytes(topology_command)
     self.send_to_broker(Packet(topology_as_bytes, sender))
Ejemplo n.º 9
0
 def __init__(self):
     self._mapper = CommandMapper()
     self._connection = NetworkIOMock()
     self._settings = BrokerSettings('1.1.1.1', 999, 999)
     self.broker = Broker(self._connection, self._mapper, self._settings)
     self.broker.start()