Skip to content

feat(idempotency): Fix KeyError when local_cache is True and an error is raised in the lambda handler #300

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 5 commits into from
Feb 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ def _retrieve_from_cache(self, idempotency_key: str):
def _delete_from_cache(self, idempotency_key: str):
if not self.use_local_cache:
return
del self._cache[idempotency_key]
if idempotency_key in self._cache:
del self._cache[idempotency_key]

def save_success(self, event: Dict[str, Any], result: dict) -> None:
"""
Expand Down
11 changes: 11 additions & 0 deletions tests/functional/idempotency/test_idempotency.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,3 +627,14 @@ def test_user_local_disabled(persistence_store):
# THEN raise AttributeError
# AND don't have a _cache attribute
assert not hasattr("persistence_store", "_cache")


@pytest.mark.parametrize("persistence_store", [{"use_local_cache": True}], indirect=True)
def test_delete_from_cache_when_empty(persistence_store):
# GIVEN use_local_cache is True AND the local cache is empty
try:
# WHEN we _delete_from_cache
persistence_store._delete_from_cache("key_does_not_exist")
except KeyError:
# THEN we should not get a KeyError
pytest.fail("KeyError should not happen")
22 changes: 22 additions & 0 deletions tests/unit/test_lru_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,25 @@ def test_setitem_moves_to_end(populated_cache):

assert last_item == f"key_{random_value}"
assert populated_cache[f"key_{random_value}"] == f"new_val_{random_value}"


def test_lru_pop_failing():
cache = LRUDict()
key = "test"
cache[key] = "value"
try:
cache.pop(key, None)
pytest.fail("GitHub #300: LRUDict pop bug has been fixed :)")
except KeyError as e:
assert e.args[0] == key


def test_lru_del():
cache = LRUDict()
key = "test"
cache[key] = "value"
assert len(cache) == 1
if key in cache:
del cache[key]
assert key not in cache
assert len(cache) == 0