forked from microsoft/durabletask-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_large_payload_e2e.py
More file actions
417 lines (317 loc) · 16 KB
/
Copy pathtest_large_payload_e2e.py
File metadata and controls
417 lines (317 loc) · 16 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
"""End-to-end tests for large-payload externalization using Azurite.
These tests spin up a real in-memory Durable Task backend *and* a real
``BlobPayloadStore`` backed by Azurite (local Azure Storage emulator).
They verify that payloads too large for inline gRPC messages are
transparently offloaded to blob storage and recovered on the other side.
Prerequisites:
- Azurite must be running locally on the default blob port (10000).
Start it with: ``azurite --silent --blobPort 10000``
- ``azure-storage-blob`` must be installed.
"""
import json
import uuid
import pytest
from durabletask import client, task, worker
from durabletask.testing import create_test_backend
from _port_utils import find_free_port
# Skip the entire module if azure-storage-blob is not installed.
azure_blob = pytest.importorskip("azure.storage.blob")
from durabletask.extensions.azure_blob_payloads import BlobPayloadStore, BlobPayloadStoreOptions # noqa: E402
# Azurite well-known connection string
AZURITE_CONN_STR = "UseDevelopmentStorage=true"
BACKEND_PORT = find_free_port()
HOST = f"localhost:{BACKEND_PORT}"
# Use a unique container per test run to avoid collisions.
TEST_CONTAINER = f"e2e-payloads-{uuid.uuid4().hex[:8]}"
# A low threshold so we can trigger externalization without massive strings.
# In production the default is 900 KB; here we use 1 KB for fast tests.
THRESHOLD_BYTES = 1_024
# Pin API version to one that Azurite supports.
AZURITE_API_VERSION = "2024-08-04"
# ------------------------------------------------------------------
# Fixtures
# ------------------------------------------------------------------
def _azurite_is_running() -> bool:
"""Return True if Azurite blob service is reachable."""
try:
svc = azure_blob.BlobServiceClient.from_connection_string(
AZURITE_CONN_STR, api_version=AZURITE_API_VERSION,
)
# list_containers is a lightweight call that works with Azurite's
# well-known credentials without any special permissions.
next(iter(svc.list_containers(results_per_page=1)), None)
return True
except Exception:
return False
# Skip all tests if Azurite is not reachable.
pytestmark = [
pytest.mark.azurite,
pytest.mark.skipif(
not _azurite_is_running(),
reason="Azurite blob service is not running on 127.0.0.1:10000",
),
]
@pytest.fixture(scope="module")
def payload_store():
"""Create a BlobPayloadStore pointing at Azurite with a low threshold."""
store = BlobPayloadStore(BlobPayloadStoreOptions(
connection_string=AZURITE_CONN_STR,
container_name=TEST_CONTAINER,
threshold_bytes=THRESHOLD_BYTES,
enable_compression=True,
api_version=AZURITE_API_VERSION,
))
yield store
# Clean up: delete the test container.
try:
svc = azure_blob.BlobServiceClient.from_connection_string(
AZURITE_CONN_STR, api_version=AZURITE_API_VERSION,
)
svc.delete_container(TEST_CONTAINER)
except Exception:
pass
@pytest.fixture(autouse=True)
def backend():
"""Create an in-memory backend for each test."""
b = create_test_backend(port=BACKEND_PORT)
yield b
b.stop()
b.reset()
# ------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------
def _make_large_string(size_kb: int = 2) -> str:
"""Return a JSON-serializable string larger than THRESHOLD_BYTES."""
return "X" * (size_kb * 1024)
def _make_large_payload(size_kb: int = 100) -> dict:
"""Return a dict whose JSON serialization is about *size_kb* KB."""
return {"data": "Y" * (size_kb * 1024)}
# ------------------------------------------------------------------
# Tests
# ------------------------------------------------------------------
class TestLargeInputOutput:
"""Orchestrations whose input/output exceeds the threshold."""
def test_large_input_round_trips(self, payload_store):
"""A large orchestration input is externalized then recovered."""
large_input = _make_large_string(5) # 5 KB > 1 KB threshold
def echo(ctx: task.OrchestrationContext, inp: str):
return inp
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(echo)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(echo, input=large_input)
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
assert json.loads(state.serialized_output) == large_input
def test_large_activity_result(self, payload_store):
"""A large activity return value is externalized then recovered."""
def produce_large(_: task.ActivityContext, size_kb: int) -> str:
return "Z" * (size_kb * 1024)
def orchestrator(ctx: task.OrchestrationContext, size_kb: int):
result = yield ctx.call_activity(produce_large, input=size_kb)
return result
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(orchestrator)
w.add_activity(produce_large)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(orchestrator, input=10) # 10 KB
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
output = json.loads(state.serialized_output)
assert len(output) == 10 * 1024
def test_large_input_and_output(self, payload_store):
"""Both input and output are large — both directions externalize."""
large_input = _make_large_payload(5) # ~5 KB dict
def transform(ctx: task.OrchestrationContext, inp: dict):
return {"echo": inp["data"], "extra": "A" * 3000}
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(transform)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(transform, input=large_input)
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
output = json.loads(state.serialized_output)
assert output["echo"] == large_input["data"]
class TestLargeEvents:
"""External events carrying large payloads."""
def test_large_event_data(self, payload_store):
"""A large external event payload is externalized and resolved."""
large_event = _make_large_string(5)
def wait_for_event(ctx: task.OrchestrationContext, _):
result = yield ctx.wait_for_external_event("big_event")
return result
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(wait_for_event)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(wait_for_event)
c.wait_for_orchestration_start(inst_id, timeout=10)
c.raise_orchestration_event(inst_id, "big_event", data=large_event)
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
assert json.loads(state.serialized_output) == large_event
class TestLargeTerminate:
"""Terminate with a large output payload."""
def test_terminate_with_large_output(self, payload_store):
"""Terminating with a large output externalizes it."""
large_output = _make_large_string(3)
def long_running(ctx: task.OrchestrationContext, _):
yield ctx.wait_for_external_event("never_arrives")
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(long_running)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(long_running)
c.wait_for_orchestration_start(inst_id, timeout=10)
c.terminate_orchestration(inst_id, output=large_output)
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.TERMINATED
class TestMultipleActivitiesLargePayloads:
"""Fan-out/fan-in with multiple large activity results."""
def test_fan_out_fan_in_large_results(self, payload_store):
"""Multiple activities each return large payloads."""
def make_large(_: task.ActivityContext, idx: int) -> str:
return f"result-{idx}-" + ("D" * 2000)
def fan_out(ctx: task.OrchestrationContext, count: int):
tasks = [ctx.call_activity(make_large, input=i) for i in range(count)]
results = yield task.when_all(tasks)
return results
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(fan_out)
w.add_activity(make_large)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(fan_out, input=5)
state = c.wait_for_orchestration_completion(inst_id, timeout=60)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
results = json.loads(state.serialized_output)
assert len(results) == 5
for i, r in enumerate(results):
assert r.startswith(f"result-{i}-")
class TestBlobStorageVerification:
"""Verify blobs actually land in Azurite storage."""
def test_blobs_created_in_container(self, payload_store):
"""After an orchestration with large payloads, blobs exist in the container."""
large_input = _make_large_string(5)
def echo(ctx: task.OrchestrationContext, inp: str):
return inp
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=payload_store) as w:
w.add_orchestrator(echo)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=payload_store) as c:
inst_id = c.schedule_new_orchestration(echo, input=large_input)
c.wait_for_orchestration_completion(inst_id, timeout=30)
# Verify blobs were actually created in the Azurite container
svc = azure_blob.BlobServiceClient.from_connection_string(
AZURITE_CONN_STR, api_version=AZURITE_API_VERSION,
)
container_client = svc.get_container_client(TEST_CONTAINER)
blobs = list(container_client.list_blobs())
assert len(blobs) > 0, "Expected at least one blob in the container"
# All blobs should contain compressed data (GZip header: 1f 8b)
for blob in blobs:
data = container_client.download_blob(blob.name).readall()
assert data[:2] == b"\x1f\x8b", f"Blob {blob.name} is not GZip-compressed"
class TestSmallPayloadNotExternalized:
"""Payloads below the threshold should NOT hit blob storage."""
def test_small_payload_stays_inline(self, payload_store):
"""A small payload should not create any blobs."""
small_input = "hello"
# Use a fresh container to isolate blob count
fresh_container = f"small-test-{uuid.uuid4().hex[:8]}"
store = BlobPayloadStore(BlobPayloadStoreOptions(
connection_string=AZURITE_CONN_STR,
container_name=fresh_container,
threshold_bytes=THRESHOLD_BYTES,
enable_compression=True,
api_version=AZURITE_API_VERSION,
))
def echo(ctx: task.OrchestrationContext, inp: str):
return inp
with worker.TaskHubGrpcWorker(host_address=HOST, payload_store=store) as w:
w.add_orchestrator(echo)
w.start()
with client.TaskHubGrpcClient(host_address=HOST, payload_store=store) as c:
inst_id = c.schedule_new_orchestration(echo, input=small_input)
state = c.wait_for_orchestration_completion(inst_id, timeout=30)
assert state is not None
assert state.runtime_status == client.OrchestrationStatus.COMPLETED
assert state.serialized_output is not None
assert json.loads(state.serialized_output) == small_input
# Verify no blobs were created
svc = azure_blob.BlobServiceClient.from_connection_string(
AZURITE_CONN_STR, api_version=AZURITE_API_VERSION,
)
container_client = svc.get_container_client(fresh_container)
try:
blobs = list(container_client.list_blobs())
assert len(blobs) == 0, f"Expected 0 blobs but found {len(blobs)}"
except Exception:
pass # Container may not even exist — that's fine
finally:
try:
svc.delete_container(fresh_container)
except Exception:
pass
class TestAsyncBlobClient:
"""Test upload_async / download_async against Azurite."""
@pytest.fixture()
def async_store(self):
"""Per-test BlobPayloadStore with a unique container."""
container = f"async-test-{uuid.uuid4().hex[:8]}"
store = BlobPayloadStore(BlobPayloadStoreOptions(
connection_string=AZURITE_CONN_STR,
container_name=container,
threshold_bytes=THRESHOLD_BYTES,
enable_compression=True,
api_version=AZURITE_API_VERSION,
))
yield store
try:
svc = azure_blob.BlobServiceClient.from_connection_string(
AZURITE_CONN_STR, api_version=AZURITE_API_VERSION,
)
svc.delete_container(container)
except Exception:
pass
@pytest.mark.asyncio
async def test_async_upload_and_download_round_trip(self, async_store):
"""upload_async stores data that download_async can retrieve."""
payload = b"async round-trip payload " * 200
token = await async_store.upload_async(payload, instance_id="async-1")
assert async_store.is_known_token(token)
result = await async_store.download_async(token)
assert result == payload
@pytest.mark.asyncio
async def test_async_upload_with_compression(self, async_store):
"""Compressed upload should still decompress on download."""
payload = b"Z" * 5000
token = await async_store.upload_async(payload)
downloaded = await async_store.download_async(token)
assert downloaded == payload
@pytest.mark.asyncio
async def test_async_upload_instance_id_scopes_blob(self, async_store):
"""Blobs uploaded with instance_id are scoped under that prefix."""
payload = b"scoped payload"
token = await async_store.upload_async(payload, instance_id="inst-42")
# Token format: blob:v1:<container>:<instance_id>/<uuid>
_, blob_name = BlobPayloadStore._parse_token(token)
assert blob_name.startswith("inst-42/")
downloaded = await async_store.download_async(token)
assert downloaded == payload