|
3 | 3 |
|
4 | 4 | """Tests for large-payload externalization and de-externalization.""" |
5 | 5 |
|
6 | | -from unittest.mock import MagicMock |
| 6 | +import asyncio |
| 7 | +import gzip |
| 8 | +import threading |
| 9 | +from unittest.mock import AsyncMock, MagicMock |
7 | 10 |
|
8 | 11 | import pytest |
9 | 12 | from google.protobuf import wrappers_pb2 |
@@ -433,6 +436,72 @@ def test_is_known_token(self): |
433 | 436 | # ------------------------------------------------------------------ |
434 | 437 |
|
435 | 438 |
|
| 439 | +@pytest.mark.asyncio |
| 440 | +@pytest.mark.parametrize("operation", ["upload", "download"]) |
| 441 | +@pytest.mark.parametrize("enable_compression", [False, True]) |
| 442 | +async def test_async_blob_compression_yields(monkeypatch, operation, enable_compression): |
| 443 | + pytest.importorskip("azure.storage.blob") |
| 444 | + from durabletask.extensions.azure_blob_payloads import blob_payload_store as module |
| 445 | + from durabletask.extensions.azure_blob_payloads import BlobPayloadStoreOptions |
| 446 | + |
| 447 | + data = b"payload" * 1000 |
| 448 | + stored_data = gzip.compress(data) if enable_compression else data |
| 449 | + loop = asyncio.get_running_loop() |
| 450 | + loop_thread = threading.get_ident() |
| 451 | + entered = asyncio.Event() |
| 452 | + release = threading.Event() |
| 453 | + gzip_method = "compress" if operation == "upload" else "decompress" |
| 454 | + original = getattr(gzip, gzip_method) |
| 455 | + |
| 456 | + def gated_gzip(value): |
| 457 | + assert enable_compression |
| 458 | + assert threading.get_ident() != loop_thread |
| 459 | + loop.call_soon_threadsafe(entered.set) |
| 460 | + assert release.wait(5), "Event loop did not release compression" |
| 461 | + return original(value) |
| 462 | + |
| 463 | + async def upload_blob(**kwargs): |
| 464 | + assert threading.get_ident() == loop_thread |
| 465 | + |
| 466 | + async def readall(): |
| 467 | + assert threading.get_ident() == loop_thread |
| 468 | + return stored_data |
| 469 | + |
| 470 | + stream = MagicMock() |
| 471 | + stream.readall = AsyncMock(side_effect=readall) |
| 472 | + container = MagicMock() |
| 473 | + container.create_container = AsyncMock() |
| 474 | + container.upload_blob = AsyncMock(side_effect=upload_blob) |
| 475 | + container.download_blob = AsyncMock(return_value=stream) |
| 476 | + service = MagicMock() |
| 477 | + service.get_container_client.return_value = container |
| 478 | + service.close = AsyncMock() |
| 479 | + monkeypatch.setattr(module, "BlobServiceClient", MagicMock()) |
| 480 | + monkeypatch.setattr(module, "AsyncBlobServiceClient", MagicMock(return_value=service)) |
| 481 | + monkeypatch.setattr(gzip, gzip_method, gated_gzip) |
| 482 | + store = module.BlobPayloadStore(BlobPayloadStoreOptions( |
| 483 | + account_url="https://example.blob.core.windows.net", enable_compression=enable_compression)) |
| 484 | + transfer = asyncio.create_task(store.upload_async(data) if operation == "upload" |
| 485 | + else store.download_async("blob:v1:durabletask-payloads:example")) |
| 486 | + try: |
| 487 | + if enable_compression: |
| 488 | + await asyncio.wait_for(entered.wait(), timeout=2) |
| 489 | + assert not transfer.done() |
| 490 | + finally: |
| 491 | + release.set() |
| 492 | + try: |
| 493 | + result = await transfer |
| 494 | + finally: |
| 495 | + store.close() |
| 496 | + await store.close_async() |
| 497 | + if operation == "upload": |
| 498 | + uploaded = container.upload_blob.call_args.kwargs["data"] |
| 499 | + assert (gzip.decompress(uploaded) if enable_compression else uploaded) == data |
| 500 | + assert store.is_known_token(result) |
| 501 | + else: |
| 502 | + assert result == data |
| 503 | + |
| 504 | + |
436 | 505 | class TestBlobPayloadStoreDefaults: |
437 | 506 | def test_default_options(self): |
438 | 507 | """Constructing with connection_string should use .NET SDK defaults.""" |
|
0 commit comments