mercredi 7 avril 2021

Testing timeouts using Pytest

So recently I've been learning how to write tests with pytest and I've come up with an issue:

I have a web application that creates a token with a timeout. There's a function that repeats itself every 5 seconds, which checks every token created. If the time is up, the token gets deleted. It works, but I need to create a test for it, and here is where I'm stuck.

Currently, my thought process is to simply run assertion, wait 6 seconds, and run it again with a different assertion, but it seems that the time.sleep() function stops the timer of my token checker as well, so it doesn't really work for me. Anyone have any ideas?

main.py:

@repeat_every(seconds=1 * 5)
def timeout_check():
    for mutex_id in list(mutexes):
        if mutexes[mutex_id].expire_datetime < datetime.now().astimezone():
            del mutexes[mutex_id] 

test.py:

def test_timeout_check_delete_inuse():
    payload = {
        "token": "timeout_check_inuse",
        "timeout": 2,
    }
    tid = res.json()["id"]
    res = client.get("/muted/mutexes/%s" % tid)
    assert res.status_code == 200
    time.sleep(6)
    assert res.status_code == 404

Any suggestions/ideas helps!

Aucun commentaire:

Enregistrer un commentaire