forked from microsoft/durabletask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dts_entity_failure_handling.py
More file actions
188 lines (152 loc) · 8.64 KB
/
Copy pathtest_dts_entity_failure_handling.py
File metadata and controls
188 lines (152 loc) · 8.64 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
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
import json
import os
from durabletask import client, entities, task
from durabletask.azuremanaged.client import DurableTaskSchedulerClient
from durabletask.azuremanaged.worker import DurableTaskSchedulerWorker
# Read the environment variables
taskhub_name = os.getenv("TASKHUB", "default")
endpoint = os.getenv("ENDPOINT", "http://localhost:8080")
def test_class_entity_unhandled_failure_fails():
class FailingEntity(entities.DurableEntity):
def fail(self, _):
raise ValueError("Something went wrong!")
def test_orchestrator(ctx: task.OrchestrationContext, _):
entity_id = entities.EntityInstanceId("FailingEntity", "testEntity")
yield ctx.call_entity(entity_id, "fail")
# Start a worker, which will connect to the sidecar in a background thread
with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None) as w:
w.add_orchestrator(test_orchestrator)
w.add_entity(FailingEntity)
w.start()
c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None)
id = c.schedule_new_orchestration(test_orchestrator)
state = c.wait_for_orchestration_completion(id, timeout=30)
assert state is not None
assert state.name == task.get_name(test_orchestrator)
assert state.instance_id == id
assert state.failure_details is not None
assert state.failure_details.error_type == "TaskFailedError"
# NOTE: Because FailureDetails does not support inner_failure, we can't verify that the inner failure type is
# EntityOperationFailedException. In the future, we should consider adding support for inner failures in
# FailureDetails to make this more robust. This applies to all tests in this file. For now, the error message's
# structure is sufficient to verify that the failure was due to the EntityOperationFailedException.
assert state.failure_details.message == "Operation 'fail' on entity '@failingentity@testEntity' failed with " \
"error: Something went wrong!"
assert state.runtime_status == client.OrchestrationStatus.FAILED
def test_function_entity_unhandled_failure_fails():
def failing_entity(ctx: entities.EntityContext, _):
raise ValueError("Something went wrong!")
def test_orchestrator(ctx: task.OrchestrationContext, _):
entity_id = entities.EntityInstanceId("failing_entity", "testEntity")
yield ctx.call_entity(entity_id, "fail")
# Start a worker, which will connect to the sidecar in a background thread
with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None) as w:
w.add_orchestrator(test_orchestrator)
w.add_entity(failing_entity)
w.start()
c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None)
id = c.schedule_new_orchestration(test_orchestrator)
state = c.wait_for_orchestration_completion(id, timeout=30)
assert state is not None
assert state.name == task.get_name(test_orchestrator)
assert state.instance_id == id
assert state.failure_details is not None
assert state.failure_details.error_type == "TaskFailedError"
assert state.failure_details.message == "Operation 'fail' on entity '@failing_entity@testEntity' failed with " \
"error: Something went wrong!"
assert state.runtime_status == client.OrchestrationStatus.FAILED
def test_class_entity_handled_failure_succeeds():
class FailingEntity(entities.DurableEntity):
def fail(self, _):
raise ValueError("Something went wrong!")
def test_orchestrator(ctx: task.OrchestrationContext, _):
entity_id = entities.EntityInstanceId("FailingEntity", "testEntity")
try:
yield ctx.call_entity(entity_id, "fail")
except task.TaskFailedError as e:
return e.details.message # returning just the message to avoid issues with JSON serialization of FailureDetails
# Start a worker, which will connect to the sidecar in a background thread
with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None) as w:
w.add_orchestrator(test_orchestrator)
w.add_entity(FailingEntity)
w.start()
c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None)
id = c.schedule_new_orchestration(test_orchestrator)
state = c.wait_for_orchestration_completion(id, timeout=30)
assert state is not None
assert state.name == task.get_name(test_orchestrator)
assert state.instance_id == id
assert state.failure_details is None
assert state.serialized_output is not None
output = json.loads(state.serialized_output)
assert output == "Operation 'fail' on entity '@failingentity@testEntity' failed with error: Something went wrong!"
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
def test_function_entity_handled_failure_succeeds():
def failing_entity(ctx: entities.EntityContext, _):
raise ValueError("Something went wrong!")
def test_orchestrator(ctx: task.OrchestrationContext, _):
entity_id = entities.EntityInstanceId("failing_entity", "testEntity")
try:
yield ctx.call_entity(entity_id, "fail")
except task.TaskFailedError as e:
return e.details.message # returning just the message to avoid issues with JSON serialization of FailureDetails
# Start a worker, which will connect to the sidecar in a background thread
with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None) as w:
w.add_orchestrator(test_orchestrator)
w.add_entity(failing_entity)
w.start()
c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None)
id = c.schedule_new_orchestration(test_orchestrator)
state = c.wait_for_orchestration_completion(id, timeout=30)
assert state is not None
assert state.name == task.get_name(test_orchestrator)
assert state.instance_id == id
assert state.failure_details is None
assert state.serialized_output is not None
output = json.loads(state.serialized_output)
assert output == "Operation 'fail' on entity '@failing_entity@testEntity' failed with error: Something went wrong!"
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
def test_class_entity_failure_unlocks_entity():
def failing_entity(ctx: entities.EntityContext, _):
raise ValueError("Something went wrong!")
def test_orchestrator(ctx: task.OrchestrationContext, _):
exception_count = 0
entity_id = entities.EntityInstanceId("failing_entity", "testEntity")
with (yield ctx.lock_entities([entity_id])):
try:
yield ctx.call_entity(entity_id, "fail")
except task.TaskFailedError:
exception_count += 1
try:
yield ctx.call_entity(entity_id, "fail")
except task.TaskFailedError:
exception_count += 1
return exception_count
# Start a worker, which will connect to the sidecar in a background thread
with DurableTaskSchedulerWorker(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None) as w:
w.add_orchestrator(test_orchestrator)
w.add_entity(failing_entity)
w.start()
c = DurableTaskSchedulerClient(host_address=endpoint, secure_channel=True,
taskhub=taskhub_name, token_credential=None)
id = c.schedule_new_orchestration(test_orchestrator)
state = c.wait_for_orchestration_completion(id, timeout=30)
assert state is not None
assert state.name == task.get_name(test_orchestrator)
assert state.instance_id == id
assert state.failure_details is None
assert state.serialized_output is not None
output = json.loads(state.serialized_output)
assert output == 2
assert state.runtime_status == client.OrchestrationStatus.COMPLETED