Skip to content

Commit 71a4f89

Browse files
committed
Better documents test #110.
1 parent fad5756 commit 71a4f89

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

tests/sitl/test_110.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import time
44
import sys
55
import os
6+
from testlib import assert_equals
67

78
def test_110(local_connect):
89
api = local_connect()
@@ -15,35 +16,41 @@ def test_110(local_connect):
1516
time.sleep(3)
1617

1718
# Define example callback for mode
18-
allow_callback = True
19-
bad_call = [False]
2019
def armed_callback(attribute):
21-
if not allow_callback:
22-
bad_call[0] = True
20+
armed_callback.called += 1
21+
armed_callback.called = 0
2322

24-
# Only one of the same observer fn should be added.
23+
# When the same (event, callback) pair is passed to add_attribute_observer,
24+
# only one instance of the observer callback should be added.
25+
v.add_attribute_observer('armed', armed_callback)
2526
v.add_attribute_observer('armed', armed_callback)
2627
v.add_attribute_observer('armed', armed_callback)
2728
v.add_attribute_observer('armed', armed_callback)
2829
v.add_attribute_observer('armed', armed_callback)
29-
allow_callback = True
3030

3131
# Disarm and see update.
3232
v.armed = False
3333
v.flush()
34-
34+
# Wait for ACK.
3535
time.sleep(3)
3636

37-
# Rmove (all) observers.
37+
# Ensure the callback was called.
38+
assert armed_callback.called > 0, "Callback should have been called."
39+
40+
# Rmove all observers. The first call should remove all listeners
41+
# we've added; the second call should be ignored and not throw.
42+
# NOTE: We test if armed_callback were treating adding each additional callback
43+
# and remove_attribute_observer were removing them one at a time; in this
44+
# case, there would be three callbacks still attached.
3845
v.remove_attribute_observer('armed', armed_callback)
3946
v.remove_attribute_observer('armed', armed_callback)
40-
allow_callback = False
47+
callcount = armed_callback.called
4148

4249
# Re-arm and see update.
4350
v.armed = True
4451
v.flush()
45-
52+
# Wait for ack
4653
time.sleep(3)
4754

48-
# Make sure no bad calls happened.
49-
assert not bad_call[0], "Callback should not have been called once removed."
55+
# Ensure the callback was called zero times.
56+
assert_equals(armed_callback.called, callcount, "Callback should not have been called once removed.")

0 commit comments

Comments
 (0)