generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 1
Add thread safety to execution operations #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| """Concurrent access tests for Execution class.""" | ||
|
|
||
| import threading | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
|
|
||
| from aws_durable_execution_sdk_python_testing.execution import Execution | ||
| from aws_durable_execution_sdk_python_testing.model import StartDurableExecutionInput | ||
|
|
||
|
|
||
| def test_concurrent_token_generation(): | ||
| """Test concurrent checkpoint token generation.""" | ||
| 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-inv-id", | ||
| input='{"test": "data"}', | ||
| ) | ||
| execution = Execution.new(input_data) | ||
| tokens = [] | ||
| tokens_lock = threading.Lock() | ||
|
|
||
| def generate_token(): | ||
| token = execution.get_new_checkpoint_token() | ||
| with tokens_lock: | ||
| tokens.append(token) | ||
|
|
||
| with ThreadPoolExecutor(max_workers=10) as executor: | ||
| futures = [executor.submit(generate_token) for _ in range(20)] | ||
|
|
||
| for future in as_completed(futures): | ||
| future.result() | ||
|
|
||
| # All tokens should be unique and sequential | ||
| assert len(tokens) == 20 | ||
| assert len(set(tokens)) == 20 # All unique | ||
| assert execution.token_sequence == 20 | ||
|
|
||
|
|
||
| def test_concurrent_operations_modification(): | ||
| """Test concurrent operations list modifications.""" | ||
| 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-inv-id", | ||
| input='{"test": "data"}', | ||
| ) | ||
| execution = Execution.new(input_data) | ||
| results = [] | ||
| results_lock = threading.Lock() | ||
|
|
||
| def start_execution(): | ||
| execution.start() | ||
| with results_lock: | ||
| results.append("started") | ||
|
|
||
| def get_operations(): | ||
| ops = execution.get_navigable_operations() | ||
| with results_lock: | ||
| results.append(f"ops-{len(ops)}") | ||
|
|
||
| with ThreadPoolExecutor(max_workers=5) as executor: | ||
| futures = [] | ||
| # One start operation | ||
| futures.append(executor.submit(start_execution)) | ||
| # Multiple read operations | ||
| futures.extend([executor.submit(get_operations) for _ in range(4)]) | ||
|
|
||
| for future in as_completed(futures): | ||
| future.result() | ||
|
|
||
| assert len(results) == 5 | ||
| assert "started" in results | ||
| # Should have at least one operation after start | ||
| final_ops = execution.get_navigable_operations() | ||
| assert len(final_ops) >= 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """Additional concurrent tests for wait and retry operations.""" | ||
|
|
||
| import threading | ||
| from concurrent.futures import ThreadPoolExecutor, as_completed | ||
| from datetime import UTC, datetime | ||
|
|
||
| from aws_durable_execution_sdk_python.lambda_service import ( | ||
| Operation, | ||
| OperationStatus, | ||
| OperationType, | ||
| StepDetails, | ||
| ) | ||
|
|
||
| from aws_durable_execution_sdk_python_testing.execution import Execution | ||
| from aws_durable_execution_sdk_python_testing.model import StartDurableExecutionInput | ||
|
|
||
|
|
||
| def test_concurrent_wait_and_retry_completion(): | ||
| """Test concurrent complete_wait and complete_retry operations.""" | ||
| 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-inv-id", | ||
| input='{"test": "data"}', | ||
| ) | ||
| execution = Execution.new(input_data) | ||
|
|
||
| # Add WAIT and STEP operations | ||
| wait_op = Operation( | ||
| operation_id="wait-1", | ||
| parent_id=None, | ||
| name="test-wait", | ||
| start_timestamp=datetime.now(UTC), | ||
| operation_type=OperationType.WAIT, | ||
| status=OperationStatus.STARTED, | ||
| ) | ||
|
|
||
| step_op = Operation( | ||
| operation_id="step-1", | ||
| parent_id=None, | ||
| name="test-step", | ||
| start_timestamp=datetime.now(UTC), | ||
| operation_type=OperationType.STEP, | ||
| status=OperationStatus.PENDING, | ||
| step_details=StepDetails(), | ||
| ) | ||
|
|
||
| execution.operations.extend([wait_op, step_op]) | ||
|
|
||
| results = [] | ||
| results_lock = threading.Lock() | ||
|
|
||
| def complete_wait(): | ||
| result = execution.complete_wait("wait-1") | ||
| with results_lock: | ||
| results.append(f"wait-completed-{result.status.value}") | ||
|
|
||
| def complete_retry(): | ||
| result = execution.complete_retry("step-1") | ||
| with results_lock: | ||
| results.append(f"retry-completed-{result.status.value}") | ||
|
|
||
| with ThreadPoolExecutor(max_workers=2) as executor: | ||
| futures = [] | ||
| futures.append(executor.submit(complete_wait)) | ||
| futures.append(executor.submit(complete_retry)) | ||
|
|
||
| for future in as_completed(futures): | ||
| future.result() | ||
|
|
||
| assert len(results) == 2 | ||
| assert "wait-completed-SUCCEEDED" in results | ||
| assert "retry-completed-READY" in results | ||
|
|
||
| # Verify token sequence was incremented twice | ||
| assert execution.token_sequence == 2 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.