forked from dapr/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversioning.py
More file actions
291 lines (229 loc) · 10.7 KB
/
versioning.py
File metadata and controls
291 lines (229 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# -*- coding: utf-8 -*-
# Copyright 2026 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import time
import dapr.ext.workflow as wf
current_test = 0
def print_test(message):
print(f'test{current_test}: {message}', flush=True)
print_activity = None
wfr = None
def new_wfr():
global wfr
global print_activity
if wfr is not None:
wfr.shutdown()
wfr = wf.WorkflowRuntime()
def print_activity(ctx, input):
return print_test(input)
wfr.register_activity(print_activity, name='print_activity')
new_wfr()
def test_full_versioning(client: wf.DaprWorkflowClient):
global current_test
# Start with only one version defined. Runnig the workflow should run this version as it normally would.
current_test = 1
@wfr.versioned_workflow(name='workflow', is_latest=True)
def version1_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version1')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished workflow for version1')
return 1
print_test('triggering workflow')
instance_id = client.schedule_new_workflow(workflow=version1_workflow)
client.raise_workflow_event(instance_id, event_name='event')
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
# Now we start a workflow, but introduce a latest version half way. It should resume the execution in the old version.
current_test = 2
print_test('triggering workflow')
instance_id = client.schedule_new_workflow(workflow=version1_workflow)
time.sleep(2) # wait for the workflow to start and wait for the event
@wfr.versioned_workflow(name='workflow', is_latest=True)
def version2_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version2')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished workflow for version2')
return 1
client.raise_workflow_event(instance_id, event_name='event')
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
# Now we have the two versions defined, running the workflow now should run v2 as it's the latest version.
current_test = 3
print_test('triggering workflow')
instance_id = client.schedule_new_workflow(workflow=version1_workflow)
client.raise_workflow_event(instance_id, event_name='event')
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
def test_patching(client: wf.DaprWorkflowClient):
global current_test
@wfr.workflow
def patching_workflow(ctx: wf.DaprWorkflowContext):
# This function will be changed throughout the test, to simulate different scenarios
return workflow_code(ctx)
# Runs the patched branch by default
current_test = 4
def workflow_code_v1_patch1_only(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='start')
if ctx.is_patched('patch1'):
yield ctx.call_activity(print_activity, input='patch1 is patched')
else:
yield ctx.call_activity(print_activity, input='patch1 is not patched')
return 1
workflow_code = workflow_code_v1_patch1_only
instance_id = client.schedule_new_workflow(workflow=patching_workflow)
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
# When the execution passed the place where a patch is introduced, it should be not patched.
def workflow_code_v2_patch2_after_event(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='start')
yield ctx.wait_for_external_event(name='event')
if ctx.is_patched('patch2'):
yield ctx.call_activity(print_activity, input='patch2 is patched')
else:
yield ctx.call_activity(print_activity, input='patch2 is not patched')
return 1
workflow_code = workflow_code_v2_patch2_after_event
current_test = 5
instance_id = client.schedule_new_workflow(workflow=patching_workflow)
time.sleep(2)
def workflow_code_v3_patch1_and_patch2_with_event(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='start')
if ctx.is_patched('patch1'):
yield ctx.call_activity(print_activity, input='patch1 is patched')
else:
yield ctx.call_activity(print_activity, input='patch1 is not patched')
yield ctx.wait_for_external_event(name='event')
if ctx.is_patched('patch2'):
yield ctx.call_activity(print_activity, input='patch2 is patched')
else:
yield ctx.call_activity(print_activity, input='patch2 is not patched')
return 1
workflow_code = workflow_code_v3_patch1_and_patch2_with_event
client.raise_workflow_event(instance_id, event_name='event')
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
# It remembers previous patches.
def workflow_code_v4_silence_patch1(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='start')
if ctx.is_patched('patch1'):
pass # keep it silenced for now, we'll add logs later and this ones would confuse the test
else:
pass
yield ctx.wait_for_external_event(name='event')
if ctx.is_patched('patch2'):
yield ctx.call_activity(print_activity, input='patch2 is patched')
else:
yield ctx.call_activity(print_activity, input='patch2 is not patched')
return 1
workflow_code = workflow_code_v4_silence_patch1
current_test = 6
instance_id = client.schedule_new_workflow(workflow=patching_workflow)
time.sleep(2)
workflow_code = workflow_code_v3_patch1_and_patch2_with_event
client.raise_workflow_event(instance_id, event_name='event')
client.wait_for_workflow_completion(instance_id, timeout_in_seconds=30)
def test_full_versioning_stall(client: wf.DaprWorkflowClient):
global current_test
new_wfr()
@wfr.versioned_workflow(name='stall_workflow', is_latest=True)
def version1_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version1')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished workflow for version1')
return 1
wfr.start()
current_test = 7
instance_id = client.schedule_new_workflow(workflow=version1_workflow)
time.sleep(3)
new_wfr()
@wfr.versioned_workflow(name='stall_workflow', is_latest=True)
def version2_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version2')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished workflow for version2')
return 1
wfr.start()
client.raise_workflow_event(instance_id, event_name='event')
time.sleep(2)
md = client.get_workflow_state(instance_id)
if md.runtime_status == wf.WorkflowStatus.STALLED:
print_test('Workflow is stalled')
else:
print_test('Workflow is not stalled')
def test_patching_stall(client: wf.DaprWorkflowClient):
global current_test
current_test = 8
@wfr.workflow
def patching_workflow(ctx: wf.DaprWorkflowContext):
# This function will be changed throughout the test, to simulate different scenarios
return workflow_code(ctx)
def workflow_code_v1_with_patch1_check(ctx: wf.DaprWorkflowContext):
if ctx.is_patched('patch1'):
pass
else:
pass
yield ctx.wait_for_external_event(name='event')
return 1
workflow_code = workflow_code_v1_with_patch1_check
instance_id = client.schedule_new_workflow(workflow=patching_workflow)
time.sleep(2)
def workflow_code_v2_without_patch1_check(ctx: wf.DaprWorkflowContext):
# Removed patch1 check
yield ctx.wait_for_external_event(name='event')
return 1
workflow_code = workflow_code_v2_without_patch1_check
client.raise_workflow_event(instance_id, event_name='event')
time.sleep(2)
md = client.get_workflow_state(instance_id)
if md.runtime_status == wf.WorkflowStatus.STALLED:
print_test('Workflow is stalled')
else:
print_test('Workflow is not stalled')
def main():
args = sys.argv[1:]
if len(args) == 0:
print('Usage: python versioning.py <part1|part2>')
return
if args[0] == 'part1':
wfr.start()
time.sleep(2) # wait for workflow runtime to start
client = wf.DaprWorkflowClient()
test_full_versioning(client)
test_patching(client)
test_full_versioning_stall(client)
test_patching_stall(client)
wfr.shutdown()
elif args[0] == 'part2':
global current_test
current_test = 100
print_test('part2')
@wfr.versioned_workflow(name='stall_workflow', is_latest=False)
def version1_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version1')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished stalled version1 workflow')
return 1
@wfr.versioned_workflow(name='stall_workflow', is_latest=True)
def version2_workflow(ctx: wf.DaprWorkflowContext):
yield ctx.call_activity(print_activity, input='Received workflow call for version2')
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished stalled version2 workflow')
return 1
@wfr.workflow
def patching_workflow(ctx: wf.DaprWorkflowContext):
if ctx.is_patched('patch1'):
pass
else:
pass
yield ctx.wait_for_external_event(name='event')
yield ctx.call_activity(print_activity, input='Finished stalled patching workflow')
return 1
wfr.start()
time.sleep(10)
wfr.shutdown()
if __name__ == '__main__':
main()