tomasbasham / ratelimit

API Rate Limit Decorator
MIT License
768 stars 159 forks source link

Documentation on mocking ratelimit limits decorator #34

Open mcohoon opened 5 years ago

mcohoon commented 5 years ago

I've implemented rate limiting as documented in the readme:

@on_exception(expo, RateLimitException, max_tries=8)
@limits(calls=15, period=FIFTEEN_MINUTES)
def call_api(url):
    response = requests.get(url)
    ...

But I would like a way to effectively turn off this rate-limiting while running tests. I've not found any concise examples on stackoverflow on how mock a decorator as such without re-importing the module, and I think this would be an ideal library to show how to mock out a decorator for testing. Would you be open to adding an example of how to do such a thing?

stuaxo commented 4 years ago

Having an API to reset the rate limit would be enough for me, since I could call it in the unittest.

stuaxo commented 4 years ago

Hi @mcohoon @tom-james-watson I've implemented this in the PR #43 _Add resetratelimit method.

I spent a lot of time trying to workaround this, but it was just too difficult without being able to reset the limit.

In my test I used this along with fakesleep and am able to test my function that is decorated with sleep_and_retry and limits