Example #1
0
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
Example #2
0
def test_dmx_data():
    packet = DataPacket(cid=tuple(range(0, 16)), sourceName="", universe=1)
    # test valid lengths
    for i in range(0, 512):
        data = tuple(x % 256 for x in range(0, i))
        # test property setter
        packet.dmxData = data
        assert len(packet.dmxData) == 512
        assert packet.length == 638
        # test constructor for the same parameter
        packet2 = DataPacket(tuple(range(0, 16)),
                             sourceName="",
                             universe=1,
                             dmxData=data)
        assert len(packet2.dmxData) == 512
        assert packet2.length == 638

    def execute_universes_expect(data: tuple):
        with pytest.raises(ValueError):
            packet.dmxData = data
        with pytest.raises(ValueError):
            DataPacket(tuple(range(0, 16)),
                       sourceName="",
                       universe=1,
                       dmxData=data)

    # test for non-int and out of range values values in tuple
    execute_universes_expect(tuple('string'))
    execute_universes_expect(tuple(range(255, 257)))

    # test for tuple-length > 512
    execute_universes_expect(tuple(range(0, 513)))
Example #3
0
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
Example #4
0
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