11use crate :: integ_tests:: mk_nexus_endpoint;
22use anyhow:: bail;
33use assert_matches:: assert_matches;
4- use std:: time:: Duration ;
4+ use std:: {
5+ sync:: {
6+ Arc ,
7+ atomic:: { AtomicBool , Ordering } ,
8+ } ,
9+ time:: Duration ,
10+ } ;
511use temporal_client:: { WfClientExt , WorkflowClientTrait , WorkflowOptions } ;
612use temporal_sdk:: { CancellableFuture , NexusOperationOptions , WfContext , WfExitValue } ;
713use temporal_sdk_core_api:: errors:: PollError ;
@@ -591,20 +597,22 @@ async fn nexus_cancellation_types(
591597 }
592598 } ) ;
593599
594- let ( cancellation_wait_tx, cancellation_wait_rx) = watch:: channel ( false ) ;
600+ let cancellation_wait_happened = Arc :: new ( AtomicBool :: new ( false ) ) ;
601+ let cancellation_wait_happened_clone = cancellation_wait_happened. clone ( ) ;
595602 let ( cancellation_tx, mut cancellation_rx) = watch:: channel ( false ) ;
596603 let ( handler_exited_tx, mut handler_exited_rx) = watch:: channel ( false ) ;
597604 worker. register_wf ( "async_completer" . to_owned ( ) , move |ctx : WfContext | {
598605 let cancellation_tx = cancellation_tx. clone ( ) ;
599- let mut cancellation_wait_rx = cancellation_wait_rx . clone ( ) ;
606+ let cancellation_wait_happened = cancellation_wait_happened_clone . clone ( ) ;
600607 let handler_exited_tx = handler_exited_tx. clone ( ) ;
601608 async move {
602609 // Wait for cancellation
603610 ctx. cancelled ( ) . await ;
604611 cancellation_tx. send ( true ) . unwrap ( ) ;
605612
606613 if cancellation_type == NexusOperationCancellationType :: WaitCancellationCompleted {
607- cancellation_wait_rx. changed ( ) . await . unwrap ( ) ;
614+ ctx. wait_condition ( || cancellation_wait_happened. load ( Ordering :: Relaxed ) )
615+ . await ;
608616 } else if cancellation_type == NexusOperationCancellationType :: WaitCancellationRequested
609617 {
610618 // For WAIT_REQUESTED, wait until the caller nexus op future has been resolved. This
@@ -624,13 +632,13 @@ async fn nexus_cancellation_types(
624632 let wf_handle = starter. start_with_worker ( wf_name, & mut worker) . await ;
625633 let client = starter. get_client ( ) . await . get_client ( ) . clone ( ) ;
626634 let ( handler_wf_id_tx, mut handler_wf_id_rx) = tokio:: sync:: oneshot:: channel ( ) ;
635+ let completer_id = & format ! ( "completer-{}" , rand_6_chars( ) ) ;
627636 let nexus_task_handle = async {
628637 let nt = core_worker. poll_nexus_task ( ) . await . unwrap ( ) . unwrap_task ( ) ;
629638 let start_req = assert_matches ! (
630639 nt. request. unwrap( ) . variant. unwrap( ) ,
631640 request:: Variant :: StartOperation ( sr) => sr
632641 ) ;
633- let completer_id = format ! ( "completer-{}" , rand_6_chars( ) ) ;
634642 let _ = handler_wf_id_tx. send ( completer_id. clone ( ) ) ;
635643 let links = start_req
636644 . links
@@ -708,7 +716,12 @@ async fn nexus_cancellation_types(
708716 request:: Variant :: CancelOperation ( _)
709717 ) ;
710718 client
711- . cancel_workflow_execution ( completer_id, None , "nexus cancel" . to_string ( ) , None )
719+ . cancel_workflow_execution (
720+ completer_id. to_string ( ) ,
721+ None ,
722+ "nexus cancel" . to_string ( ) ,
723+ None ,
724+ )
712725 . await
713726 . unwrap ( ) ;
714727 core_worker
@@ -732,7 +745,18 @@ async fn nexus_cancellation_types(
732745 if cancellation_type == NexusOperationCancellationType :: WaitCancellationCompleted {
733746 assert ! ( !* caller_op_future_rx. borrow( ) ) ;
734747
735- cancellation_wait_tx. send ( true ) . unwrap ( ) ;
748+ cancellation_wait_happened. store ( true , Ordering :: Relaxed ) ;
749+ // Send a signal just to wake up the workflow so it'll check the condition
750+ client
751+ . signal_workflow_execution (
752+ completer_id. to_string ( ) ,
753+ "" . to_string ( ) ,
754+ "wakeupdude" . to_string ( ) ,
755+ None ,
756+ None ,
757+ )
758+ . await
759+ . unwrap ( ) ;
736760 wf_handle
737761 . get_workflow_result ( Default :: default ( ) )
738762 . await
0 commit comments