Skip to content

Commit 740d7ec

Browse files
authored
Improve stability of the tests (#352)
* Improve test stability. --------- Signed-off-by: Jelmer de Wolde <jelmer.de.wolde@alliander.com>
1 parent 950f3ee commit 740d7ec

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

ros2_ws/src/rcdt_franka/src/gripper.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ void Gripper::open(
5959
auto goal = Move::Goal();
6060
goal.width = 0.08;
6161
goal.speed = 0.03;
62+
63+
auto server_ready =
64+
client_move->wait_for_action_server(std::chrono::seconds(timeout));
65+
if (!server_ready) {
66+
RCLCPP_ERROR(this->get_logger(), "Action server not available. Timeout");
67+
return;
68+
}
69+
6270
auto future_goal_handle = client_move->async_send_goal(goal);
6371
if (future_goal_handle.wait_for(std::chrono::seconds(timeout)) ==
6472
std::future_status::timeout) {
@@ -93,6 +101,14 @@ void Gripper::close(
93101
goal.width = 0.0;
94102
goal.force = 100.0;
95103
goal.speed = 0.03;
104+
105+
auto server_ready =
106+
client_move->wait_for_action_server(std::chrono::seconds(timeout));
107+
if (!server_ready) {
108+
RCLCPP_ERROR(this->get_logger(), "Action server not available. Timeout");
109+
return;
110+
}
111+
96112
auto future_goal_handle = client_grasp->async_send_goal(goal);
97113
if (future_goal_handle.wait_for(std::chrono::seconds(timeout)) ==
98114
std::future_status::timeout) {

ros2_ws/src/rcdt_test/rcdt_test/combined/end_to_end/test_nav2.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def test_goal_pose(
109109
timeout (int): The timeout in seconds.
110110
111111
Raises:
112-
AssertionError: When a timeout occurs.
112+
TimeoutError: When a timeout occurs.
113113
"""
114114
# 1) Obtain current pose in map frame:
115115
tf_buffer = Buffer()
@@ -124,7 +124,7 @@ def test_goal_pose(
124124
"map", f"{namespace_vehicle}/base_link", Time()
125125
)
126126
if time.time() - start_time > timeout:
127-
raise AssertionError("Timeout while waiting for current pose.")
127+
raise TimeoutError()
128128

129129
# 2) Publish goal pose 1 meter in front of current position:
130130
goal_pose = PoseStamped()
@@ -151,8 +151,8 @@ def test_goal_pose(
151151
current_pose.transform.translation.x - goal_pose.pose.position.x
152152
)
153153
if time.time() - start_time > timeout:
154-
raise AssertionError(
155-
f"Timeout. Distance is {distance} while tolerance is {navigation_distance_tolerance}."
154+
raise TimeoutError(
155+
f"Distance is {distance} while tolerance is {navigation_distance_tolerance}."
156156
)
157157

158158
# 4) Stop navigation, since the goal can be reached before the navigation is finished due to tolerance:
@@ -172,7 +172,7 @@ def test_gps_navigation(
172172
timeout (int): The timeout in seconds.
173173
174174
Raises:
175-
AssertionError: When a timeout occurs.
175+
TimeoutError: When a timeout occurs.
176176
"""
177177
# 1) Obtain current GPS location:
178178
current_nav_sat = NavSatFix()
@@ -189,7 +189,7 @@ def callback(msg: NavSatFix) -> None:
189189
while current_nav_sat == NavSatFix():
190190
rclpy.spin_once(test_node, timeout_sec=0)
191191
if time.time() - start_time > timeout:
192-
raise AssertionError("Timeout while waiting for current GPS location.")
192+
raise TimeoutError("Timeout while waiting for current GPS location.")
193193

194194
# 2) Publish goal GPS location 1e-5 degrees north of current location:
195195
goal_nav_sat = copy.deepcopy(current_nav_sat)
@@ -211,8 +211,8 @@ def callback(msg: NavSatFix) -> None:
211211
rclpy.spin_once(test_node, timeout_sec=0)
212212
distance = abs(current_nav_sat.latitude - goal_nav_sat.latitude)
213213
if time.time() - start_time > timeout:
214-
raise AssertionError(
215-
f"Timeout. Distance is {distance} while tolerance is {navigation_degree_tolerance}."
214+
raise TimeoutError(
215+
f"Distance is {distance} while tolerance is {navigation_degree_tolerance}."
216216
)
217217

218218
# 4) Stop navigation, since the goal can be reached before the navigation is finished due to tolerance:

ros2_ws/src/rcdt_test/rcdt_test/franka/integration/test_franka_moveit.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,9 @@ def test_move_to_drop_configuration(
7575
joint_movement_tolerance (float): The tolerance for joint movement.
7676
timeout (int): The timeout in seconds before stopping the test.
7777
"""
78-
assert (
79-
call_move_to_configuration_service(
80-
test_node, namespace, "drop", timeout=timeout
81-
)
82-
is True
83-
)
78+
assert call_move_to_configuration_service(
79+
test_node, namespace, "drop", timeout=timeout
80+
), "Failed to call move_to_configuration service."
8481
drop_values = [-1.57079632679, -0.65, 0, -2.4, 0, 1.75, 0.78539816339]
8582
for i in range(7):
8683
joint_value = get_joint_position(

ros2_ws/src/rcdt_utilities/rcdt_utilities/test_utils.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,14 @@ def wait_for_subscriber(pub: Publisher, timeout: int) -> None:
7474
pub (Publisher): The publisher to wait for.
7575
timeout (int): The maximum time to wait for a subscriber in seconds.
7676
77+
Raises:
78+
TimeoutError: If no subscriber is found within the timeout period.
79+
7780
"""
78-
start_time = time.monotonic()
79-
while pub.get_subscription_count() == 0 and time.monotonic() - start_time < timeout:
81+
start_time = time.time()
82+
while pub.get_subscription_count() == 0:
83+
if time.time() > (start_time + timeout):
84+
raise TimeoutError()
8085
time.sleep(0.1)
8186

8287

@@ -93,7 +98,7 @@ def get_joint_position(namespace: str, joint: str, timeout: int) -> float:
9398
"""
9499
topic_list = [(f"{namespace}/joint_states", JointState)]
95100
wait_for_topics = WaitForTopics(topic_list, timeout=timeout)
96-
assert wait_for_topics.wait()
101+
assert wait_for_topics.wait(), "Did not receive JointState."
97102
msg: JointState = wait_for_topics.received_messages(f"{namespace}/joint_states")[0]
98103
idx = msg.name.index(joint)
99104
position = msg.position[idx]
@@ -277,7 +282,7 @@ def assert_joy_topic_switch(
277282
state_topic (str): Topic to listen for state updates from joy_topic_manager.
278283
279284
Raises:
280-
AssertionError: When a timeout occurs.
285+
TimeoutError: When a timeout occurs.
281286
"""
282287
logger.info("Starting to assert joy topic switch")
283288
qos = QoSProfile(
@@ -316,9 +321,7 @@ def callback_function(msg: String) -> None:
316321
while result.get("state") != expected_topic:
317322
rclpy.spin_once(node, timeout_sec=0)
318323
if time.time() > (start_time + timeout):
319-
raise AssertionError(
320-
f"Timeout. Did not receive {expected_topic} on {state_topic}."
321-
)
324+
raise TimeoutError(f"Did not receive {expected_topic} on {state_topic}.")
322325

323326
assert result.get("state") == expected_topic, (
324327
f"Expected state '{expected_topic}', but got '{result.get('state')}'"

0 commit comments

Comments
 (0)