|
| 1 | +import json |
| 2 | +from time import sleep |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from tests.e2e.utils import data_fetcher |
| 7 | +from tests.e2e.utils.functions import execute_lambdas_in_parallel |
| 8 | + |
| 9 | + |
| 10 | +@pytest.fixture |
| 11 | +def ttl_cache_expiration_handler_fn_arn(infrastructure: dict) -> str: |
| 12 | + return infrastructure.get("TtlCacheExpirationHandlerArn", "") |
| 13 | + |
| 14 | + |
| 15 | +@pytest.fixture |
| 16 | +def ttl_cache_timeout_handler_fn_arn(infrastructure: dict) -> str: |
| 17 | + return infrastructure.get("TtlCacheTimeoutHandlerArn", "") |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def parallel_execution_handler_fn_arn(infrastructure: dict) -> str: |
| 22 | + return infrastructure.get("ParallelExecutionHandlerArn", "") |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def idempotency_table_name(infrastructure: dict) -> str: |
| 27 | + return infrastructure.get("DynamoDBTable", "") |
| 28 | + |
| 29 | + |
| 30 | +def test_ttl_caching_expiration_idempotency(ttl_cache_expiration_handler_fn_arn: str): |
| 31 | + # GIVEN |
| 32 | + payload = json.dumps({"message": "Lambda Powertools - TTL 20s"}) |
| 33 | + |
| 34 | + # WHEN |
| 35 | + # first execution |
| 36 | + first_execution, _ = data_fetcher.get_lambda_response( |
| 37 | + lambda_arn=ttl_cache_expiration_handler_fn_arn, payload=payload |
| 38 | + ) |
| 39 | + first_execution_response = first_execution["Payload"].read().decode("utf-8") |
| 40 | + |
| 41 | + # the second execution should return the same response as the first execution |
| 42 | + second_execution, _ = data_fetcher.get_lambda_response( |
| 43 | + lambda_arn=ttl_cache_expiration_handler_fn_arn, payload=payload |
| 44 | + ) |
| 45 | + second_execution_response = second_execution["Payload"].read().decode("utf-8") |
| 46 | + |
| 47 | + # wait 20s to expire ttl and execute again, this should return a new response value |
| 48 | + sleep(20) |
| 49 | + third_execution, _ = data_fetcher.get_lambda_response( |
| 50 | + lambda_arn=ttl_cache_expiration_handler_fn_arn, payload=payload |
| 51 | + ) |
| 52 | + third_execution_response = third_execution["Payload"].read().decode("utf-8") |
| 53 | + |
| 54 | + # THEN |
| 55 | + assert first_execution_response == second_execution_response |
| 56 | + assert third_execution_response != second_execution_response |
| 57 | + |
| 58 | + |
| 59 | +def test_ttl_caching_timeout_idempotency(ttl_cache_timeout_handler_fn_arn: str): |
| 60 | + # GIVEN |
| 61 | + payload_timeout_execution = json.dumps({"sleep": 10, "message": "Lambda Powertools - TTL 1s"}) |
| 62 | + payload_working_execution = json.dumps({"sleep": 0, "message": "Lambda Powertools - TTL 1s"}) |
| 63 | + |
| 64 | + # WHEN |
| 65 | + # first call should fail due to timeout |
| 66 | + execution_with_timeout, _ = data_fetcher.get_lambda_response( |
| 67 | + lambda_arn=ttl_cache_timeout_handler_fn_arn, payload=payload_timeout_execution |
| 68 | + ) |
| 69 | + execution_with_timeout_response = execution_with_timeout["Payload"].read().decode("utf-8") |
| 70 | + |
| 71 | + # the second call should work and return the payload |
| 72 | + execution_working, _ = data_fetcher.get_lambda_response( |
| 73 | + lambda_arn=ttl_cache_timeout_handler_fn_arn, payload=payload_working_execution |
| 74 | + ) |
| 75 | + execution_working_response = execution_working["Payload"].read().decode("utf-8") |
| 76 | + |
| 77 | + # THEN |
| 78 | + assert "Task timed out after" in execution_with_timeout_response |
| 79 | + assert payload_working_execution == execution_working_response |
| 80 | + |
| 81 | + |
| 82 | +def test_parallel_execution_idempotency(parallel_execution_handler_fn_arn: str): |
| 83 | + # GIVEN |
| 84 | + arguments = json.dumps({"message": "Lambda Powertools - Parallel execution"}) |
| 85 | + |
| 86 | + # WHEN |
| 87 | + # executing Lambdas in parallel |
| 88 | + lambdas_arn = [parallel_execution_handler_fn_arn, parallel_execution_handler_fn_arn] |
| 89 | + execution_result_list = execute_lambdas_in_parallel("data_fetcher.get_lambda_response", lambdas_arn, arguments) |
| 90 | + |
| 91 | + timeout_execution_response = execution_result_list[0][0]["Payload"].read().decode("utf-8") |
| 92 | + error_idempotency_execution_response = execution_result_list[1][0]["Payload"].read().decode("utf-8") |
| 93 | + |
| 94 | + # THEN |
| 95 | + assert "Execution already in progress with idempotency key" in error_idempotency_execution_response |
| 96 | + assert "Task timed out after" in timeout_execution_response |
0 commit comments