Skip to content

Commit dbdf0a2

Browse files
committed
address review
1 parent 1193e92 commit dbdf0a2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

hypothesis-python/src/hypothesis/strategies/_internal/strategies.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,10 @@ def do_validate(self) -> None:
864864

865865
@property
866866
def branches(self) -> Sequence[SearchStrategy[Ex]]:
867+
if self.__element_strategies is not None:
868+
# common fast path which avoids the lock
869+
return self.element_strategies
870+
867871
with self._branches_lock:
868872
if not self.__in_branches:
869873
try:

hypothesis-python/tests/nocover/test_threading.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from hypothesis.errors import DeadlineExceeded, InvalidArgument
1919
from hypothesis.internal.conjecture.junkdrawer import ensure_free_stackframes
2020
from hypothesis.stateful import RuleBasedStateMachine, invariant, rule
21+
from hypothesis.strategies import SearchStrategy
2122

2223
from tests.common.debug import check_can_generate_examples
2324

@@ -72,9 +73,9 @@ def my_invariant(self):
7273
run_concurrently(TestMyStateful, n=2)
7374

7475

75-
def do_work():
76+
def do_work(*, multiplier=1):
7677
# arbitrary moderately-expensive work
77-
for x in range(500):
78+
for x in range(500 * multiplier):
7879
_y = x**x
7980

8081

@@ -213,14 +214,22 @@ def slow_test(n):
213214

214215

215216
def test_one_of_branches_lock():
216-
# I can't actually get this test to reproduce the race locally.
217-
# This should in theory reproduce, but the timings here are very tight.
217+
class SlowBranchesStrategy(SearchStrategy):
218+
@property
219+
def branches(self):
220+
# multiplier=2 reproduces more consistently than multiplier=1 for me
221+
do_work(multiplier=2)
222+
return [st.integers(), st.text()]
223+
218224
branch_counts = set()
219-
s = st.one_of(st.integers(), st.integers(), st.integers())
225+
s = st.one_of(SlowBranchesStrategy(), SlowBranchesStrategy())
220226

221227
def test():
222228
branches = len(s.branches)
223229
branch_counts.add(branches)
224230

225231
run_concurrently(test, n=10)
226232
assert len(branch_counts) == 1
233+
# there are 4 independent strategies, but only 2 distinct ones -
234+
# st.integers(), and st.text().
235+
assert branch_counts == {2}

0 commit comments

Comments
 (0)