@@ -989,7 +989,7 @@ where
989989 let update_id = options
990990 . update_id
991991 . unwrap_or_else ( || Uuid :: new_v4 ( ) . to_string ( ) ) ;
992- let mut request = build_update_workflow_request (
992+ let request = build_update_workflow_request (
993993 client. namespace ( ) ,
994994 client. identity ( ) ,
995995 workflow_id. clone ( ) ,
@@ -998,14 +998,23 @@ where
998998 update_name,
999999 options. header ,
10001000 payloads,
1001- )
1002- . into_request ( ) ;
1003- options. rpc_options . apply_to ( & mut request) ;
1004- let response =
1005- WorkflowService :: update_workflow_execution ( & mut client, request)
1006- . await
1007- . map_err ( WorkflowUpdateError :: from_status) ?
1008- . into_inner ( ) ;
1001+ ) ;
1002+ let response = loop {
1003+ let mut rpc_request = request. clone ( ) . into_request ( ) ;
1004+ options. rpc_options . apply_to ( & mut rpc_request) ;
1005+ let response = WorkflowService :: update_workflow_execution (
1006+ & mut client,
1007+ rpc_request,
1008+ )
1009+ . await
1010+ . map_err ( WorkflowUpdateError :: from_status) ?
1011+ . into_inner ( ) ;
1012+ if response. stage
1013+ >= UpdateWorkflowExecutionLifecycleStage :: Accepted as i32
1014+ {
1015+ break response;
1016+ }
1017+ } ;
10091018 let run_id = response
10101019 . update_ref
10111020 . as_ref ( )
@@ -1477,15 +1486,21 @@ mod tests {
14771486 use temporalio_common:: {
14781487 data_converters:: DefaultFailureConverter ,
14791488 protos:: temporal:: api:: {
1480- common:: v1:: { Memo , SearchAttributes } ,
1481- enums:: v1:: WorkflowExecutionStatus as ProtoWorkflowExecutionStatus ,
1489+ common:: v1:: { Memo , SearchAttributes , WorkflowExecution } ,
1490+ enums:: v1:: {
1491+ UpdateWorkflowExecutionLifecycleStage as UpdateStage ,
1492+ WorkflowExecutionStatus as ProtoWorkflowExecutionStatus ,
1493+ } ,
14821494 history:: v1:: WorkflowExecutionStartedEventAttributes ,
14831495 sdk:: v1:: UserMetadata ,
1496+ update:: v1:: UpdateRef ,
14841497 workflow:: v1:: WorkflowExecutionConfig ,
1485- workflowservice:: v1:: GetWorkflowExecutionHistoryResponse ,
1498+ workflowservice:: v1:: {
1499+ GetWorkflowExecutionHistoryResponse , UpdateWorkflowExecutionResponse ,
1500+ } ,
14861501 } ,
14871502 } ;
1488- use tonic:: { Request , Response } ;
1503+ use tonic:: { Request , Response , Status } ;
14891504
14901505 #[ tokio:: test]
14911506 async fn workflow_history_workflow_id_roundtrips ( ) {
@@ -1855,4 +1870,125 @@ mod tests {
18551870 "Encoding error: workflow history_length must be non-negative, got -1"
18561871 ) ;
18571872 }
1873+
1874+ #[ derive( Default ) ]
1875+ struct MockUpdateState {
1876+ responses : VecDeque < Result < UpdateWorkflowExecutionResponse , Status > > ,
1877+ requests : Vec < UpdateWorkflowExecutionRequest > ,
1878+ }
1879+
1880+ #[ derive( Clone ) ]
1881+ struct MockUpdateClient ( Arc < Mutex < MockUpdateState > > ) ;
1882+
1883+ impl NamespacedClient for MockUpdateClient {
1884+ fn namespace ( & self ) -> String {
1885+ "ns" . into ( )
1886+ }
1887+ fn identity ( & self ) -> String {
1888+ "identity" . into ( )
1889+ }
1890+ }
1891+
1892+ impl WorkflowService for MockUpdateClient {
1893+ fn update_workflow_execution (
1894+ & mut self ,
1895+ request : Request < UpdateWorkflowExecutionRequest > ,
1896+ ) -> BoxFuture < ' _ , Result < Response < UpdateWorkflowExecutionResponse > , Status > > {
1897+ let mut state = self . 0 . lock ( ) . unwrap ( ) ;
1898+ state. requests . push ( request. into_inner ( ) ) ;
1899+ let response = state. responses . pop_front ( ) . expect ( "unexpected submission" ) ;
1900+ Box :: pin ( async { response. map ( Response :: new) } )
1901+ }
1902+ }
1903+
1904+ #[ rstest:: rstest]
1905+ #[ case:: retries_until_accepted(
1906+ vec![ UpdateStage :: Unspecified , UpdateStage :: Admitted , UpdateStage :: Admitted , UpdateStage :: Accepted ] ,
1907+ None
1908+ ) ]
1909+ #[ case:: already_accepted( vec![ UpdateStage :: Accepted ] , None ) ]
1910+ #[ case:: already_completed( vec![ UpdateStage :: Completed ] , None ) ]
1911+ #[ case:: rpc_error( vec![ UpdateStage :: Admitted ] , Some ( Status :: unavailable( "transport failure" ) ) ) ]
1912+ #[ tokio:: test]
1913+ async fn update_waits_for_acceptance (
1914+ #[ case] stages : Vec < UpdateStage > ,
1915+ #[ case] error : Option < Status > ,
1916+ ) {
1917+ use std:: time:: Duration ;
1918+
1919+ let mut responses: VecDeque < _ > = stages
1920+ . iter ( )
1921+ . map ( |stage| {
1922+ Ok ( UpdateWorkflowExecutionResponse {
1923+ stage : * stage as i32 ,
1924+ update_ref : Some ( UpdateRef {
1925+ workflow_execution : Some ( WorkflowExecution {
1926+ workflow_id : "wf" . into ( ) ,
1927+ run_id : "run" . into ( ) ,
1928+ } ) ,
1929+ ..Default :: default ( )
1930+ } ) ,
1931+ ..Default :: default ( )
1932+ } )
1933+ } )
1934+ . collect ( ) ;
1935+ // Only successful responses below Accepted should trigger another submission.
1936+ if let Some ( error) = error. clone ( ) {
1937+ responses. push_back ( Err ( error) ) ;
1938+ }
1939+ let expected_requests = responses. len ( ) ;
1940+ let state = Arc :: new ( Mutex :: new ( MockUpdateState {
1941+ responses,
1942+ ..Default :: default ( )
1943+ } ) ) ;
1944+ let handle = WorkflowHandle :: < _ , UntypedWorkflow > :: new (
1945+ MockUpdateClient ( state. clone ( ) ) ,
1946+ WorkflowExecutionInfo :: builder ( )
1947+ . namespace ( "ns" )
1948+ . workflow_id ( "wf" )
1949+ . build ( ) ,
1950+ ) ;
1951+ let result = handle
1952+ . start_update (
1953+ UntypedUpdate :: new ( "handler" ) ,
1954+ RawValue :: new ( vec ! [ Payload {
1955+ data: vec![ 1 , 2 , 3 ] ,
1956+ ..Default :: default ( )
1957+ } ] ) ,
1958+ WorkflowStartUpdateOptions :: builder ( )
1959+ . rpc_options (
1960+ RpcOptions :: builder ( )
1961+ . timeout ( Duration :: from_secs ( 30 ) )
1962+ . build ( ) ,
1963+ )
1964+ . build ( ) ,
1965+ )
1966+ . await ;
1967+ let state = state. lock ( ) . unwrap ( ) ;
1968+ assert_eq ! ( state. requests. len( ) , expected_requests) ;
1969+ let first = & state. requests [ 0 ] ;
1970+ // A retry must remain the same logical update
1971+ assert ! ( state. requests. iter( ) . all( |request| request == first) ) ;
1972+ assert ! ( first. workflow_execution. as_ref( ) . unwrap( ) . run_id. is_empty( ) ) ;
1973+ let update_id = & first
1974+ . request
1975+ . as_ref ( )
1976+ . unwrap ( )
1977+ . meta
1978+ . as_ref ( )
1979+ . unwrap ( )
1980+ . update_id ;
1981+ assert ! ( !update_id. is_empty( ) ) ;
1982+ match ( error, result) {
1983+ ( None , Ok ( handle) ) => {
1984+ assert_eq ! ( handle. id( ) , update_id) ;
1985+ assert_eq ! ( handle. workflow_run_id( ) , Some ( "run" ) ) ;
1986+ }
1987+ ( Some ( expected) , Err ( WorkflowUpdateError :: Rpc ( actual) ) ) => {
1988+ assert_eq ! ( actual. code( ) , expected. code( ) ) ;
1989+ assert_eq ! ( actual. message( ) , expected. message( ) ) ;
1990+ }
1991+ _ => panic ! ( "unexpected update result" ) ,
1992+ }
1993+ }
18581994}
0 commit comments