def test_remove_listener_from_universe(): receiver, socket = get_receiver() test_universe_one = 1 test_universe_two = 2 packet_send = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=test_universe_one, dmxData=tuple(range(0, 16))) called = 0 def callback_packet(packet): assert packet_send.__dict__ == packet.__dict__ nonlocal called called += 1 # register listener multiple times receiver.register_listener('universe', callback_packet, universe=test_universe_one) receiver.register_listener('universe', callback_packet, universe=test_universe_two) packet_send.universe = test_universe_one socket.call_on_data(bytes(packet_send.getBytes()), 0) assert called == 1 packet_send.universe = test_universe_two socket.call_on_data(bytes(packet_send.getBytes()), 0) assert called == 2 # change DMX data to trigger a change packet_send.dmxData = tuple(range(16, 32)) packet_send.sequence_increase() test_universe_removed = test_universe_one receiver.remove_listener_from_universe(test_universe_removed) # removing from a universe that does not exist, should do nothing receiver.remove_listener_from_universe(12345) # call to the removed universe should not happen packet_send.universe = test_universe_removed socket.call_on_data(bytes(packet_send.getBytes()), 0) assert called == 2 # other universes should not be affected packet_send.universe = test_universe_two socket.call_on_data(bytes(packet_send.getBytes()), 0) assert called == 3
def test_property_adjustment_and_deconstruction(): # Converting DataPacket -> bytes -> DataPacket should produce the same result, # but with changed properties that are not the default built_packet = DataPacket(cid=(16, 1, 15, 2, 14, 3, 13, 4, 12, 5, 11, 6, 10, 7, 9, 8), sourceName='Test Name', universe=30) built_packet.cid = tuple(range(16)) built_packet.sourceName = '2nd Test Name' built_packet.universe = 31425 built_packet.dmxData = ((200, ) + tuple(range(255, 0, -1)) + tuple(range(255)) + (0, )) built_packet.priority = 12 built_packet.sequence = 45 built_packet.option_StreamTerminated = True built_packet.option_PreviewData = True built_packet.option_ForceSync = True built_packet.syncAddr = 34003 built_packet.dmxStartCode = 8 read_packet = DataPacket.make_data_packet(built_packet.getBytes()) assert read_packet.cid == tuple(range(16)) assert read_packet.sourceName == '2nd Test Name' assert read_packet.universe == 31425 assert read_packet.dmxData == ((200, ) + tuple(range(255, 0, -1)) + tuple(range(255)) + (0, )) assert read_packet.priority == 12 assert read_packet.sequence == 45 assert read_packet.option_StreamTerminated is True assert read_packet.option_PreviewData is True assert read_packet.option_ForceSync is True assert read_packet.syncAddr == 34003 assert read_packet.dmxStartCode == 8
def test_universe_stream_terminated(): _, listener, socket = get_handler() assert listener.on_availability_change_changed is None assert listener.on_availability_change_universe is None packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) socket.call_on_data(bytes(packet.getBytes()), 0) assert listener.on_availability_change_changed == 'available' assert listener.on_availability_change_universe == 1 packet.sequence_increase() packet.option_StreamTerminated = True socket.call_on_data(bytes(packet.getBytes()), 0) assert listener.on_availability_change_changed == 'timeout' assert listener.on_availability_change_universe == 1
def test_possible_universes(): handler, _, socket = get_handler() assert handler.get_possible_universes() == [] packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) # add universe 1 socket.call_on_data(bytes(packet.getBytes()), 0) assert handler.get_possible_universes() == [1] # add universe 2 packet.universe = 2 socket.call_on_data(bytes(packet.getBytes()), 0) assert handler.get_possible_universes() == [1, 2] # remove universe 2 packet.option_StreamTerminated = True socket.call_on_data(bytes(packet.getBytes()), 0) assert handler.get_possible_universes() == [1]
def test_possible_universes(): receiver, socket = get_receiver() assert receiver.get_possible_universes() == () packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) socket.call_on_data(bytes(packet.getBytes()), 0) assert receiver.get_possible_universes() == tuple([1])
def test_invalid_priority(): # send a lower priority on a second packet _, listener, socket = get_handler() assert listener.on_dmx_data_change_packet is None packet1 = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16)), priority=100) socket.call_on_data(bytes(packet1.getBytes()), 0) assert listener.on_dmx_data_change_packet.__dict__ == packet1.__dict__ packet2 = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16)), priority=99) socket.call_on_data(bytes(packet2.getBytes()), 1) # second packet does not override the previous one assert listener.on_dmx_data_change_packet.__dict__ == packet1.__dict__
def case_goes_through(sequence_a: int, sequence_b: int): _, listener, socket = get_handler() assert listener.on_dmx_data_change_packet is None packet1 = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16)), sequence=sequence_a) socket.call_on_data(bytes(packet1.getBytes()), 0) assert listener.on_dmx_data_change_packet.__dict__ == packet1.__dict__ packet2 = DataPacket( cid=tuple(range(0, 16)), sourceName='Test', universe=1, # change DMX data to simulate data from another source dmxData=tuple(range(1, 17)), sequence=sequence_b) socket.call_on_data(bytes(packet2.getBytes()), 1) assert listener.on_dmx_data_change_packet.__dict__ == packet2.__dict__
def test_first_packet(): _, listener, socket = get_handler() assert listener.on_availability_change_changed is None assert listener.on_availability_change_universe is None assert listener.on_dmx_data_change_packet is None packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) socket.call_on_data(bytes(packet.getBytes()), 0) assert listener.on_availability_change_changed == 'available' assert listener.on_availability_change_universe == 1 assert listener.on_dmx_data_change_packet.__dict__ == packet.__dict__
def test_remove_listener(): receiver, socket = get_receiver() packetSend = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) called = 0 def callback_packet(packet): assert packetSend.__dict__ == packet.__dict__ nonlocal called called += 1 # register listener multiple times receiver.register_listener('universe', callback_packet, universe=packetSend.universe) receiver.register_listener('universe', callback_packet, universe=packetSend.universe) socket.call_on_data(bytes(packetSend.getBytes()), 0) assert called == 2 # change DMX data to trigger a change packetSend.dmxData = tuple(range(16, 32)) packetSend.sequence_increase() receiver.remove_listener(callback_packet) # removing a listener does not exist, should do nothing receiver.remove_listener(None) socket.call_on_data(bytes(packetSend.getBytes()), 0) assert called == 2
def test_byte_construction_and_deconstruction(): built_packet = DataPacket(cid=(16, 1, 15, 2, 14, 3, 13, 4, 12, 5, 11, 6, 10, 7, 9, 8), sourceName='Test Name', universe=62000, dmxData=tuple(x % 256 for x in range(0, 512)), priority=195, sequence=34, streamTerminated=True, previewData=True, forceSync=True, sync_universe=12000, dmxStartCode=12) read_packet = DataPacket.make_data_packet(built_packet.getBytes()) assert built_packet.dmxData == read_packet.dmxData assert built_packet == read_packet
def test_listen_on_dmx_data_change(): receiver, socket = get_receiver() packetSend = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) called = False @receiver.listen_on('universe', universe=packetSend.universe) def callback_packet(packet): assert packetSend.__dict__ == packet.__dict__ nonlocal called called = True socket.call_on_data(bytes(packetSend.getBytes()), 0) assert called
def test_listen_on_availability_change(): receiver, socket = get_receiver() called = False @receiver.listen_on('availability') def callback_available(universe, changed): assert changed == 'available' assert universe == 1 nonlocal called called = True packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) socket.call_on_data(bytes(packet.getBytes()), 0) assert called
def test_universe_timeout(): _, listener, socket = get_handler() assert listener.on_availability_change_changed is None assert listener.on_availability_change_universe is None packet = DataPacket(cid=tuple(range(0, 16)), sourceName='Test', universe=1, dmxData=tuple(range(0, 16))) socket.call_on_data(bytes(packet.getBytes()), 0) socket.call_on_periodic_callback(0) assert listener.on_availability_change_changed == 'available' assert listener.on_availability_change_universe == 1 # wait the specified amount of time and check, that a timeout was triggered # add 10ms of grace time socket.call_on_periodic_callback((E131_NETWORK_DATA_LOSS_TIMEOUT_ms / 1000) + 0.01) assert listener.on_availability_change_changed == 'timeout' assert listener.on_availability_change_universe == 1