Merge remote-tracking branch 'origin/master' into 3988-failing-test-http

This commit is contained in:
Itamar Turner-Trauring 2023-03-20 14:31:44 -04:00
commit ab300c090a
4 changed files with 42 additions and 13 deletions

View File

@ -0,0 +1 @@
tenacity is no longer a dependency.

View File

@ -58,7 +58,6 @@ let
pytest pytest
pytest-timeout pytest-timeout
pytest-twisted pytest-twisted
tenacity
testtools testtools
towncrier towncrier
]; ];

View File

@ -413,7 +413,6 @@ setup(name="tahoe-lafs", # also set in __init__.py
"beautifulsoup4", "beautifulsoup4",
"html5lib", "html5lib",
"junitxml", "junitxml",
"tenacity",
# Pin old version until # Pin old version until
# https://github.com/paramiko/paramiko/issues/1961 is fixed. # https://github.com/paramiko/paramiko/issues/1961 is fixed.
"paramiko < 2.9", "paramiko < 2.9",

View File

@ -4,18 +4,13 @@ Tests for allmydata.util.iputil.
Ported to Python 3. Ported to Python 3.
""" """
from __future__ import absolute_import from __future__ import annotations
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future.utils import PY2, native_str
if PY2:
from builtins import filter, map, zip, ascii, chr, hex, input, next, oct, open, pow, round, super, bytes, dict, list, object, range, str, max, min # noqa: F401
import os, socket import os, socket
import gc import gc
from functools import wraps
from typing import TypeVar, Callable
from testtools.matchers import ( from testtools.matchers import (
MatchesAll, MatchesAll,
IsInstance, IsInstance,
@ -25,8 +20,6 @@ from testtools.matchers import (
from twisted.trial import unittest from twisted.trial import unittest
from tenacity import retry, stop_after_attempt
from foolscap.api import Tub from foolscap.api import Tub
from allmydata.util import iputil, gcutil from allmydata.util import iputil, gcutil
@ -39,6 +32,43 @@ from .common import (
SyncTestCase, SyncTestCase,
) )
T = TypeVar("T", contravariant=True)
U = TypeVar("U", covariant=True)
def retry(stop: Callable[[], bool]) -> Callable[[Callable[[T], U]], Callable[[T], U]]:
"""
Call a function until the predicate says to stop or the function stops
raising an exception.
:param stop: A callable to call after the decorated function raises an
exception. The decorated function will be called again if ``stop``
returns ``False``.
:return: A decorator function.
"""
def decorate(f: Callable[[T], U]) -> Callable[[T], U]:
@wraps(f)
def decorator(self: T) -> U:
while True:
try:
return f(self)
except Exception:
if stop():
raise
return decorator
return decorate
def stop_after_attempt(limit: int) -> Callable[[], bool]:
"""
Stop after ``limit`` calls.
"""
counter = 0
def check():
nonlocal counter
counter += 1
return counter < limit
return check
class ListenOnUsed(unittest.TestCase): class ListenOnUsed(unittest.TestCase):
"""Tests for listenOnUnused.""" """Tests for listenOnUnused."""
@ -127,7 +157,7 @@ class GetLocalAddressesSyncTests(SyncTestCase):
IsInstance(list), IsInstance(list),
AllMatch( AllMatch(
MatchesAll( MatchesAll(
IsInstance(native_str), IsInstance(str),
MatchesPredicate( MatchesPredicate(
lambda addr: socket.inet_pton(socket.AF_INET, addr), lambda addr: socket.inet_pton(socket.AF_INET, addr),
"%r is not an IPv4 address.", "%r is not an IPv4 address.",