11import inspect
22import time
33import unittest
4+ from time import sleep
45
56from iwf .client import Client
6- from iwf .command_request import CommandRequest , TimerCommand
7+ from iwf .command_request import CommandRequest
78from iwf .command_results import CommandResults
89from iwf .communication import Communication
910from iwf .iwf_api .models import SearchAttributeValueType
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
31+ sa_bool : bool = False
3132sa_datetime : str = "2024-11-12T16:00:01.731455544-08:00"
3233sa_keyword_array : list [str ] = ["keyword-1" , "keyword-2" ]
3334
3435final_sa_keyword : str = "final_keyword"
35- final_sa_text = None
3636final_sa_int : int = 567
3737final_sa_bool : bool = False
3838final_sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
@@ -47,14 +47,6 @@ def wait_until(
4747 persistence : Persistence ,
4848 communication : Communication ,
4949 ) -> CommandRequest :
50- persistence .set_search_attribute_keyword (sa_keyword_key , sa_keyword )
51- persistence .set_search_attribute_text (sa_text_key , sa_text )
52- persistence .set_search_attribute_double (sa_double_key , sa_double )
53- persistence .set_search_attribute_int64 (sa_int_key , sa_int )
54- persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
55- persistence .set_search_attribute_keyword_array (
56- sa_keyword_array_key , sa_keyword_array
57- )
5850 return CommandRequest .empty ()
5951
6052 def execute (
@@ -65,6 +57,14 @@ def execute(
6557 persistence : Persistence ,
6658 communication : Communication ,
6759 ) -> StateDecision :
60+ persistence .set_search_attribute_keyword (sa_keyword_key , sa_keyword )
61+ persistence .set_search_attribute_double (sa_double_key , sa_double )
62+ persistence .set_search_attribute_boolean (sa_bool_key , sa_bool )
63+ persistence .set_search_attribute_keyword_array (
64+ sa_keyword_array_key , sa_keyword_array
65+ )
66+ persistence .set_search_attribute_int64 (sa_int_key , sa_int )
67+ persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
6868 return StateDecision .single_next_state (SearchAttributeState2 )
6969
7070
@@ -76,9 +76,7 @@ def wait_until(
7676 persistence : Persistence ,
7777 communication : Communication ,
7878 ) -> CommandRequest :
79- return CommandRequest .for_all_command_completed (
80- TimerCommand .by_seconds (7 ),
81- )
79+ return CommandRequest .empty ()
8280
8381 def execute (
8482 self ,
@@ -88,14 +86,15 @@ def execute(
8886 persistence : Persistence ,
8987 communication : Communication ,
9088 ) -> StateDecision :
89+ # Delay updating search attributes to allow for the first assertion
90+ sleep (1 )
9191 persistence .set_search_attribute_keyword (sa_keyword_key , final_sa_keyword )
92- persistence .set_search_attribute_text (sa_text_key , final_sa_text )
93- persistence .set_search_attribute_int64 (sa_int_key , final_sa_int )
9492 persistence .set_search_attribute_boolean (sa_bool_key , final_sa_bool )
95- persistence .set_search_attribute_datetime (sa_datetime_key , final_sa_datetime )
9693 persistence .set_search_attribute_keyword_array (
9794 sa_keyword_array_key , final_sa_keyword_array
9895 )
96+ persistence .set_search_attribute_int64 (sa_int_key , final_sa_int )
97+ persistence .set_search_attribute_datetime (sa_datetime_key , final_sa_datetime )
9998 return StateDecision .graceful_complete_workflow ()
10099
101100
@@ -110,23 +109,20 @@ def get_persistence_schema(self) -> PersistenceSchema:
110109 PersistenceField .search_attribute_def (
111110 sa_keyword_key , SearchAttributeValueType .KEYWORD
112111 ),
113- PersistenceField .search_attribute_def (
114- sa_text_key , SearchAttributeValueType .TEXT
115- ),
116112 PersistenceField .search_attribute_def (
117113 sa_double_key , SearchAttributeValueType .DOUBLE
118114 ),
119115 PersistenceField .search_attribute_def (
120- sa_int_key , SearchAttributeValueType .INT
116+ sa_bool_key , SearchAttributeValueType .BOOL
121117 ),
122118 PersistenceField .search_attribute_def (
123- sa_bool_key , SearchAttributeValueType .BOOL
119+ sa_keyword_array_key , SearchAttributeValueType .KEYWORD_ARRAY
124120 ),
125121 PersistenceField .search_attribute_def (
126- sa_datetime_key , SearchAttributeValueType .DATETIME
122+ sa_int_key , SearchAttributeValueType .INT
127123 ),
128124 PersistenceField .search_attribute_def (
129- sa_keyword_array_key , SearchAttributeValueType .KEYWORD_ARRAY
125+ sa_datetime_key , SearchAttributeValueType .DATETIME
130126 ),
131127 )
132128
@@ -141,22 +137,27 @@ def setUpClass(cls):
141137 def test_persistence_search_attributes_workflow (self ):
142138 wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
143139
144- 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+ )
145145
146- # TODO: Should be replaced with wait_for_state_execution_completed once implemented
147- # https://github.com/indeedeng/iwf-python-sdk/issues/48
148- time . sleep ( 5 )
146+ self . client . wait_for_state_execution_completion_with_state_execution_id (
147+ SearchAttributeState1 , wf_id
148+ )
149149
150150 returned_search_attributes = self .client .get_all_search_attributes (
151- PersistenceSearchAttributesWorkflow , wf_id
151+ PersistenceSearchAttributesWorkflow ,
152+ wf_id ,
152153 )
153154
154155 expected_search_attributes = dict ()
155156 expected_search_attributes [sa_keyword_key ] = sa_keyword
156- expected_search_attributes [sa_text_key ] = sa_text
157157 expected_search_attributes [sa_double_key ] = sa_double
158- expected_search_attributes [sa_int_key ] = sa_int
158+ expected_search_attributes [sa_bool_key ] = sa_bool
159159 expected_search_attributes [sa_keyword_array_key ] = sa_keyword_array
160+ expected_search_attributes [sa_int_key ] = sa_int
160161 expected_search_attributes [sa_datetime_key ] = (
161162 "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
162163 # "2024-11-12T18:00:01.731455544-06:00"
@@ -166,14 +167,8 @@ def test_persistence_search_attributes_workflow(self):
166167
167168 self .client .wait_for_workflow_completion (wf_id )
168169
169- final_returned_search_attributes = self .client .get_all_search_attributes (
170- PersistenceSearchAttributesWorkflow ,
171- wf_id ,
172- )
173-
174170 final_expected_search_attributes = dict ()
175171 final_expected_search_attributes [sa_keyword_key ] = final_sa_keyword
176- final_expected_search_attributes [sa_text_key ] = ""
177172 final_expected_search_attributes [sa_double_key ] = sa_double
178173 final_expected_search_attributes [sa_int_key ] = final_sa_int
179174 final_expected_search_attributes [sa_bool_key ] = final_sa_bool
@@ -183,4 +178,9 @@ def test_persistence_search_attributes_workflow(self):
183178 # "2024-12-13T18:00:01.731455544-06:00"
184179 )
185180
181+ final_returned_search_attributes = self .client .get_all_search_attributes (
182+ PersistenceSearchAttributesWorkflow ,
183+ wf_id ,
184+ )
185+
186186 assert final_expected_search_attributes == final_returned_search_attributes
0 commit comments