-
Notifications
You must be signed in to change notification settings - Fork 5
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
Make sure Dynamodb lock context handler doesn't swallow exceptions #36
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 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,62 @@ | ||
from datetime import timedelta | ||
from python_dynamodb_lock.python_dynamodb_lock import DynamoDBLockError # type: ignore | ||
from test.impl.mock_dynamodb_test_case import MockDynamoDbTestCase | ||
from src.dynamodb.lock import dynamodb_lock | ||
|
||
|
||
class DynamodbLockTest(MockDynamoDbTestCase): | ||
def test_dynamodb_lock_different_pull_request_ids(self): | ||
dummy_counter = 0 | ||
with dynamodb_lock("pull_request_1"): | ||
dummy_counter += 1 | ||
with dynamodb_lock("pull_request_2"): | ||
dummy_counter += 1 | ||
|
||
self.assertEqual(dummy_counter, 2) | ||
|
||
def test_dynamodb_lock_consecutive_same_lock(self): | ||
# Just make sure the lock is released properly after the block | ||
lock_name = "pull_request_id" | ||
dummy_counter = 0 | ||
with dynamodb_lock(lock_name): | ||
dummy_counter += 1 | ||
|
||
with dynamodb_lock(lock_name): | ||
dummy_counter += 1 | ||
|
||
self.assertEqual(dummy_counter, 2) | ||
|
||
def test_dynamodb_lock_raises_exception_thrown_in_block(self): | ||
class DemoException(Exception): | ||
pass | ||
|
||
lock_name = "pull_request_id" | ||
|
||
with self.assertRaises(DemoException): | ||
with dynamodb_lock(lock_name): | ||
raise DemoException("oops") | ||
|
||
# Lock should still be released after the exception was raised | ||
dummy_counter = 0 | ||
with dynamodb_lock(lock_name): | ||
dummy_counter += 1 | ||
self.assertEqual(dummy_counter, 1) | ||
|
||
def test_dynamodb_lock_blocks_others_from_acquiring_lock(self): | ||
lock_name = "pull_request_id" | ||
dummy_counter = 0 | ||
with dynamodb_lock(lock_name): | ||
dummy_counter += 1 | ||
with self.assertRaises(DynamoDBLockError): | ||
with dynamodb_lock( | ||
lock_name, retry_timeout=timedelta(milliseconds=0.001) | ||
): # same lock name | ||
dummy_counter += 1 | ||
|
||
self.assertEqual(dummy_counter, 1) | ||
|
||
|
||
if __name__ == "__main__": | ||
from unittest import main as run_tests | ||
|
||
run_tests() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm don't know how much it actually matters, but the pep8 standard for imports is:
https://www.python.org/dev/peps/pep-0008/#imports
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'm generally sticking to
black
for formatting, and since the ordering of these is generally pep8 compatible besides the blank line between, I think it's fine and consistent with the other files in the codebase