Skip to content

Commit

Permalink
added new function for removing listener by universe
Browse files Browse the repository at this point in the history
  • Loading branch information
Hundemeier committed Jul 12, 2021
1 parent 564fb44 commit ebcae30
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions sacn/receiver.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ def remove_listener(self, func: callable) -> None:
except ValueError:
break

def remove_listener_from_universe(self, universe: int) -> None:
"""
Removes all listeners from the given universe. This does only have effect on the 'universe' listening trigger.
If no function was registered, nothing happens.
:param universe: the universe to clear
"""
self._callbacks.pop(universe, None)

def join_multicast(self, universe: int) -> None:
"""
Joins the multicast address that is used for the given universe. Note: If you are on Windows you must have given
Expand Down
54 changes: 54 additions & 0 deletions sacn/receiver_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,64 @@ def callback_packet(packet):

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_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_invalid_listener():
receiver, socket = get_receiver()

Expand Down

0 comments on commit ebcae30

Please sign in to comment.