File: utils.py

package info (click to toggle)
toot 0.51.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,108 kB
  • sloc: python: 9,284; makefile: 41
file content (29 lines) | stat: -rw-r--r-- 723 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"""
Helpers for testing.
"""

import time
from typing import Callable, TypeVar


T = TypeVar("T")


def run_with_retries(fn: Callable[..., T]) -> T:
    """
    Run the the given function repeatedly until it finishes without raising an
    AssertionError. Sleep a bit between attempts. If the function doesn't
    succeed in the given number of tries raises the AssertionError. Used for
    tests which should eventually succeed.
    """

    # Wait upto 6 seconds with incrementally longer sleeps
    delays = [0.1, 0.2, 0.3, 0.4, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]

    for delay in delays:
        try:
            return fn()
        except AssertionError:
            time.sleep(delay)

    return fn()