3
3
import unittest
4
4
5
5
from iwf .client import Client
6
+ from iwf .iwf_api .models .timer_result import TimerStatus
6
7
from iwf .command_request import (
7
8
CommandRequest ,
8
9
SignalChannelCommand ,
10
+ TimerCommand ,
11
+ )
12
+ from iwf .command_results import (
13
+ CommandResults ,
14
+ SignalChannelCommandResult ,
15
+ TimerCommandResult ,
9
16
)
10
- from iwf .command_results import CommandResults , SignalChannelCommandResult
11
17
from iwf .communication import Communication
12
18
from iwf .communication_schema import CommunicationMethod , CommunicationSchema
13
19
from iwf .iwf_api .models import ChannelRequestStatus
25
31
test_channel_str = "test-str"
26
32
test_idle_channel_none = "test-idle"
27
33
34
+ test_channel1 = "test-channel1"
35
+ test_channel1_id = "test-channel1-id"
36
+ test_channel2 = "test-channel2"
37
+ test_channel2_id = "test-channel2-id"
38
+ test_timer_id = "test-timer-id"
39
+
28
40
29
- class WaitState (WorkflowState [None ]):
41
+ class WaitState1 (WorkflowState [None ]):
30
42
def wait_until (
31
43
self ,
32
44
ctx : WorkflowContext ,
@@ -61,7 +73,58 @@ def execute(
61
73
assert sig3 == SignalChannelCommandResult (
62
74
test_channel_str , "abc" , ChannelRequestStatus .RECEIVED , ""
63
75
)
64
- return StateDecision .graceful_complete_workflow (sig3 .value )
76
+ return StateDecision .single_next_state (WaitState2 , sig3 .value )
77
+
78
+
79
+ class WaitState2 (WorkflowState [str ]):
80
+ def wait_until (
81
+ self ,
82
+ ctx : WorkflowContext ,
83
+ input : str ,
84
+ persistence : Persistence ,
85
+ communication : Communication ,
86
+ ) -> CommandRequest :
87
+ return CommandRequest .for_any_command_combination_completed (
88
+ [
89
+ [
90
+ test_channel1_id ,
91
+ test_timer_id ,
92
+ ]
93
+ ],
94
+ SignalChannelCommand .by_name (test_channel1 , test_channel1_id ),
95
+ SignalChannelCommand .by_name (test_channel2 , test_channel2_id ),
96
+ TimerCommand .by_seconds (1 , test_timer_id ),
97
+ )
98
+
99
+ def execute (
100
+ self ,
101
+ ctx : WorkflowContext ,
102
+ input : str ,
103
+ command_results : CommandResults ,
104
+ persistence : Persistence ,
105
+ communication : Communication ,
106
+ ) -> StateDecision :
107
+ assert len (command_results .signal_channel_commands ) == 2
108
+ assert (
109
+ len (
110
+ [
111
+ r
112
+ for r in command_results .signal_channel_commands
113
+ if r .status == ChannelRequestStatus .RECEIVED
114
+ ]
115
+ )
116
+ == 1
117
+ )
118
+ sig1 = command_results .signal_channel_commands [0 ]
119
+ tim1 = command_results .timer_commands [0 ]
120
+ assert sig1 == SignalChannelCommandResult (
121
+ test_channel1 , None , ChannelRequestStatus .RECEIVED , test_channel1_id
122
+ )
123
+ assert tim1 == TimerCommandResult (
124
+ TimerStatus .FIRED ,
125
+ test_timer_id ,
126
+ )
127
+ return StateDecision .graceful_complete_workflow (input )
65
128
66
129
67
130
class WaitSignalWorkflow (ObjectWorkflow ):
@@ -71,10 +134,12 @@ def get_communication_schema(self) -> CommunicationSchema:
71
134
CommunicationMethod .signal_channel_def (test_channel_none , type (None )),
72
135
CommunicationMethod .signal_channel_def (test_channel_str , str ),
73
136
CommunicationMethod .signal_channel_def (test_idle_channel_none , type (None )),
137
+ CommunicationMethod .signal_channel_def (test_channel1 , type (None )),
138
+ CommunicationMethod .signal_channel_def (test_channel2 , type (None )),
74
139
)
75
140
76
141
def get_workflow_states (self ) -> StateSchema :
77
- return StateSchema .with_starting_state (WaitState ())
142
+ return StateSchema .with_starting_state (WaitState1 (), WaitState2 ())
78
143
79
144
@rpc ()
80
145
def get_idle_signal_channel_size (self , com : Communication ):
@@ -90,11 +155,13 @@ def setUpClass(cls):
90
155
91
156
def test_signal (self ):
92
157
wf_id = f"{ inspect .currentframe ().f_code .co_name } -{ time .time_ns ()} "
93
- self .client .start_workflow (WaitSignalWorkflow , wf_id , 1 )
158
+ self .client .start_workflow (WaitSignalWorkflow , wf_id , 10 )
94
159
self .client .signal_workflow (wf_id , test_channel_int , 123 )
95
160
self .client .signal_workflow (wf_id , test_channel_str , "abc" )
96
161
self .client .signal_workflow (wf_id , test_channel_none )
97
- res = self .client .get_simple_workflow_result_with_wait (wf_id )
162
+
163
+ self .client .signal_workflow (wf_id , test_channel1 )
164
+ res = self .client .wait_for_workflow_completion (wf_id , str )
98
165
assert res == "abc"
99
166
100
167
def test_signal_channel_size (self ):
0 commit comments