Skip to content

Commit c5cad08

Browse files
Tomoya FujitaTomoya Fujita
authored andcommitted
give some time for the discovery for test_on_new_message_callback.
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
1 parent f88e081 commit c5cad08

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

rclpy/test/test_subscription.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,31 @@ def test_on_new_message_callback(test_node) -> None:
190190
qos_profile=10,
191191
callback=cb)
192192
pub = test_node.create_publisher(Empty, topic_name, 10)
193+
194+
# Wait for publisher and subscriber to discover each other
195+
max_seconds_to_wait = 5
196+
end_time = time.time() + max_seconds_to_wait
197+
while sub.get_publisher_count() != 1:
198+
time.sleep(0.1)
199+
assert time.time() <= end_time # timeout waiting for pub/sub to discover each other
200+
193201
sub.handle.set_on_new_message_callback(cb)
194202
cb.assert_not_called()
195203
pub.publish(Empty())
204+
205+
# Wait for the callback to be invoked
206+
end_time = time.time() + max_seconds_to_wait
207+
while cb.call_count == 0 and time.time() <= end_time:
208+
time.sleep(0.1)
209+
196210
cb.assert_called_once_with(1)
197211
sub.handle.clear_on_new_message_callback()
212+
cb.reset_mock()
198213
pub.publish(Empty())
199-
cb.assert_called_once()
214+
215+
# Wait a bit to ensure the message would have been received if callback was still set
216+
time.sleep(1.0)
217+
cb.assert_not_called()
200218

201219

202220
def test_subscription_set_content_filter(test_node) -> None:

0 commit comments

Comments
 (0)