-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfilesystem_store_test.py
More file actions
420 lines (340 loc) · 13.9 KB
/
filesystem_store_test.py
File metadata and controls
420 lines (340 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""Tests for FileSystemExecutionStore."""
import tempfile
from pathlib import Path
import pytest
from aws_durable_execution_sdk_python_testing.exceptions import (
ResourceNotFoundException,
)
from aws_durable_execution_sdk_python_testing.execution import Execution
from aws_durable_execution_sdk_python_testing.model import StartDurableExecutionInput
from aws_durable_execution_sdk_python_testing.stores.filesystem import (
FileSystemExecutionStore,
)
from datetime import datetime, timezone
@pytest.fixture
def temp_storage_dir():
"""Create a temporary directory for testing."""
with tempfile.TemporaryDirectory() as temp_dir:
yield Path(temp_dir)
@pytest.fixture
def store(temp_storage_dir):
"""Create a FileSystemExecutionStore with temporary storage."""
return FileSystemExecutionStore.create(temp_storage_dir)
@pytest.fixture
def sample_execution():
"""Create a sample execution for testing."""
input_data = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="test-execution",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="test-invocation-id",
)
return Execution.new(input_data)
def test_filesystem_execution_store_save_and_load(store, sample_execution):
"""Test saving and loading an execution."""
store.save(sample_execution)
loaded_execution = store.load(sample_execution.durable_execution_arn)
assert (
loaded_execution.durable_execution_arn == sample_execution.durable_execution_arn
)
assert (
loaded_execution.start_input.function_name
== sample_execution.start_input.function_name
)
assert (
loaded_execution.start_input.execution_name
== sample_execution.start_input.execution_name
)
assert loaded_execution.token_sequence == sample_execution.token_sequence
assert loaded_execution.is_complete == sample_execution.is_complete
def test_filesystem_execution_store_load_nonexistent(store):
"""Test loading a nonexistent execution raises ResourceNotFoundException."""
with pytest.raises(
ResourceNotFoundException, match="Execution nonexistent-arn not found"
):
store.load("nonexistent-arn")
def test_filesystem_execution_store_update(store, sample_execution):
"""Test updating an execution."""
store.save(sample_execution)
sample_execution.is_complete = True
for _ in range(5):
sample_execution.get_new_checkpoint_token()
store.update(sample_execution)
loaded_execution = store.load(sample_execution.durable_execution_arn)
assert loaded_execution.is_complete is True
assert loaded_execution.token_sequence == 5
def test_filesystem_execution_store_update_overwrites(store, temp_storage_dir):
"""Test that update overwrites existing execution."""
input_data = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="test-execution",
execution_timeout_seconds=300,
execution_retention_period_days=7,
)
execution1 = Execution.new(input_data)
execution2 = Execution.new(input_data)
execution2.durable_execution_arn = execution1.durable_execution_arn
for _ in range(10):
execution2.get_new_checkpoint_token()
store.save(execution1)
store.update(execution2)
loaded_execution = store.load(execution1.durable_execution_arn)
assert loaded_execution.token_sequence == 10
def test_filesystem_execution_store_multiple_executions(store):
"""Test storing multiple executions."""
input_data1 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function-1",
function_qualifier="$LATEST",
execution_name="test-execution-1",
execution_timeout_seconds=300,
execution_retention_period_days=7,
)
input_data2 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function-2",
function_qualifier="$LATEST",
execution_name="test-execution-2",
execution_timeout_seconds=600,
execution_retention_period_days=14,
)
execution1 = Execution.new(input_data1)
execution2 = Execution.new(input_data2)
store.save(execution1)
store.save(execution2)
loaded_execution1 = store.load(execution1.durable_execution_arn)
loaded_execution2 = store.load(execution2.durable_execution_arn)
assert loaded_execution1.durable_execution_arn == execution1.durable_execution_arn
assert loaded_execution2.durable_execution_arn == execution2.durable_execution_arn
assert loaded_execution1.start_input.function_name == "test-function-1"
assert loaded_execution2.start_input.function_name == "test-function-2"
def test_filesystem_execution_store_list_all_empty(store):
"""Test list_all method with empty store."""
result = store.list_all()
assert result == []
def test_filesystem_execution_store_list_all_with_executions(store):
"""Test list_all method with multiple executions."""
# Create test executions
input_data1 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function-1",
function_qualifier="$LATEST",
execution_name="test-execution-1",
execution_timeout_seconds=300,
execution_retention_period_days=7,
)
input_data2 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function-2",
function_qualifier="$LATEST",
execution_name="test-execution-2",
execution_timeout_seconds=600,
execution_retention_period_days=14,
)
input_data3 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function-3",
function_qualifier="$LATEST",
execution_name="test-execution-3",
execution_timeout_seconds=900,
execution_retention_period_days=21,
)
execution1 = Execution.new(input_data1)
execution2 = Execution.new(input_data2)
execution3 = Execution.new(input_data3)
# Save executions
store.save(execution1)
store.save(execution2)
store.save(execution3)
# Test list_all
result = store.list_all()
assert len(result) == 3
arns = {execution.durable_execution_arn for execution in result}
assert execution1.durable_execution_arn in arns
assert execution2.durable_execution_arn in arns
assert execution3.durable_execution_arn in arns
def test_filesystem_execution_store_file_path_generation(
store, sample_execution, temp_storage_dir
):
"""Test that file paths are generated correctly with safe filenames."""
arn_with_colons = "arn:aws:lambda:us-east-1:123456789012:durable-execution:test"
expected_filename = (
"arn_aws_lambda_us-east-1_123456789012_durable-execution_test.json"
)
# Test by saving and checking the file exists with expected name
sample_execution.durable_execution_arn = arn_with_colons
store.save(sample_execution)
expected_file = temp_storage_dir / expected_filename
assert expected_file.exists()
def test_filesystem_execution_store_corrupted_file_handling(store, temp_storage_dir):
"""Test that corrupted files are skipped during list_all."""
# Create a valid execution
input_data = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="test-execution",
execution_timeout_seconds=300,
execution_retention_period_days=7,
)
execution = Execution.new(input_data)
store.save(execution)
# Create a corrupted file
corrupted_file = temp_storage_dir / "corrupted.json"
with open(corrupted_file, "w") as f:
f.write("invalid json content")
# list_all should skip the corrupted file and return only valid executions
result = store.list_all()
assert len(result) == 1
assert result[0].durable_execution_arn == execution.durable_execution_arn
def test_filesystem_execution_store_custom_storage_dir():
"""Test creating store with custom storage directory."""
with tempfile.TemporaryDirectory() as temp_dir:
custom_dir = Path(temp_dir) / "custom_storage"
FileSystemExecutionStore.create(custom_dir)
# Directory should be created
assert custom_dir.exists()
assert custom_dir.is_dir()
def test_filesystem_execution_store_init_no_side_effects():
"""Test that __init__ doesn't create directories (no side effects)."""
with tempfile.TemporaryDirectory() as temp_dir:
nonexistent_dir = Path(temp_dir) / "nonexistent"
# __init__ should not create the directory
FileSystemExecutionStore(nonexistent_dir)
assert not nonexistent_dir.exists()
def test_filesystem_execution_store_thread_safety_basic(store, sample_execution):
"""Basic test that operations work without locking (atomic file operations)."""
# Test that basic operations work - atomic file operations provide thread safety
store.save(sample_execution)
loaded = store.load(sample_execution.durable_execution_arn)
assert loaded.durable_execution_arn == sample_execution.durable_execution_arn
def test_filesystem_execution_store_query_empty(store):
"""Test query method with empty store."""
executions, next_marker = store.query()
assert executions == []
assert next_marker is None
def test_filesystem_execution_store_query_by_function_name(store):
"""Test query filtering by function name."""
# Create executions with different function names
input1 = StartDurableExecutionInput(
account_id="123456789012",
function_name="function-a",
function_qualifier="$LATEST",
execution_name="exec-1",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="invocation-1",
)
input2 = StartDurableExecutionInput(
account_id="123456789012",
function_name="function-b",
function_qualifier="$LATEST",
execution_name="exec-2",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="invocation-2",
)
exec1 = Execution.new(input1)
exec1.start()
exec2 = Execution.new(input2)
exec2.start()
store.save(exec1)
store.save(exec2)
# Query for function-a only
executions, next_marker = store.query(function_name="function-a")
assert len(executions) == 1
assert executions[0].durable_execution_arn == exec1.durable_execution_arn
assert next_marker is None
def test_filesystem_execution_store_query_by_status(store):
"""Test query filtering by status."""
# Create running execution
input1 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="running-exec",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="invocation-1",
)
exec1 = Execution.new(input1)
exec1.start()
# Create completed execution
input2 = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="completed-exec",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="invocation-2",
)
exec2 = Execution.new(input2)
exec2.start()
exec2.complete_success("success result")
store.save(exec1)
store.save(exec2)
# Query for running executions
executions, next_marker = store.query(status_filter="RUNNING")
assert len(executions) == 1
assert executions[0].durable_execution_arn == exec1.durable_execution_arn
# Query for succeeded executions
executions, next_marker = store.query(status_filter="SUCCEEDED")
assert len(executions) == 1
assert executions[0].durable_execution_arn == exec2.durable_execution_arn
def test_filesystem_execution_store_query_pagination(store):
"""Test query pagination."""
# Create multiple executions
executions = []
for i in range(5):
input_data = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name=f"exec-{i}",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id=f"invocation-{i}",
)
exec_obj = Execution.new(input_data)
exec_obj.start()
executions.append(exec_obj)
store.save(exec_obj)
# Test first page
executions, next_marker = store.query(limit=2, offset=0)
assert len(executions) == 2
assert next_marker is not None
# Test last page
executions, next_marker = store.query(limit=2, offset=4)
assert len(executions) == 1
assert next_marker is None
def test_filesystem_execution_store_query_corrupted_file_handling(
store, temp_storage_dir
):
"""Test that corrupted files are skipped during query."""
# Create a valid execution
input_data = StartDurableExecutionInput(
account_id="123456789012",
function_name="test-function",
function_qualifier="$LATEST",
execution_name="test-execution",
execution_timeout_seconds=300,
execution_retention_period_days=7,
invocation_id="test-invocation-id",
)
execution = Execution.new(input_data)
execution.start()
store.save(execution)
# Create a corrupted file
corrupted_file = temp_storage_dir / "corrupted.json"
with open(corrupted_file, "w") as f:
f.write("invalid json content")
# Query should skip the corrupted file and return only valid executions
executions, next_marker = store.query()
assert len(executions) == 1
assert executions[0].durable_execution_arn == execution.durable_execution_arn