| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
import gc |
| 12 |
import re |
| 13 |
import time |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import given, settings, strategies as st |
| 18 |
from hypothesis.errors import DeadlineExceeded, FlakyFailure, InvalidArgument |
| 19 |
|
| 20 |
from tests.common.utils import assert_falsifying_output, fails_with |
| 21 |
|
| 22 |
pytestmark = pytest.mark.skipif( |
| 23 |
settings.get_current_profile_name() == "threading", |
| 24 |
reason="takes a long time because we don't monkeypatch time", |
| 25 |
) |
| 26 |
|
| 27 |
|
| 28 |
def test_raises_deadline_on_slow_test(): |
| 29 |
@settings(deadline=500) |
| 30 |
@given(st.integers()) |
| 31 |
def slow(i): |
| 32 |
time.sleep(1) |
| 33 |
|
| 34 |
with pytest.raises(DeadlineExceeded): |
| 35 |
slow() |
| 36 |
|
| 37 |
|
| 38 |
@pytest.mark.skipif( |
| 39 |
settings().deadline is None, |
| 40 |
reason="not expected to fail if deadline is disabled", |
| 41 |
) |
| 42 |
@fails_with(DeadlineExceeded) |
| 43 |
@given(st.integers()) |
| 44 |
def test_slow_tests_are_errors_by_default(i): |
| 45 |
time.sleep(1) |
| 46 |
|
| 47 |
|
| 48 |
def test_non_numeric_deadline_is_an_error(): |
| 49 |
with pytest.raises(InvalidArgument): |
| 50 |
settings(deadline="3 seconds") |
| 51 |
|
| 52 |
|
| 53 |
@given(st.integers()) |
| 54 |
@settings(deadline=None) |
| 55 |
def test_slow_with_none_deadline(i): |
| 56 |
time.sleep(1) |
| 57 |
|
| 58 |
|
| 59 |
def test_raises_flaky_if_a_test_becomes_fast_on_rerun(): |
| 60 |
once = True |
| 61 |
|
| 62 |
@settings(deadline=500, backend="hypothesis") |
| 63 |
@given(st.integers()) |
| 64 |
def test_flaky_slow(i): |
| 65 |
nonlocal once |
| 66 |
if once: |
| 67 |
once = False |
| 68 |
time.sleep(1) |
| 69 |
|
| 70 |
with pytest.raises(FlakyFailure): |
| 71 |
test_flaky_slow() |
| 72 |
|
| 73 |
|
| 74 |
def test_deadlines_participate_in_shrinking(): |
| 75 |
@settings(deadline=500, max_examples=1000, database=None) |
| 76 |
@given(st.integers(min_value=0)) |
| 77 |
def slow_if_large(i): |
| 78 |
if i >= 1000: |
| 79 |
time.sleep(1) |
| 80 |
|
| 81 |
assert_falsifying_output( |
| 82 |
slow_if_large, |
| 83 |
expected_exception=DeadlineExceeded, |
| 84 |
i=1000, |
| 85 |
) |
| 86 |
|
| 87 |
|
| 88 |
def test_keeps_you_well_above_the_deadline(): |
| 89 |
seen = set() |
| 90 |
failed_once = False |
| 91 |
|
| 92 |
@settings(deadline=100, backend="hypothesis") |
| 93 |
@given(st.integers(0, 2000)) |
| 94 |
def slow(i): |
| 95 |
nonlocal failed_once |
| 96 |
# Make sure our initial failure isn't something that immediately goes flaky. |
| 97 |
if not failed_once: |
| 98 |
if i * 0.9 <= 100: |
| 99 |
return |
| 100 |
else: |
| 101 |
failed_once = True |
| 102 |
|
| 103 |
t = i / 1000 |
| 104 |
if i in seen: |
| 105 |
time.sleep(0.9 * t) |
| 106 |
else: |
| 107 |
seen.add(i) |
| 108 |
time.sleep(t) |
| 109 |
|
| 110 |
with pytest.raises(DeadlineExceeded): |
| 111 |
slow() |
| 112 |
|
| 113 |
|
| 114 |
def test_gives_a_deadline_specific_flaky_error_message(): |
| 115 |
once = True |
| 116 |
|
| 117 |
@settings(deadline=100, backend="hypothesis") |
| 118 |
@given(st.integers()) |
| 119 |
def slow_once(i): |
| 120 |
nonlocal once |
| 121 |
if once: |
| 122 |
once = False |
| 123 |
time.sleep(0.2) |
| 124 |
|
| 125 |
with pytest.raises(FlakyFailure) as err: |
| 126 |
slow_once() |
| 127 |
assert "Unreliable test timing" in "\n".join(err.value.__notes__) |
| 128 |
# this used to be "took 2", but we saw that flake (on pypy, though unsure if |
| 129 |
# that means anything) with "took 199.59ms". It's possible our gc accounting |
| 130 |
# is incorrect, or we could just be running into rare non-guarantees of |
| 131 |
# time.sleep. |
| 132 |
assert re.search(r"took \d", "\n".join(err.value.__notes__)) |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.parametrize("slow_strategy", [False, True]) |
| 136 |
@pytest.mark.parametrize("slow_test", [False, True]) |
| 137 |
def test_should_only_fail_a_deadline_if_the_test_is_slow(slow_strategy, slow_test): |
| 138 |
s = st.integers() |
| 139 |
if slow_strategy: |
| 140 |
s = s.map(lambda x: time.sleep(0.08)) |
| 141 |
|
| 142 |
@settings(deadline=50) |
| 143 |
@given(st.data()) |
| 144 |
def test(data): |
| 145 |
data.draw(s) |
| 146 |
if slow_test: |
| 147 |
time.sleep(0.1) |
| 148 |
|
| 149 |
if slow_test: |
| 150 |
with pytest.raises(DeadlineExceeded): |
| 151 |
test() |
| 152 |
else: |
| 153 |
test() |
| 154 |
|
| 155 |
|
| 156 |
@pytest.mark.skipif(not hasattr(gc, "callbacks"), reason="CPython specific gc delay") |
| 157 |
def test_should_not_fail_deadline_due_to_gc(): |
| 158 |
@settings(max_examples=1, deadline=50) |
| 159 |
@given(st.integers()) |
| 160 |
def test(i): |
| 161 |
before = time.perf_counter() |
| 162 |
gc.collect() |
| 163 |
assert time.perf_counter() - before >= 0.1 # verify that we're slow |
| 164 |
|
| 165 |
def delay(phase, _info): |
| 166 |
if phase == "start": |
| 167 |
time.sleep(0.1) |
| 168 |
|
| 169 |
try: |
| 170 |
gc.callbacks.append(delay) |
| 171 |
test() |
| 172 |
finally: |
| 173 |
gc.callbacks.remove(delay) |