11import inspect
22import time
33import unittest
4+ from time import sleep
45
56from iwf .client import Client
67from iwf .command_request import CommandRequest
1415from iwf .tests .worker_server import registry
1516from iwf .workflow import ObjectWorkflow
1617from iwf .workflow_context import WorkflowContext
18+ from iwf .workflow_options import WorkflowOptions
1719from iwf .workflow_state import T , WorkflowState
1820
1921sa_keyword_key = "CustomKeywordField"
3234sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
3335sa_keyword_array : list [str ] = ["keyword-1" , "keyword-2" ]
3436
37+ final_sa_keyword : str = "final_keyword"
38+ final_sa_text = None
39+ final_sa_int : int = 567
40+ final_sa_bool : bool = False
41+ final_sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
42+ final_sa_keyword_array : list [str ] = ["final_keyword-1" , "final_keyword-2" ]
3543
36- class SearchAttributeState (WorkflowState [None ]):
44+
45+ class SearchAttributeState1 (WorkflowState [None ]):
3746 def wait_until (
3847 self ,
3948 ctx : WorkflowContext ,
@@ -59,12 +68,44 @@ def execute(
5968 )
6069 persistence .set_search_attribute_int64 (sa_int_key , sa_int )
6170 persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
71+ return StateDecision .single_next_state (SearchAttributeState2 )
72+
73+
74+ class SearchAttributeState2 (WorkflowState [None ]):
75+ def wait_until (
76+ self ,
77+ ctx : WorkflowContext ,
78+ input : T ,
79+ persistence : Persistence ,
80+ communication : Communication ,
81+ ) -> CommandRequest :
82+ return CommandRequest .empty ()
83+
84+ def execute (
85+ self ,
86+ ctx : WorkflowContext ,
87+ input : T ,
88+ command_results : CommandResults ,
89+ persistence : Persistence ,
90+ communication : Communication ,
91+ ) -> StateDecision :
92+ # Delay updating search attributes to allow for the first assertion
93+ sleep (1 )
94+ persistence .set_search_attribute_keyword (sa_keyword_key , final_sa_keyword )
95+ persistence .set_search_attribute_boolean (sa_bool_key , final_sa_bool )
96+ persistence .set_search_attribute_keyword_array (
97+ sa_keyword_array_key , final_sa_keyword_array
98+ )
99+ persistence .set_search_attribute_int64 (sa_int_key , final_sa_int )
100+ persistence .set_search_attribute_datetime (sa_datetime_key , final_sa_datetime )
62101 return StateDecision .graceful_complete_workflow ()
63102
64103
65104class PersistenceSearchAttributesWorkflow (ObjectWorkflow ):
66105 def get_workflow_states (self ) -> StateSchema :
67- return StateSchema .with_starting_state (SearchAttributeState ())
106+ return StateSchema .with_starting_state (
107+ SearchAttributeState1 (), SearchAttributeState2 ()
108+ )
68109
69110 def get_persistence_schema (self ) -> PersistenceSchema :
70111 return PersistenceSchema .create (
@@ -99,9 +140,15 @@ def setUpClass(cls):
99140 def test_persistence_search_attributes_workflow (self ):
100141 wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
101142
102- self .client .start_workflow (PersistenceSearchAttributesWorkflow , wf_id , 100 )
143+ wf_opts = WorkflowOptions ()
144+ wf_opts .add_wait_for_completion_state_ids (SearchAttributeState1 )
145+ self .client .start_workflow (
146+ PersistenceSearchAttributesWorkflow , wf_id , 100 , None , wf_opts
147+ )
103148
104- self .client .wait_for_workflow_completion (wf_id )
149+ self .client .wait_for_state_execution_completion_with_state_execution_id (
150+ SearchAttributeState1 , wf_id
151+ )
105152
106153 returned_search_attributes = self .client .get_all_search_attributes (
107154 PersistenceSearchAttributesWorkflow ,
@@ -120,3 +167,23 @@ def test_persistence_search_attributes_workflow(self):
120167 )
121168
122169 assert expected_search_attributes == returned_search_attributes
170+
171+ self .client .wait_for_workflow_completion (wf_id )
172+
173+ final_expected_search_attributes = dict ()
174+ final_expected_search_attributes [sa_keyword_key ] = final_sa_keyword
175+ final_expected_search_attributes [sa_double_key ] = sa_double
176+ final_expected_search_attributes [sa_int_key ] = final_sa_int
177+ final_expected_search_attributes [sa_bool_key ] = final_sa_bool
178+ final_expected_search_attributes [sa_keyword_array_key ] = final_sa_keyword_array
179+ final_expected_search_attributes [sa_datetime_key ] = (
180+ "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
181+ # "2024-12-13T18:00:01.731455544-06:00"
182+ )
183+
184+ final_returned_search_attributes = self .client .get_all_search_attributes (
185+ PersistenceSearchAttributesWorkflow ,
186+ wf_id ,
187+ )
188+
189+ assert final_expected_search_attributes == final_returned_search_attributes
0 commit comments