@@ -73,6 +73,8 @@ impl From<&[u8; RCL_ACTION_UUID_SIZE]> for GoalUuid {
7373}
7474
7575/// The response returned by an [`ActionServer`]'s cancel callback when a goal is requested to be cancelled.
76+ #[ cfg_attr( feature = "serde" , derive( Deserialize , Serialize ) ) ]
77+ #[ cfg_attr( feature = "serde" , serde( rename = "snake_case" ) ) ]
7678#[ repr( i8 ) ]
7779#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord ) ]
7880pub enum CancelResponseCode {
@@ -118,6 +120,7 @@ impl From<i8> for CancelResponseCode {
118120///
119121/// When a cancellation request might cancel multiple goals, [`MultiCancelResponse`]
120122/// will be used.
123+ #[ cfg_attr( feature = "serde" , derive( Deserialize , Serialize ) ) ]
121124#[ derive( Debug , Clone , PartialEq , PartialOrd ) ]
122125pub struct CancelResponse {
123126 /// What kind of response was given.
@@ -154,6 +157,7 @@ impl MultiCancelResponse {
154157
155158/// Values defined by `action_msgs/msg/GoalStatus`
156159#[ cfg_attr( feature = "serde" , derive( Deserialize , Serialize ) ) ]
160+ #[ cfg_attr( feature = "serde" , serde( rename = "snake_case" ) ) ]
157161#[ repr( i8 ) ]
158162#[ derive( Debug , Clone , Copy , Hash , PartialEq , Eq , PartialOrd , Ord ) ]
159163pub enum GoalStatusCode {
@@ -240,15 +244,17 @@ mod tests {
240244 use tokio:: sync:: mpsc:: unbounded_channel;
241245
242246 #[ test]
243- fn test_action_success ( ) {
247+ fn test_action_success_streaming ( ) {
244248 let mut executor = Context :: default ( ) . create_basic_executor ( ) ;
245249
246250 let node = executor
247251 . create_node ( & format ! ( "test_action_success_{}" , line!( ) ) )
248252 . unwrap ( ) ;
249253 let action_name = format ! ( "test_action_success_{}_action" , line!( ) ) ;
250254 let _action_server = node
251- . create_action_server ( & action_name, fibonacci_action)
255+ . create_action_server ( & action_name, |handle| {
256+ fibonacci_action ( handle, Duration :: from_micros ( 10 ) )
257+ } )
252258 . unwrap ( ) ;
253259
254260 let client = node
@@ -302,7 +308,41 @@ mod tests {
302308 executor. spin ( SpinOptions :: default ( ) . until_promise_resolved ( promise) ) ;
303309 }
304310
305- async fn fibonacci_action ( handle : RequestedGoal < Fibonacci > ) -> TerminatedGoal {
311+ #[ test]
312+ fn test_action_cancel ( ) {
313+ let mut executor = Context :: default ( ) . create_basic_executor ( ) ;
314+
315+ let node = executor
316+ . create_node ( & format ! ( "test_action_cancel_{}" , line!( ) ) )
317+ . unwrap ( ) ;
318+ let action_name = format ! ( "test_action_cancel_{}_slow_action" , line!( ) ) ;
319+ let _action_server = node
320+ . create_action_server ( & action_name, |handle| {
321+ fibonacci_action ( handle, Duration :: from_secs ( 1 ) )
322+ } )
323+ . unwrap ( ) ;
324+
325+ let client = node
326+ . create_action_client :: < Fibonacci > ( & action_name)
327+ . unwrap ( ) ;
328+
329+ let request = client. request_goal ( Fibonacci_Goal { order : 10 } ) ;
330+
331+ let promise = executor. commands ( ) . run ( async move {
332+ let goal_client = request. await . unwrap ( ) ;
333+ let cancellation = goal_client. cancellation . cancel ( ) . await ;
334+ assert ! ( cancellation. is_accepted( ) ) ;
335+ let ( status, _) = goal_client. result . await ;
336+ assert_eq ! ( status, GoalStatusCode :: Cancelled ) ;
337+ } ) ;
338+
339+ executor. spin ( SpinOptions :: default ( ) . until_promise_resolved ( promise) ) ;
340+ }
341+
342+ async fn fibonacci_action (
343+ handle : RequestedGoal < Fibonacci > ,
344+ period : Duration ,
345+ ) -> TerminatedGoal {
306346 let goal_order = handle. goal ( ) . order ;
307347 if goal_order < 0 {
308348 return handle. reject ( ) ;
@@ -331,7 +371,7 @@ mod tests {
331371 let next = previous + current;
332372 previous = current;
333373 current = next;
334- std:: thread:: sleep ( Duration :: from_micros ( 10 ) ) ;
374+ std:: thread:: sleep ( period ) ;
335375 }
336376 } ) ;
337377
0 commit comments