44from time import sleep
55
66from iwf .client import Client
7- from iwf .command_request import CommandRequest
7+ from iwf .command_request import CommandRequest , TimerCommand
88from iwf .command_results import CommandResults
99from iwf .communication import Communication
1010from iwf .iwf_api .models import SearchAttributeValueType
2525sa_datetime_key = "CustomDatetimeField"
2626sa_keyword_array_key = "CustomKeywordArrayField"
2727
28+ initial_sa_keyword : str = "initial_keyword"
29+ initial_sa_double : float = 1.11
30+ initial_sa_int : int = 1
31+ initial_sa_bool : bool = True
32+ initial_sa_datetime : str = "2024-11-09T16:00:01.731455544-08:00"
33+ initial_sa_keyword_array : list [str ] = ["initial_keyword-1" , "initial_keyword-2" ]
34+
2835sa_keyword : str = "keyword"
2936sa_double : float = 2.34
3037sa_int : int = 234
3946final_sa_keyword_array : list [str ] = ["final_keyword-1" , "final_keyword-2" ]
4047
4148
42- class SearchAttributeState1 (WorkflowState [None ]):
49+ class SearchAttributeStateInit (WorkflowState [None ]):
4350 def wait_until (
4451 self ,
4552 ctx : WorkflowContext ,
4653 input : T ,
4754 persistence : Persistence ,
4855 communication : Communication ,
4956 ) -> CommandRequest :
50- return CommandRequest .empty ()
57+ return CommandRequest .for_all_command_completed (
58+ TimerCommand .by_seconds (2 ),
59+ )
5160
5261 def execute (
5362 self ,
@@ -65,6 +74,29 @@ def execute(
6574 )
6675 persistence .set_search_attribute_int64 (sa_int_key , sa_int )
6776 persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
77+ return StateDecision .single_next_state (SearchAttributeState1 )
78+
79+
80+ class SearchAttributeState1 (WorkflowState [None ]):
81+ def wait_until (
82+ self ,
83+ ctx : WorkflowContext ,
84+ input : T ,
85+ persistence : Persistence ,
86+ communication : Communication ,
87+ ) -> CommandRequest :
88+ return CommandRequest .for_all_command_completed (
89+ TimerCommand .by_seconds (2 ),
90+ )
91+
92+ def execute (
93+ self ,
94+ ctx : WorkflowContext ,
95+ input : T ,
96+ command_results : CommandResults ,
97+ persistence : Persistence ,
98+ communication : Communication ,
99+ ) -> StateDecision :
68100 return StateDecision .single_next_state (SearchAttributeState2 )
69101
70102
@@ -86,7 +118,7 @@ def execute(
86118 persistence : Persistence ,
87119 communication : Communication ,
88120 ) -> StateDecision :
89- # Delay updating search attributes to allow for the first assertion
121+ # Delay updating search attributes to allow for the previous assertion
90122 sleep (1 )
91123 persistence .set_search_attribute_keyword (sa_keyword_key , final_sa_keyword )
92124 persistence .set_search_attribute_boolean (sa_bool_key , final_sa_bool )
@@ -101,7 +133,7 @@ def execute(
101133class PersistenceSearchAttributesWorkflow (ObjectWorkflow ):
102134 def get_workflow_states (self ) -> StateSchema :
103135 return StateSchema .with_starting_state (
104- SearchAttributeState1 (), SearchAttributeState2 ()
136+ SearchAttributeStateInit (), SearchAttributeState1 (), SearchAttributeState2 ()
105137 )
106138
107139 def get_persistence_schema (self ) -> PersistenceSchema :
@@ -137,12 +169,44 @@ def setUpClass(cls):
137169 def test_persistence_search_attributes_workflow (self ):
138170 wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
139171
140- wf_opts = WorkflowOptions ()
172+ wf_opts = WorkflowOptions (
173+ initial_search_attributes = {
174+ sa_keyword_key : initial_sa_keyword ,
175+ sa_double_key : initial_sa_double ,
176+ sa_int_key : initial_sa_int ,
177+ sa_bool_key : initial_sa_bool ,
178+ sa_datetime_key : initial_sa_datetime ,
179+ sa_keyword_array_key : initial_sa_keyword_array ,
180+ }
181+ )
141182 wf_opts .add_wait_for_completion_state_ids (SearchAttributeState1 )
142183 self .client .start_workflow (
143184 PersistenceSearchAttributesWorkflow , wf_id , 100 , None , wf_opts
144185 )
145186
187+ # Delay to allow for workflow to start and set search attributes
188+ sleep (1 )
189+
190+ initial_returned_search_attributes = self .client .get_all_search_attributes (
191+ PersistenceSearchAttributesWorkflow ,
192+ wf_id ,
193+ )
194+
195+ initial_expected_search_attributes = dict ()
196+ initial_expected_search_attributes [sa_keyword_key ] = initial_sa_keyword
197+ initial_expected_search_attributes [sa_double_key ] = initial_sa_double
198+ initial_expected_search_attributes [sa_bool_key ] = initial_sa_bool
199+ initial_expected_search_attributes [sa_keyword_array_key ] = (
200+ initial_sa_keyword_array
201+ )
202+ initial_expected_search_attributes [sa_int_key ] = initial_sa_int
203+ initial_expected_search_attributes [sa_datetime_key ] = (
204+ "2024-11-10T00:00:01.731455544Z" # This is a bug. The iwf-server always returns utc time. See https://github.com/indeedeng/iwf/issues/261
205+ # "2024-11-09T18:00:01.731455544-06:00"
206+ )
207+
208+ assert initial_expected_search_attributes == initial_returned_search_attributes
209+
146210 self .client .wait_for_state_execution_completion_with_state_execution_id (
147211 SearchAttributeState1 , wf_id
148212 )
0 commit comments