Skip to content

Commit 1e65fa9

Browse files
DS-392 Flaky Integrationtest Fix (#1044)
# Task Branch Pull Request **<https://nhsd-jira.digital.nhs.uk/browse/DS-392>** ## Description of Changes Please include a summary of the change ## Type of change Delete not appropriate - Bug fix (non-breaking change which fixes an issue) - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) - Refactoring (non-breaking change which improves the structure of the code) ## Development Checklist - [x] I have performed a self-review of my own code - [x] Tests have added that prove my fix is effective or that my feature works (Integration tests) - [x] I have updated Dependabot to include my changes (if applicable) ## Code Reviewer Checklist - [x] I can confirm the changes have been tested or approved by a tester
1 parent ef6c2b6 commit 1e65fa9

3 files changed

Lines changed: 18 additions & 4 deletions

File tree

test/integration/steps/functions/aws/cloudwatch.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import random
12
from datetime import datetime
23
from json import dumps
34
from os import getenv
@@ -15,8 +16,8 @@ def get_logs(
1516
query: str,
1617
lambda_name: str,
1718
start_time: Timestamp | None = None,
18-
retry_count: int = 32,
19-
sleep_per_loop: int = 20,
19+
retry_count: int = 32, # Retry Count
20+
sleep_per_loop: int = 20, # Sleep time between retries.
2021
) -> str:
2122
"""Get logs from CloudWatch.
2223
@@ -44,15 +45,19 @@ def get_logs(
4445
)
4546
except ClientError as error:
4647
if error.response["Error"]["Code"] == "LimitExceededException":
47-
limit_exceeded_timeout += 30
48+
limit_exceeded_timeout += random.uniform(30, 60) * (2**counter) # Exponential backoff with jitter
49+
print(f"Limit exceeded, retrying after {limit_exceeded_timeout} seconds.")
4850
sleep(limit_exceeded_timeout)
4951
continue
5052
raise
5153
query_id = start_query_response["queryId"]
5254
response = None
55+
56+
# Wait for the query to complete without a max timeout
5357
while response is None or response["status"] != "Complete":
5458
sleep(sleep_per_loop)
5559
response = LAMBDA_CLIENT_LOGS.get_query_results(queryId=query_id)
60+
5661
counter += 1
5762
if response["results"] != []:
5863
logs_found = True

test/integration/steps/functions/utils.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import secrets
2+
import string
13
from json import dumps, loads
24
from os import getenv
35
from random import randint, randrange
@@ -274,3 +276,9 @@ def quality_checker_negative_log_check(
274276
| filter message="{reason}"
275277
| sort @timestamp asc"""
276278
return negative_log_check(query=query, event_lambda="quality-checker", start_time=start_time)
279+
280+
281+
def generate_unique_ods_code() -> str:
282+
"""Generate a unique 5-character uppercase alphanumeric ODSCode."""
283+
chars = string.ascii_uppercase + string.digits # A-Z, 0-9
284+
return "".join(secrets.choice(chars) for _ in range(5)) # 5-character random string

test/integration/steps/test_steps.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
create_pending_change_for_service,
7171
generate_correlation_id,
7272
generate_random_int,
73+
generate_unique_ods_code,
7374
get_address_string,
7475
get_expected_data,
7576
quality_checker_log_check,
@@ -136,7 +137,7 @@ def a_service_table_entry_is_created(context: Context, ods_code: int = 0, servic
136137
Context: The context object.
137138
"""
138139
if ods_code == 0:
139-
ods_code = str(randint(10000, 99999))
140+
ods_code = generate_unique_ods_code()
140141
query_values = {
141142
"uid": f"test{randint(10000, 99999)!s}",
142143
"service_type": service_type,

0 commit comments

Comments
 (0)