Skip to content

Commit 7ed3af2

Browse files
committed
wip
1 parent 9eeb8b4 commit 7ed3af2

File tree

2 files changed

+72
-61
lines changed

2 files changed

+72
-61
lines changed

e2e_batch/test_e2e_batch.py

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33
from utils import (
44
upload_file_to_s3,
55
get_file_content_from_s3,
6-
wait_for_ack_file,
76
check_ack_file_content,
87
validate_row_count,
98
generate_csv_files,
10-
SeedTestData
9+
SeedTestData,
10+
TestData,
11+
poll_destination
1112
)
1213

1314
from constants import (
1415
SOURCE_BUCKET,
1516
INPUT_PREFIX,
16-
ACK_BUCKET,
1717
environment
1818
)
1919

@@ -22,20 +22,6 @@
2222
DELETE = "DELETE"
2323

2424

25-
ods_vaccines = {
26-
"DPSFULL": ["3IN1", "COVID19", "FLU", "HPV", "MENACWY", "MMR", "RSV"],
27-
"DPSREDUCED": ["3IN1", "COVID19", "FLU", "HPV", "MENACWY", "MMR", "RSV"],
28-
"V0V8L": ["3IN1", "FLU", "HPV", "MENACWY", "MMR"],
29-
"8HK48": ["FLU"],
30-
"8HA94": ["COVID19"],
31-
"X26": ["MMR", "RSV"],
32-
"X8E5B": ["MMR", "RSV"],
33-
"YGM41": ["3IN1", "COVID19", "HPV", "MENACWY", "MMR", "RSV"],
34-
"YGJ": ["3IN1", "COVID19", "HPV", "MENACWY", "MMR", "RSV"],
35-
"YGA": ["3IN1", "HPV", "MENACWY", "MMR", "RSV"],
36-
"YGMYW": ["3IN1", "HPV", "MENACWY", "MMR", "RSV"],
37-
}
38-
3925
seed_datas = [
4026
SeedTestData("Create", "V0V8L", [CREATE]),
4127
SeedTestData("Update", "8HK48", [CREATE, UPDATE]),
@@ -50,39 +36,52 @@
5036

5137
class TestE2EBatch(unittest.TestCase):
5238

53-
@unittest.skipIf(environment == "ref")
39+
@unittest.skipIf(environment == "ref", "Skip for ref")
5440
def test_create_success(self):
5541
"""Test CREATE scenario."""
42+
max_timeout = 1200 # seconds
5643

57-
test_datas = generate_csv_files(seed_datas)
44+
test_datas: list[TestData] = generate_csv_files(seed_datas)
5845

5946
for test in test_datas:
47+
6048
key = upload_file_to_s3(test.file_name, SOURCE_BUCKET, INPUT_PREFIX)
6149
test.key = key
6250

63-
process_acks_as_received(test_datas, ACK_BUCKET)
64-
65-
66-
def process_acks_as_received(test_datas, ack_bucket, poll_interval=2, timeout=1200):
67-
"""
68-
Polls for ACK files and processes them as soon as they are available.
69-
"""
70-
start_time = time.time()
71-
pending = {test.file_name: test for test in test_datas}
72-
processed = set()
73-
74-
while pending and (time.time() - start_time) < timeout:
75-
for file_name in list(pending.keys()):
76-
ack_key = wait_for_ack_file(None, file_name, ack_bucket, timeout=1)
77-
if ack_key:
78-
# Process the ACK immediately
79-
validate_row_count(file_name, ack_key)
80-
ack_content = get_file_content_from_s3(ack_bucket, ack_key)
81-
check_ack_file_content(ack_content, "OK", None, "CREATE")
82-
processed.add(file_name)
83-
del pending[file_name]
84-
if pending:
85-
time.sleep(poll_interval)
86-
87-
if pending:
88-
raise TimeoutError(f"Timeout waiting for ACKs: {list(pending.keys())}")
51+
# dictionary of file name to track whether inf and bus acks have been received
52+
pending = {test.file_name: {"inf": True, "bus": True} for test in test_datas}
53+
54+
start_time = time.time()
55+
# while there are still pending files, poll for acks and forwarded files
56+
while pending:
57+
for file_name in list(pending.keys()):
58+
test = pending[file_name]
59+
for key in ["inf", "bus"]:
60+
if test[key]:
61+
inf_key = poll_destination(file_name, check_ack=test.check_ack)
62+
if inf_key:
63+
test[key] = False
64+
for file_name in list(pending.keys()):
65+
test = pending[file_name]
66+
# if both inf and bus are False, remove from pending
67+
if not test["inf"] and not test["bus"]:
68+
del pending[file_name]
69+
70+
# if max_timeout exceeded, break
71+
if (time.time() - start_time) > max_timeout:
72+
break
73+
74+
if pending:
75+
time.sleep(5)
76+
77+
# Now validate all files have been processed correctly
78+
for test in test_datas:
79+
# Validate the ACK file
80+
ack_content = get_file_content_from_s3(environment.ACK_BUCKET, test.file_name)
81+
fwd_content = get_file_content_from_s3(environment.FORWARDEDFILE_BUCKET, test.fwd_key)
82+
83+
check_ack_file_content(ack_content, "OK", None, test.action)
84+
validate_row_count(test.file_name, test.key)
85+
# Validate the forwarded file
86+
validate_row_count(test.file_name, test.key)
87+
check_ack_file_content(fwd_content, "OK", None, test.action)

e2e_batch/utils.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,44 @@
2626
HEADER_RESPONSE_CODE_COLUMN,
2727
)
2828

29+
ods_vaccines = {
30+
"DPSFULL": ["3IN1", "COVID19", "FLU", "HPV", "MENACWY", "MMR", "RSV"],
31+
"DPSREDUCED": ["3IN1", "COVID19", "FLU", "HPV", "MENACWY", "MMR", "RSV"],
32+
"V0V8L": ["3IN1", "FLU", "HPV", "MENACWY", "MMR"],
33+
"8HK48": ["FLU"],
34+
"8HA94": ["COVID19"],
35+
"X26": ["MMR", "RSV"],
36+
"X8E5B": ["MMR", "RSV"],
37+
"YGM41": ["3IN1", "COVID19", "HPV", "MENACWY", "MMR", "RSV"],
38+
"YGJ": ["3IN1", "COVID19", "HPV", "MENACWY", "MMR", "RSV"],
39+
"YGA": ["3IN1", "HPV", "MENACWY", "MMR", "RSV"],
40+
"YGMYW": ["3IN1", "HPV", "MENACWY", "MMR", "RSV"],
41+
}
42+
2943

3044
class SeedTestData:
3145
def __init__(self, description, vax_ods, actions: list, header="NHS_NUMBER",
32-
success: bool = True, dose_amount=0.5, inject_char=False, version=4):
46+
success: bool = True, dose_amount=0.5, inject_char=False, version=4,
47+
check_ack=False):
3348
self.description = description
3449
self.dose_amount = dose_amount
3550
self.actions = actions
36-
self.vax = vax_ods.key()
37-
self.ods = vax_ods.value()[0] # Use the first ODS code for the vaccine
51+
self.vax = ods_vaccines[vax_ods][0] # Use the first vaccine for the ods
52+
self.ods = vax_ods
3853
self.success = success
3954
self.header = header
4055
self.inject_char = inject_char
4156
self.version = version
57+
self.check_ack = check_ack
4258

4359

4460
class TestData:
45-
def __init__(self, file_name, success, description, actions):
61+
def __init__(self, file_name, success, description, action):
4662
self.file_name = file_name
4763
self.success = success
4864
self.description = description
49-
self.actions = actions
50-
self.key = None # To be set when the file is uploaded to S3
65+
self.action = action
66+
self.key = None
5167

5268

5369
def generate_csv(dose_amount, action_flag, headers="NHS_NUMBER", same_id=False, version="4",
@@ -181,16 +197,14 @@ def wait_for_ack_file(ack_prefix, input_file_name, timeout=1200):
181197
)
182198

183199

184-
def poll_ack_file(ack_prefix, input_file_name):
200+
def poll_destination(input_file_name, check_ack: bool = False):
185201
"""Poll the ACK_BUCKET for an ack file that contains the input_file_name as a substring."""
186202

187203
filename_without_ext = input_file_name[:-4] if input_file_name.endswith(".csv") else input_file_name
188-
if ack_prefix:
189-
search_pattern = f"{ACK_PREFIX}{filename_without_ext}"
190-
ack_prefix = ACK_PREFIX
191-
else:
192-
search_pattern = f"{FORWARDEDFILE_PREFIX}{filename_without_ext}"
193-
ack_prefix = FORWARDEDFILE_PREFIX
204+
205+
ack_prefix = ACK_PREFIX if check_ack else FORWARDEDFILE_PREFIX
206+
207+
search_pattern = f"{ack_prefix}{filename_without_ext}"
194208
return poll_s3_file_pattern(ack_prefix, search_pattern)
195209

196210

@@ -529,9 +543,7 @@ def generate_csv_files(seed_data_list: list[SeedTestData]) -> list[TestData]:
529543
vax_type=seed_data.vax,
530544
ods=seed_data.ods,
531545
)
532-
test_data.append(
533-
TestData(file_name, seed_data.success, seed_data.description, seed_data.actions)
534-
)
546+
test_data.append(TestData(file_name, seed_data.success, seed_data.description, action))
535547
return test_data
536548

537549

0 commit comments

Comments
 (0)