11import inspect
22import time
33import unittest
4- from time import sleep
54
65from iwf .client import Client
7- from iwf .command_request import CommandRequest , TimerCommand
6+ from iwf .command_request import CommandRequest
87from iwf .command_results import CommandResults
98from iwf .communication import Communication
109from iwf .iwf_api .models import SearchAttributeValueType
1514from iwf .tests .worker_server import registry
1615from iwf .workflow import ObjectWorkflow
1716from iwf .workflow_context import WorkflowContext
18- from iwf .workflow_options import WorkflowOptions
1917from iwf .workflow_state import T , WorkflowState
2018
19+ sa_keyword_key = "CustomKeywordField"
2120sa_text_key = "CustomTextField"
2221sa_double_key = "CustomDoubleField"
22+ sa_int_key = "CustomIntField"
2323sa_bool_key = "CustomBoolField"
2424sa_datetime_key = "CustomDatetimeField"
2525sa_keyword_array_key = "CustomKeywordArrayField"
2626
27+ sa_keyword : str = "keyword"
2728sa_text : str = "text"
2829sa_double : float = 2.34
29- sa_datetime : str = "2024-11-12T16:00:01.731455544-08:00"
30+ sa_int : int = 234
31+ sa_bool : bool = False
32+ sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
3033sa_keyword_array : list [str ] = ["keyword-1" , "keyword-2" ]
3134
32- final_sa_text = None
33- final_sa_bool : bool = False
34- final_sa_datetime : str = "2024-12-13T16:00:01.731455544-08:00"
35- final_sa_keyword_array : list [str ] = ["final_keyword-1" , "final_keyword-2" ]
3635
37-
38- class SearchAttributeState1 (WorkflowState [None ]):
36+ class SearchAttributeState (WorkflowState [None ]):
3937 def wait_until (
4038 self ,
4139 ctx : WorkflowContext ,
4240 input : T ,
4341 persistence : Persistence ,
4442 communication : Communication ,
4543 ) -> CommandRequest :
46- persistence .set_search_attribute_text (sa_text_key , sa_text )
4744 return CommandRequest .empty ()
4845
4946 def execute (
@@ -54,76 +51,25 @@ def execute(
5451 persistence : Persistence ,
5552 communication : Communication ,
5653 ) -> StateDecision :
54+ persistence .set_search_attribute_keyword (sa_keyword_key , sa_keyword )
5755 persistence .set_search_attribute_double (sa_double_key , sa_double )
58- return StateDecision .single_next_state (SearchAttributeState2 )
59-
60-
61- class SearchAttributeState2 (WorkflowState [None ]):
62- def wait_until (
63- self ,
64- ctx : WorkflowContext ,
65- input : T ,
66- persistence : Persistence ,
67- communication : Communication ,
68- ) -> CommandRequest :
69- persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
70- return CommandRequest .for_all_command_completed (
71- TimerCommand .by_seconds (2 ),
72- )
73-
74- def execute (
75- self ,
76- ctx : WorkflowContext ,
77- input : T ,
78- command_results : CommandResults ,
79- persistence : Persistence ,
80- communication : Communication ,
81- ) -> StateDecision :
56+ persistence .set_search_attribute_boolean (sa_bool_key , sa_bool )
8257 persistence .set_search_attribute_keyword_array (
8358 sa_keyword_array_key , sa_keyword_array
8459 )
85- return StateDecision .single_next_state (SearchAttributeState3 )
86-
87-
88- class SearchAttributeState3 (WorkflowState [None ]):
89- def wait_until (
90- self ,
91- ctx : WorkflowContext ,
92- input : T ,
93- persistence : Persistence ,
94- communication : Communication ,
95- ) -> CommandRequest :
96- return CommandRequest .for_all_command_completed (
97- TimerCommand .by_seconds (2 ),
98- )
99-
100- def execute (
101- self ,
102- ctx : WorkflowContext ,
103- input : T ,
104- command_results : CommandResults ,
105- persistence : Persistence ,
106- communication : Communication ,
107- ) -> StateDecision :
108- persistence .set_search_attribute_text (sa_text_key , final_sa_text )
109- persistence .set_search_attribute_boolean (sa_bool_key , final_sa_bool )
110- persistence .set_search_attribute_datetime (sa_datetime_key , final_sa_datetime )
111- persistence .set_search_attribute_keyword_array (
112- sa_keyword_array_key , final_sa_keyword_array
113- )
60+ persistence .set_search_attribute_int64 (sa_int_key , sa_int )
61+ persistence .set_search_attribute_datetime (sa_datetime_key , sa_datetime )
11462 return StateDecision .graceful_complete_workflow ()
11563
11664
11765class PersistenceSearchAttributesWorkflow (ObjectWorkflow ):
11866 def get_workflow_states (self ) -> StateSchema :
119- return StateSchema .with_starting_state (
120- SearchAttributeState1 (), SearchAttributeState2 (), SearchAttributeState3 ()
121- )
67+ return StateSchema .with_starting_state (SearchAttributeState ())
12268
12369 def get_persistence_schema (self ) -> PersistenceSchema :
12470 return PersistenceSchema .create (
12571 PersistenceField .search_attribute_def (
126- sa_text_key , SearchAttributeValueType .TEXT
72+ sa_keyword_key , SearchAttributeValueType .KEYWORD
12773 ),
12874 PersistenceField .search_attribute_def (
12975 sa_double_key , SearchAttributeValueType .DOUBLE
@@ -132,10 +78,13 @@ def get_persistence_schema(self) -> PersistenceSchema:
13278 sa_bool_key , SearchAttributeValueType .BOOL
13379 ),
13480 PersistenceField .search_attribute_def (
135- sa_datetime_key , SearchAttributeValueType .DATETIME
81+ sa_keyword_array_key , SearchAttributeValueType .KEYWORD_ARRAY
13682 ),
13783 PersistenceField .search_attribute_def (
138- sa_keyword_array_key , SearchAttributeValueType .KEYWORD_ARRAY
84+ sa_int_key , SearchAttributeValueType .INT
85+ ),
86+ PersistenceField .search_attribute_def (
87+ sa_datetime_key , SearchAttributeValueType .DATETIME
13988 ),
14089 )
14190
@@ -150,53 +99,24 @@ def setUpClass(cls):
15099 def test_persistence_search_attributes_workflow (self ):
151100 wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
152101
153- wf_opts = WorkflowOptions ()
154- wf_opts .add_wait_for_completion_state_ids (SearchAttributeState2 )
155-
156- self .client .start_workflow (
157- PersistenceSearchAttributesWorkflow , wf_id , 100 , None , wf_opts
158- )
159-
160- # Short sleep distribute calls made to the server
161- sleep (1 )
162-
163- self .client .wait_for_state_execution_completion_with_state_execution_id (
164- SearchAttributeState2 , wf_id
165- )
102+ self .client .start_workflow (PersistenceSearchAttributesWorkflow , wf_id , 100 )
166103
167- # Short sleep distribute calls made to the server
168- sleep (1 )
104+ self .client .wait_for_workflow_completion (wf_id )
169105
170106 returned_search_attributes = self .client .get_all_search_attributes (
171- PersistenceSearchAttributesWorkflow , wf_id
107+ PersistenceSearchAttributesWorkflow ,
108+ wf_id ,
172109 )
173110
174111 expected_search_attributes = dict ()
175- expected_search_attributes [sa_text_key ] = sa_text
112+ expected_search_attributes [sa_keyword_key ] = sa_keyword
176113 expected_search_attributes [sa_double_key ] = sa_double
114+ expected_search_attributes [sa_bool_key ] = sa_bool
177115 expected_search_attributes [sa_keyword_array_key ] = sa_keyword_array
116+ expected_search_attributes [sa_int_key ] = sa_int
178117 expected_search_attributes [sa_datetime_key ] = (
179- "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
180- # "2024-11-12T18:00:01.731455544-06:00"
181- )
182-
183- assert expected_search_attributes == returned_search_attributes
184-
185- self .client .wait_for_workflow_completion (wf_id )
186-
187- final_returned_search_attributes = self .client .get_all_search_attributes (
188- PersistenceSearchAttributesWorkflow ,
189- wf_id ,
190- )
191-
192- final_expected_search_attributes = dict ()
193- final_expected_search_attributes [sa_text_key ] = ""
194- final_expected_search_attributes [sa_double_key ] = sa_double
195- final_expected_search_attributes [sa_bool_key ] = final_sa_bool
196- final_expected_search_attributes [sa_keyword_array_key ] = final_sa_keyword_array
197- final_expected_search_attributes [sa_datetime_key ] = (
198118 "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
199119 # "2024-12-13T18:00:01.731455544-06:00"
200120 )
201121
202- assert final_expected_search_attributes == final_returned_search_attributes
122+ assert expected_search_attributes == returned_search_attributes
0 commit comments