1
1
# ruff: noqa
2
2
import copy
3
+ import datetime
3
4
import json
4
5
import time as t
6
+ from unittest import mock
5
7
6
8
import pytest
7
- from unittest .mock import patch
8
-
9
- from aws_lambda_powertools .utilities .idempotency .persistence .redis import (
10
- RedisCachePersistenceLayer ,
11
- )
12
- import datetime
9
+ from multiprocess import Lock , Manager , Process
13
10
14
- from aws_lambda_powertools .utilities .idempotency .persistence .base import (
15
- STATUS_CONSTANTS ,
16
- DataRecord ,
17
- )
18
-
19
- from unittest import mock
20
- from multiprocessing import Process , Manager , Lock
21
11
from aws_lambda_powertools .utilities .idempotency .exceptions import (
22
12
IdempotencyAlreadyInProgressError ,
23
13
IdempotencyItemAlreadyExistsError ,
24
14
IdempotencyItemNotFoundError ,
25
- IdempotencyPersistenceConnectionError ,
26
15
IdempotencyPersistenceConfigError ,
16
+ IdempotencyPersistenceConnectionError ,
27
17
IdempotencyPersistenceConsistencyError ,
28
18
IdempotencyValidationError ,
29
19
)
30
20
from aws_lambda_powertools .utilities .idempotency .idempotency import (
21
+ IdempotencyConfig ,
31
22
idempotent ,
32
23
idempotent_function ,
33
- IdempotencyConfig ,
24
+ )
25
+ from aws_lambda_powertools .utilities .idempotency .persistence .base import (
26
+ STATUS_CONSTANTS ,
27
+ DataRecord ,
28
+ )
29
+ from aws_lambda_powertools .utilities .idempotency .persistence .redis import (
30
+ RedisCachePersistenceLayer ,
34
31
)
35
32
36
33
redis_badhost = "badhost"
@@ -557,6 +554,7 @@ def test_redis_orphan_record_race_condition(lambda_context):
557
554
port = "63005" ,
558
555
mock_latency_ms = 50 ,
559
556
)
557
+
560
558
manager = Manager ()
561
559
# use a thread safe dict
562
560
redis_client .expire_dict = manager .dict ()
@@ -576,11 +574,13 @@ def lambda_handler(event, context):
576
574
577
575
# run handler for the first time to create a valid record in cache
578
576
lambda_handler (mock_event , lambda_context )
577
+
579
578
# modify the cache expiration to create the orphan record
580
579
for key , item in redis_client .cache .items ():
581
580
json_dict = json .loads (item )
582
581
json_dict ["expiration" ] = int (t .time ()) - 4000
583
582
redis_client .cache [key ] = json .dumps (json_dict ).encode ()
583
+
584
584
# Given orphan idempotency record with same payload already in Redis
585
585
# When running two lambda handler at the same time
586
586
redis_client .cache ["exec_count" ] = 0
@@ -590,6 +590,7 @@ def lambda_handler(event, context):
590
590
p2 .start ()
591
591
p1 .join ()
592
592
p2 .join ()
593
+
593
594
# Then only one handler will actually run
594
595
assert redis_client .cache ["exec_count" ] == 1
595
596
0 commit comments