12
12
13
13
from aws_lambda_powertools .utilities .data_classes import APIGatewayProxyEventV2 , event_source
14
14
from aws_lambda_powertools .utilities .idempotency import DynamoDBPersistenceLayer , IdempotencyConfig
15
- from aws_lambda_powertools .utilities .idempotency .base import MAX_RETRIES , _prepare_data
15
+ from aws_lambda_powertools .utilities .idempotency .base import MAX_RETRIES , IdempotencyHandler , _prepare_data
16
16
from aws_lambda_powertools .utilities .idempotency .exceptions import (
17
17
IdempotencyAlreadyInProgressError ,
18
18
IdempotencyInconsistentStateError ,
@@ -692,7 +692,7 @@ def test_idempotent_lambda_expires_in_progress_before_expire(
692
692
693
693
now = datetime .datetime .now ()
694
694
period = datetime .timedelta (seconds = 5 )
695
- timestamp_expires_in_progress = str ( int ((now + period ).timestamp ()) )
695
+ timestamp_expires_in_progress = int ((now + period ).timestamp () * 1000 )
696
696
697
697
expected_params_get_item = {
698
698
"TableName" : TABLE_NAME ,
@@ -703,7 +703,7 @@ def test_idempotent_lambda_expires_in_progress_before_expire(
703
703
"Item" : {
704
704
"id" : {"S" : hashed_idempotency_key },
705
705
"expiration" : {"N" : timestamp_future },
706
- "in_progress_expiration" : {"N" : timestamp_expires_in_progress },
706
+ "in_progress_expiration" : {"N" : str ( timestamp_expires_in_progress ) },
707
707
"data" : {"S" : '{"message": "test", "statusCode": 200' },
708
708
"status" : {"S" : "INPROGRESS" },
709
709
}
@@ -748,7 +748,7 @@ def test_idempotent_lambda_expires_in_progress_after_expire(
748
748
"Item" : {
749
749
"id" : {"S" : hashed_idempotency_key },
750
750
"expiration" : {"N" : timestamp_future },
751
- "in_progress_expiration" : {"N" : str (int (one_second_ago .timestamp ()))},
751
+ "in_progress_expiration" : {"N" : str (int (one_second_ago .timestamp () * 1000 ))},
752
752
"data" : {"S" : '{"message": "test", "statusCode": 200' },
753
753
"status" : {"S" : "INPROGRESS" },
754
754
}
@@ -816,6 +816,62 @@ def test_data_record_json_to_dict_mapping_when_response_data_none():
816
816
assert response_data is None
817
817
818
818
819
+ @pytest .mark .parametrize ("idempotency_config" , [{"use_local_cache" : True }], indirect = True )
820
+ def test_handler_for_status_expired_data_record (
821
+ idempotency_config : IdempotencyConfig , persistence_store : DynamoDBPersistenceLayer
822
+ ):
823
+ idempotency_handler = IdempotencyHandler (
824
+ function = lambda a : a ,
825
+ function_payload = {},
826
+ config = idempotency_config ,
827
+ persistence_store = persistence_store ,
828
+ )
829
+ data_record = DataRecord ("key" , status = "EXPIRED" , response_data = None )
830
+
831
+ with pytest .raises (IdempotencyInconsistentStateError ):
832
+ idempotency_handler ._handle_for_status (data_record )
833
+
834
+
835
+ @pytest .mark .parametrize ("idempotency_config" , [{"use_local_cache" : True }], indirect = True )
836
+ def test_handler_for_status_inprogress_data_record_inconsistent (
837
+ idempotency_config : IdempotencyConfig , persistence_store : DynamoDBPersistenceLayer
838
+ ):
839
+ idempotency_handler = IdempotencyHandler (
840
+ function = lambda a : a ,
841
+ function_payload = {},
842
+ config = idempotency_config ,
843
+ persistence_store = persistence_store ,
844
+ )
845
+
846
+ now = datetime .datetime .now ()
847
+ period = datetime .timedelta (milliseconds = 100 )
848
+ timestamp = int ((now - period ).timestamp () * 1000 )
849
+ data_record = DataRecord ("key" , in_progress_expiry_timestamp = timestamp , status = "INPROGRESS" , response_data = None )
850
+
851
+ with pytest .raises (IdempotencyInconsistentStateError ):
852
+ idempotency_handler ._handle_for_status (data_record )
853
+
854
+
855
+ @pytest .mark .parametrize ("idempotency_config" , [{"use_local_cache" : True }], indirect = True )
856
+ def test_handler_for_status_inprogress_data_record_consistent (
857
+ idempotency_config : IdempotencyConfig , persistence_store : DynamoDBPersistenceLayer
858
+ ):
859
+ idempotency_handler = IdempotencyHandler (
860
+ function = lambda a : a ,
861
+ function_payload = {},
862
+ config = idempotency_config ,
863
+ persistence_store = persistence_store ,
864
+ )
865
+
866
+ now = datetime .datetime .now ()
867
+ period = datetime .timedelta (milliseconds = 100 )
868
+ timestamp = int ((now + period ).timestamp () * 1000 )
869
+ data_record = DataRecord ("key" , in_progress_expiry_timestamp = timestamp , status = "INPROGRESS" , response_data = None )
870
+
871
+ with pytest .raises (IdempotencyAlreadyInProgressError ):
872
+ idempotency_handler ._handle_for_status (data_record )
873
+
874
+
819
875
@pytest .mark .parametrize ("idempotency_config" , [{"use_local_cache" : True }], indirect = True )
820
876
def test_in_progress_never_saved_to_cache (
821
877
idempotency_config : IdempotencyConfig , persistence_store : DynamoDBPersistenceLayer
0 commit comments