1414from iwf .tests .worker_server import registry
1515from iwf .workflow import ObjectWorkflow
1616from iwf .workflow_context import WorkflowContext
17+ from iwf .workflow_options import WorkflowOptions
1718from iwf .workflow_state import T , WorkflowState
1819
1920sa_keyword_key = "CustomKeywordField"
3233sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
3334sa_keyword_array : list [str ] = ["keyword-1" , "keyword-2" ]
3435
36+ final_sa_keyword : str = "final_keyword"
37+ final_sa_text = None
38+ final_sa_int : int = 567
39+ final_sa_bool : bool = False
40+ final_sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
41+ final_sa_keyword_array : list [str ] = ["final_keyword-1" , "final_keyword-2" ]
3542
36- class SearchAttributeState (WorkflowState [None ]):
43+
44+ class SearchAttributeState1 (WorkflowState [None ]):
3745 def wait_until (
3846 self ,
3947 ctx : WorkflowContext ,
@@ -59,12 +67,42 @@ def execute(
5967 )
6068 persistence .set_search_attribute_int64 (sa_int_key , sa_int )
6169 persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
70+ return StateDecision .single_next_state (SearchAttributeState2 )
71+
72+
73+ class SearchAttributeState2 (WorkflowState [None ]):
74+ def wait_until (
75+ self ,
76+ ctx : WorkflowContext ,
77+ input : T ,
78+ persistence : Persistence ,
79+ communication : Communication ,
80+ ) -> CommandRequest :
81+ return CommandRequest .empty ()
82+
83+ def execute (
84+ self ,
85+ ctx : WorkflowContext ,
86+ input : T ,
87+ command_results : CommandResults ,
88+ persistence : Persistence ,
89+ communication : Communication ,
90+ ) -> StateDecision :
91+ persistence .set_search_attribute_keyword (sa_keyword_key , final_sa_keyword )
92+ persistence .set_search_attribute_boolean (sa_bool_key , final_sa_bool )
93+ persistence .set_search_attribute_keyword_array (
94+ sa_keyword_array_key , final_sa_keyword_array
95+ )
96+ persistence .set_search_attribute_int64 (sa_int_key , final_sa_int )
97+ persistence .set_search_attribute_datetime (sa_datetime_key , final_sa_datetime )
6298 return StateDecision .graceful_complete_workflow ()
6399
64100
65101class PersistenceSearchAttributesWorkflow (ObjectWorkflow ):
66102 def get_workflow_states (self ) -> StateSchema :
67- return StateSchema .with_starting_state (SearchAttributeState ())
103+ return StateSchema .with_starting_state (
104+ SearchAttributeState1 (), SearchAttributeState2 ()
105+ )
68106
69107 def get_persistence_schema (self ) -> PersistenceSchema :
70108 return PersistenceSchema .create (
@@ -99,9 +137,15 @@ def setUpClass(cls):
99137 def test_persistence_search_attributes_workflow (self ):
100138 wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
101139
102- self .client .start_workflow (PersistenceSearchAttributesWorkflow , wf_id , 100 )
140+ wf_opts = WorkflowOptions ()
141+ wf_opts .add_wait_for_completion_state_ids (SearchAttributeState1 )
142+ self .client .start_workflow (
143+ PersistenceSearchAttributesWorkflow , wf_id , 100 , None , wf_opts
144+ )
103145
104- self .client .wait_for_workflow_completion (wf_id )
146+ self .client .wait_for_state_execution_completion_with_state_execution_id (
147+ SearchAttributeState1 , wf_id
148+ )
105149
106150 returned_search_attributes = self .client .get_all_search_attributes (
107151 PersistenceSearchAttributesWorkflow ,
@@ -120,3 +164,23 @@ def test_persistence_search_attributes_workflow(self):
120164 )
121165
122166 assert expected_search_attributes == returned_search_attributes
167+
168+ self .client .wait_for_workflow_completion (wf_id )
169+
170+ final_expected_search_attributes = dict ()
171+ final_expected_search_attributes [sa_keyword_key ] = final_sa_keyword
172+ final_expected_search_attributes [sa_double_key ] = sa_double
173+ final_expected_search_attributes [sa_int_key ] = final_sa_int
174+ final_expected_search_attributes [sa_bool_key ] = final_sa_bool
175+ final_expected_search_attributes [sa_keyword_array_key ] = final_sa_keyword_array
176+ final_expected_search_attributes [sa_datetime_key ] = (
177+ "2024-12-14T00:00:01.731455544Z" # This is a bug. The iwf-server always returns utc time. See https://github.com/indeedeng/iwf/issues/261
178+ # "2024-12-13T18:00:01.731455544-06:00"
179+ )
180+
181+ final_returned_search_attributes = self .client .get_all_search_attributes (
182+ PersistenceSearchAttributesWorkflow ,
183+ wf_id ,
184+ )
185+
186+ assert final_expected_search_attributes == final_returned_search_attributes
0 commit comments