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"
20- sa_text_key = "CustomTextField"
2122sa_double_key = "CustomDoubleField"
2223sa_int_key = "CustomIntField"
2324sa_bool_key = "CustomBoolField"
2425sa_datetime_key = "CustomDatetimeField"
2526sa_keyword_array_key = "CustomKeywordArrayField"
2627
2728sa_keyword : str = "keyword"
28- sa_text : str = "text"
2929sa_double : float = 2.34
3030sa_int : int = 234
3131sa_bool : bool = False
32- sa_datetime : str = "2024-12-13T16 :00:01.731455544-08:00"
32+ sa_datetime : str = "2024-11-12T16 :00:01.731455544-08:00"
3333sa_keyword_array : list [str ] = ["keyword-1" , "keyword-2" ]
3434
35+ final_sa_keyword : str = "final_keyword"
36+ final_sa_int : int = 567
37+ final_sa_bool : bool = False
38+ final_sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
39+ final_sa_keyword_array : list [str ] = ["final_keyword-1" , "final_keyword-2" ]
3540
36- class SearchAttributeState (WorkflowState [None ]):
41+
42+ class SearchAttributeState1 (WorkflowState [None ]):
3743 def wait_until (
3844 self ,
3945 ctx : WorkflowContext ,
@@ -59,12 +65,44 @@ def execute(
5965 )
6066 persistence .set_search_attribute_int64 (sa_int_key , sa_int )
6167 persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
68+ return StateDecision .single_next_state (SearchAttributeState2 )
69+
70+
71+ class SearchAttributeState2 (WorkflowState [None ]):
72+ def wait_until (
73+ self ,
74+ ctx : WorkflowContext ,
75+ input : T ,
76+ persistence : Persistence ,
77+ communication : Communication ,
78+ ) -> CommandRequest :
79+ return CommandRequest .empty ()
80+
81+ def execute (
82+ self ,
83+ ctx : WorkflowContext ,
84+ input : T ,
85+ command_results : CommandResults ,
86+ persistence : Persistence ,
87+ communication : Communication ,
88+ ) -> StateDecision :
89+ # Delay updating search attributes to allow for the first assertion
90+ sleep (1 )
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 ,
@@ -115,8 +159,28 @@ def test_persistence_search_attributes_workflow(self):
115159 expected_search_attributes [sa_keyword_array_key ] = sa_keyword_array
116160 expected_search_attributes [sa_int_key ] = sa_int
117161 expected_search_attributes [sa_datetime_key ] = (
162+ "2024-11-13T00:00:01.731455544Z" # This is a bug. The iwf-server always returns utc time. See https://github.com/indeedeng/iwf/issues/261
163+ # "2024-11-12T18:00:01.731455544-06:00"
164+ )
165+
166+ 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 ] = (
118177 "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
119178 # "2024-12-13T18:00:01.731455544-06:00"
120179 )
121180
122- assert expected_search_attributes == returned_search_attributes
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