jeudi 6 février 2020

Python test a function with a timeout decorator

So I have a function which calls some API. I used a decorator with a timeout on it using timeout-decorator package. Basically it looks like this:

import timeout_decorator
import requests

@timeout_decorator.timeout(seconds=10, use_signals=False, timeout_exception=MyTimeoutException)
def get_from_api(self, url: str, params: Dict):
    requests.get(url=url, params=params)

Now I want to create a test which would check whether this decorator works, so if the execution of the body of get_from_api will take longer than in my case 10 seconds - an exception will get raised.

The problem is of course I don't want to wait whole 10 seconds + I have to somehow make the body execute for longer than the timeout, to make the exception get raised.

My question here is: is there a lib, or a simple mock technique, to for example make this timeout be e.g. 0.1s and the body execution time to be e.g. 0.2s? P.S I don't know if it's that much relevant, but I am using pytest

Aucun commentaire:

Enregistrer un commentaire