forked from GoogleCloudPlatform/python-repo-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: fix several issues in eventually_consistent
* correctly handle argument provided to the decorators * provide the real callback for retry_on_exception instead of None fixes GoogleCloudPlatform#25 The helper function `_retry_on_exception` didn't return the inner function properly so that, it is always None. Luckily, if you pass `None` to `retry_on_exception` argument, the retrying module always retries as shown bellow: https://github.com/rholder/retrying/blob/1d5699348d707e377aad7488da6a8a1b48a65933/retrying.py#L139 Previously, you can not pass any arguments to the decorator `mark` and `call`. This PR will allow both syntax with or without arguments. I also added unit tests to this module. I'm not sure if we have CI builds, but the test passes locally.
- Loading branch information
Takashi Matsuo
committed
Apr 4, 2020
1 parent
87422ba
commit 7074014
Showing
2 changed files
with
210 additions
and
16 deletions.
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,129 @@ | ||
# Copyright 2018 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import pytest | ||
|
||
from gcp_devrel.testing import eventually_consistent | ||
|
||
test_mark_simple_tries = 0 | ||
test_mark_args_tries = 0 | ||
test_mark_custom_exception_tries = 0 | ||
test_mark_custom_exception_with_tuple_tries = 0 | ||
|
||
class MyException(Exception): | ||
pass | ||
|
||
@eventually_consistent.mark | ||
def test_mark_simple(): | ||
global test_mark_simple_tries | ||
test_mark_simple_tries += 1 | ||
if test_mark_simple_tries == 2: | ||
assert True | ||
else: | ||
assert False | ||
|
||
@eventually_consistent.mark(tries=2) | ||
def test_mark_args(): | ||
global test_mark_args_tries | ||
test_mark_args_tries += 1 | ||
if test_mark_args_tries == 2: | ||
assert True | ||
else: | ||
assert False | ||
|
||
|
||
@eventually_consistent.mark(tries=2, exceptions=MyException) | ||
def test_mark_custom_exception(): | ||
global test_mark_custom_exception_tries | ||
test_mark_custom_exception_tries += 1 | ||
if test_mark_custom_exception_tries == 2: | ||
assert True | ||
else: | ||
raise MyException | ||
|
||
|
||
@eventually_consistent.mark(tries=3, exceptions=(MyException, AssertionError)) | ||
def test_mark_custom_exceptions_with_tuple(): | ||
global test_mark_custom_exception_with_tuple_tries | ||
test_mark_custom_exception_with_tuple_tries += 1 | ||
if test_mark_custom_exception_with_tuple_tries == 3: | ||
assert True | ||
elif test_mark_custom_exception_with_tuple_tries == 2: | ||
raise MyException | ||
else: | ||
assert False | ||
|
||
|
||
def test_call_simple(): | ||
tried = 0 | ||
@eventually_consistent.call | ||
def _(): | ||
nonlocal tried | ||
tried += 1 | ||
if tried == 2: | ||
assert True | ||
else: | ||
assert False | ||
|
||
|
||
def test_call_args(): | ||
tried = 0 | ||
@eventually_consistent.call(tries=2) | ||
def _(): | ||
nonlocal tried | ||
tried += 1 | ||
if tried == 2: | ||
assert True | ||
else: | ||
assert False | ||
|
||
|
||
def test_call_args_fail(): | ||
with pytest.raises(AssertionError): | ||
tried = 0 | ||
@eventually_consistent.call(tries=2) | ||
def _(): | ||
nonlocal tried | ||
tried += 1 | ||
if tried == 3: | ||
assert True | ||
else: | ||
assert False | ||
|
||
|
||
def test_call_custom_exception(): | ||
tried = 0 | ||
@eventually_consistent.call(tries=2, exceptions=MyException) | ||
def _(): | ||
nonlocal tried | ||
tried += 1 | ||
if tried == 2: | ||
assert True | ||
else: | ||
raise MyException | ||
|
||
|
||
def test_call_custom_exception_with_tuple(): | ||
tried = 0 | ||
@eventually_consistent.call( | ||
tries=3, exceptions=(MyException, AssertionError)) | ||
def _(): | ||
nonlocal tried | ||
tried += 1 | ||
if tried == 3: | ||
assert True | ||
elif tried == 2: | ||
assert False | ||
else: | ||
raise MyException |