| 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 sys |
| 12 |
import time |
| 13 |
from collections import Counter |
| 14 |
|
| 15 |
import pytest |
| 16 |
|
| 17 |
from hypothesis import Phase, settings |
| 18 |
from hypothesis.database import ( |
| 19 |
DirectoryBasedExampleDatabase, |
| 20 |
InMemoryExampleDatabase, |
| 21 |
MultiplexedDatabase, |
| 22 |
) |
| 23 |
|
| 24 |
from tests.common.utils import flaky, skipif_threading, wait_for |
| 25 |
from tests.cover.test_database_backend import _database_conforms_to_listener_api |
| 26 |
|
| 27 |
# Our database listener tests are tantalizingly close to being useful, but are |
| 28 |
# still subtly flaky. I have spent two afternoons trying to isolate the problem |
| 29 |
# and have concluded it would require sitting down at a deep level with not only |
| 30 |
# the watchdog implementation but also platform-specific filesystem watcher details. |
| 31 |
# |
| 32 |
# I've seen issues like duplicate events being emitted, stopping/starting a listener |
| 33 |
# in quick succession repeating the previous batch of events, and other weirdness. |
| 34 |
# |
| 35 |
# These tests are still useful as a manual check when changing database listener |
| 36 |
# code, and as an encoding of our desired semantics. |
| 37 |
pytestmark = pytest.mark.skip(reason="see comment") |
| 38 |
|
| 39 |
|
| 40 |
# we need real time here, not monkeypatched for CI |
| 41 |
time_sleep = time.sleep |
| 42 |
|
| 43 |
|
| 44 |
def test_database_listener_directory(): |
| 45 |
_database_conforms_to_listener_api( |
| 46 |
lambda path: DirectoryBasedExampleDatabase(path), |
| 47 |
supports_value_delete=False, |
| 48 |
parent_settings=settings( |
| 49 |
# this test is very expensive because we wait between every rule for |
| 50 |
# the filesystem observer to fire. |
| 51 |
max_examples=5, |
| 52 |
stateful_step_count=10, |
| 53 |
# expensive runtime makes shrinking take forever |
| 54 |
phases=set(Phase) - {Phase.shrink}, |
| 55 |
deadline=None, |
| 56 |
), |
| 57 |
) |
| 58 |
|
| 59 |
|
| 60 |
# seen flaky on test-win; we get *three* of the same save events in the first |
| 61 |
# assertion, which...is baffling, and possibly a genuine bug (most likely in |
| 62 |
# watchdog). |
| 63 |
@skipif_threading # add_listener is not thread safe because watchdog is not |
| 64 |
def test_database_listener_multiplexed(tmp_path): |
| 65 |
db = MultiplexedDatabase( |
| 66 |
InMemoryExampleDatabase(), DirectoryBasedExampleDatabase(tmp_path) |
| 67 |
) |
| 68 |
events = [] |
| 69 |
|
| 70 |
def listener(event): |
| 71 |
events.append(event) |
| 72 |
|
| 73 |
db.add_listener(listener) |
| 74 |
time_sleep(0.1) |
| 75 |
|
| 76 |
db.save(b"a", b"a") |
| 77 |
wait_for(lambda: events == [("save", (b"a", b"a"))] * 2, timeout=30) |
| 78 |
|
| 79 |
db.remove_listener(listener) |
| 80 |
db.delete(b"a", b"a") |
| 81 |
db.save(b"a", b"b") |
| 82 |
wait_for(lambda: events == [("save", (b"a", b"a"))] * 2, timeout=30) |
| 83 |
|
| 84 |
db.add_listener(listener) |
| 85 |
time_sleep(0.1) |
| 86 |
|
| 87 |
db.delete(b"a", b"b") |
| 88 |
db.save(b"a", b"c") |
| 89 |
# InMemory database fires immediately, while DirectoryBased has to |
| 90 |
# wait for filesystem listeners. Therefore the events can arrive out of |
| 91 |
# order. Test a weaker multiset property, disregarding ordering. |
| 92 |
wait_for( |
| 93 |
lambda: Counter(events[2:]) |
| 94 |
== { |
| 95 |
# InMemory |
| 96 |
("delete", (b"a", b"b")): 1, |
| 97 |
# DirectoryBased |
| 98 |
("delete", (b"a", None)): 1, |
| 99 |
# both |
| 100 |
("save", (b"a", b"c")): 2, |
| 101 |
}, |
| 102 |
timeout=30, |
| 103 |
) |
| 104 |
|
| 105 |
|
| 106 |
@skipif_threading # add_listener is not thread safe because watchdog is not |
| 107 |
def test_database_listener_directory_explicit(tmp_path): |
| 108 |
db = DirectoryBasedExampleDatabase(tmp_path) |
| 109 |
events = [] |
| 110 |
|
| 111 |
def listener(event): |
| 112 |
events.append(event) |
| 113 |
|
| 114 |
time_sleep(0.1) |
| 115 |
db.add_listener(listener) |
| 116 |
time_sleep(0.1) |
| 117 |
|
| 118 |
db.save(b"k1", b"v1") |
| 119 |
wait_for(lambda: events == [("save", (b"k1", b"v1"))], timeout=30) |
| 120 |
|
| 121 |
time_sleep(0.1) |
| 122 |
db.remove_listener(listener) |
| 123 |
time_sleep(0.1) |
| 124 |
|
| 125 |
db.delete(b"k1", b"v1") |
| 126 |
db.save(b"k1", b"v2") |
| 127 |
wait_for(lambda: events == [("save", (b"k1", b"v1"))], timeout=30) |
| 128 |
|
| 129 |
time_sleep(0.1) |
| 130 |
db.add_listener(listener) |
| 131 |
time_sleep(0.1) |
| 132 |
|
| 133 |
db.delete(b"k1", b"v2") |
| 134 |
db.save(b"k1", b"v3") |
| 135 |
wait_for( |
| 136 |
lambda: events[1:] |
| 137 |
== [ |
| 138 |
("delete", (b"k1", None)), |
| 139 |
("save", (b"k1", b"v3")), |
| 140 |
], |
| 141 |
timeout=30, |
| 142 |
) |
| 143 |
|
| 144 |
# moving into a nonexistent key |
| 145 |
db.move(b"k1", b"k2", b"v3") |
| 146 |
time_sleep(0.5) |
| 147 |
# moving back into an existing key |
| 148 |
db.move(b"k2", b"k1", b"v3") |
| 149 |
time_sleep(0.5) |
| 150 |
|
| 151 |
if sys.platform.startswith("darwin"): |
| 152 |
assert events[3:] == [ |
| 153 |
("delete", (b"k1", b"v3")), |
| 154 |
("save", (b"k2", b"v3")), |
| 155 |
("delete", (b"k2", b"v3")), |
| 156 |
("save", (b"k1", b"v3")), |
| 157 |
], str(events[3:]) |
| 158 |
elif sys.platform.startswith("win"): |
| 159 |
# watchdog fires save/delete events instead of move events on windows. |
| 160 |
# This means we don't broadcast the exact deleted value. |
| 161 |
assert events[3:] == [ |
| 162 |
("delete", (b"k1", None)), |
| 163 |
("save", (b"k2", b"v3")), |
| 164 |
("delete", (b"k2", None)), |
| 165 |
("save", (b"k1", b"v3")), |
| 166 |
], str(events[3:]) |
| 167 |
elif sys.platform.startswith("linux"): |
| 168 |
# move #1 |
| 169 |
assert ("save", (b"k2", b"v3")) in events |
| 170 |
# sometimes watchdog fires a move event (= save + delete with value), |
| 171 |
# and other times it fires separate save and delete events (= delete with |
| 172 |
# no value). I think this is due to particulars of what happens when |
| 173 |
# a new directory gets created very close to the time when a file is |
| 174 |
# saved to that directory. |
| 175 |
assert any(("delete", (b"k1", val)) in events for val in [b"v3", None]) |
| 176 |
|
| 177 |
# move #2 |
| 178 |
assert ("save", (b"k1", b"v3")) in events |
| 179 |
assert any(("delete", (b"k2", val)) in events for val in [b"v3", None]) |
| 180 |
else: |
| 181 |
raise NotImplementedError(f"unknown platform {sys.platform}") |
| 182 |
|
| 183 |
|
| 184 |
@flaky(max_runs=5, min_passes=1) # time_sleep(0.1) probably isn't enough here |
| 185 |
@skipif_threading # add_listener is not thread safe because watchdog is not |
| 186 |
def test_database_listener_directory_move(tmp_path): |
| 187 |
db = DirectoryBasedExampleDatabase(tmp_path) |
| 188 |
events = [] |
| 189 |
|
| 190 |
def listener(event): |
| 191 |
events.append(event) |
| 192 |
|
| 193 |
# make sure both keys exist and that v1 exists in k1 and not k2 |
| 194 |
db.save(b"k1", b"v1") |
| 195 |
db.save(b"k2", b"v_unrelated") |
| 196 |
|
| 197 |
time_sleep(0.1) |
| 198 |
db.add_listener(listener) |
| 199 |
time_sleep(0.1) |
| 200 |
|
| 201 |
db.move(b"k1", b"k2", b"v1") |
| 202 |
# events might arrive in either order |
| 203 |
wait_for( |
| 204 |
lambda: set(events) |
| 205 |
== { |
| 206 |
("save", (b"k2", b"v1")), |
| 207 |
# windows doesn't fire move events, so value is None |
| 208 |
("delete", (b"k1", None if sys.platform.startswith("win") else b"v1")), |
| 209 |
}, |
| 210 |
timeout=30, |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
@skipif_threading # add_listener is not thread safe because watchdog is not |
| 215 |
def test_still_listens_if_directory_did_not_exist(tmp_path): |
| 216 |
# if we start listening on a nonexistent path, we will create that path and |
| 217 |
# still listen for events |
| 218 |
events = [] |
| 219 |
|
| 220 |
def listener(event): |
| 221 |
events.append(event) |
| 222 |
|
| 223 |
p = tmp_path / "does_not_exist_yet" |
| 224 |
db = DirectoryBasedExampleDatabase(p) |
| 225 |
assert not p.exists() |
| 226 |
|
| 227 |
db.add_listener(listener) |
| 228 |
assert p.exists() |
| 229 |
|
| 230 |
assert not events |
| 231 |
db.save(b"k1", b"v1") |
| 232 |
wait_for(lambda: len(events) == 1, timeout=30) |