Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions aws_lambda_powertools/utilities/idempotency/idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@

import functools
import logging
import os
import sys
import warnings
from inspect import isclass, unwrap
from typing import TYPE_CHECKING, Any, Protocol, TypeVar, cast, get_type_hints

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.functions import strtobool
from aws_lambda_powertools.shared.functions import resolve_bool_env_var
from aws_lambda_powertools.shared.types import AnyCallableT
from aws_lambda_powertools.utilities.idempotency.base import IdempotencyHandler
from aws_lambda_powertools.utilities.idempotency.config import IdempotencyConfig
Expand Down Expand Up @@ -107,7 +106,7 @@ def handler(event, context):

# Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value
# Raises a warning if not running in development mode
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")):
if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV):
warnings.warn(
message="Disabling idempotency is intended for development environments only "
"and should not be used in production.",
Expand Down Expand Up @@ -245,7 +244,7 @@ def process_order(customer_id: str, order: dict, **kwargs):
def decorate(*args, **kwargs):
# Skip idempotency controls when POWERTOOLS_IDEMPOTENCY_DISABLED has a truthy value
# Raises a warning if not running in development mode
if strtobool(os.getenv(constants.IDEMPOTENCY_DISABLED_ENV, "false")):
if resolve_bool_env_var(constants.IDEMPOTENCY_DISABLED_ENV):
warnings.warn(
message="Disabling idempotency is intended for development environments only "
"and should not be used in production.",
Expand Down
38 changes: 38 additions & 0 deletions tests/functional/idempotency/_boto3/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -1892,6 +1892,44 @@ def dummy_handler(event, context):
assert len(persistence_store.client.method_calls) == 4


@pytest.mark.parametrize("idempotency_disabled_value", ["", " ", "maybe", None])
def test_idempotency_disabled_envvar_empty_or_unparsable(
monkeypatch,
lambda_context,
persistence_store: DynamoDBPersistenceLayer,
idempotency_disabled_value,
):
# Scenario to validate an empty, unset or unparsable 'POWERTOOLS_IDEMPOTENCY_DISABLED'
# keeps idempotency enabled instead of crashing the invocation
mock_event = {"data": "value"}

persistence_store.client = MagicMock()

if idempotency_disabled_value is None:
monkeypatch.delenv("POWERTOOLS_IDEMPOTENCY_DISABLED", raising=False)
else:
monkeypatch.setenv("POWERTOOLS_IDEMPOTENCY_DISABLED", idempotency_disabled_value)

@idempotent_function(data_keyword_argument="data", persistence_store=persistence_store)
def dummy(data):
return {"message": "hello"}

@idempotent(persistence_store=persistence_store)
def dummy_handler(event, context):
return {"message": "hi"}

if idempotency_disabled_value is None:
assert dummy(data=mock_event) == {"message": "hello"}
assert dummy_handler(mock_event, lambda_context) == {"message": "hi"}
else:
with pytest.warns(PowertoolsUserWarning, match="unrecognised value"):
assert dummy(data=mock_event) == {"message": "hello"}
with pytest.warns(PowertoolsUserWarning, match="unrecognised value"):
assert dummy_handler(mock_event, lambda_context) == {"message": "hi"}

assert len(persistence_store.client.method_calls) == 4


@pytest.mark.parametrize("idempotency_config", [{"use_local_cache": True}], indirect=True)
def test_idempotent_function_duplicates(
idempotency_config: IdempotencyConfig,
Expand Down
Loading