Skip to content

Commit f14b2ce

Browse files
committed
test: ensure non-empty test selection
1 parent 8d03e0f commit f14b2ce

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

tests/test_scoped_critical_section.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ class BoolWrapper {
2323
std::atomic<bool> value_{false};
2424
};
2525

26-
#ifdef PYBIND11_HAS_BARRIER
27-
bool test_scoped_critical_section(const py::handle &cls) {
26+
#if defined(PYBIND11_HAS_BARRIER) && defined(Py_GIL_DISABLED)
27+
28+
void test_scoped_critical_section(const py::handle &cls) {
2829
auto barrier = std::barrier(2);
2930
auto bool_wrapper = cls(false);
3031
bool output = false;
@@ -47,10 +48,12 @@ bool test_scoped_critical_section(const py::handle &cls) {
4748
t1.join();
4849
t2.join();
4950

50-
return output;
51+
if (!output) {
52+
throw std::runtime_error("Scoped critical section test failed: output is false");
53+
}
5154
}
5255

53-
std::pair<bool, bool> test_scoped_critical_section2(const py::handle &cls) {
56+
void test_scoped_critical_section2(const py::handle &cls) {
5457
auto barrier = std::barrier(3);
5558
auto bool_wrapper1 = cls(false);
5659
auto bool_wrapper2 = cls(false);
@@ -84,10 +87,13 @@ std::pair<bool, bool> test_scoped_critical_section2(const py::handle &cls) {
8487
t2.join();
8588
t3.join();
8689

87-
return output;
90+
if (!output.first || !output.second) {
91+
throw std::runtime_error(
92+
"Scoped critical section test with two objects failed: output is false");
93+
}
8894
}
8995

90-
bool test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls) {
96+
void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls) {
9197
auto barrier = std::barrier(2);
9298
auto bool_wrapper = cls(false);
9399
bool output = false;
@@ -110,8 +116,18 @@ bool test_scoped_critical_section2_same_object_no_deadlock(const py::handle &cls
110116
t1.join();
111117
t2.join();
112118

113-
return output;
119+
if (!output) {
120+
throw std::runtime_error(
121+
"Scoped critical section test with same object failed: output is false");
122+
}
114123
}
124+
125+
#else
126+
127+
void test_scoped_critical_section(const py::handle &) {}
128+
void test_scoped_critical_section2(const py::handle &) {}
129+
void test_scoped_critical_section2_same_object_no_deadlock(const py::handle &) {}
130+
115131
#endif
116132

117133
TEST_SUBMODULE(scoped_critical_section, m) {
@@ -132,14 +148,12 @@ TEST_SUBMODULE(scoped_critical_section, m) {
132148
#ifdef PYBIND11_HAS_BARRIER
133149
m.attr("has_barrier") = true;
134150

135-
m.def("test_scoped_critical_section", [BoolWrapperHandle]() -> bool {
136-
return test_scoped_critical_section(BoolWrapperHandle);
137-
});
138-
m.def("test_scoped_critical_section2", [BoolWrapperHandle]() -> std::pair<bool, bool> {
139-
return test_scoped_critical_section2(BoolWrapperHandle);
140-
});
141-
m.def("test_scoped_critical_section2_same_object_no_deadlock", [BoolWrapperHandle]() -> bool {
142-
return test_scoped_critical_section2_same_object_no_deadlock(BoolWrapperHandle);
151+
m.def("test_scoped_critical_section",
152+
[BoolWrapperHandle]() -> void { test_scoped_critical_section(BoolWrapperHandle); });
153+
m.def("test_scoped_critical_section2",
154+
[BoolWrapperHandle]() -> void { test_scoped_critical_section2(BoolWrapperHandle); });
155+
m.def("test_scoped_critical_section2_same_object_no_deadlock", [BoolWrapperHandle]() -> void {
156+
test_scoped_critical_section2_same_object_no_deadlock(BoolWrapperHandle);
143157
});
144158
#else
145159
m.attr("has_barrier") = false;

tests/test_scoped_critical_section.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22

33
import pytest
44

5-
import env
65
from pybind11_tests import scoped_critical_section as m
76

87

9-
@pytest.mark.skipif(not env.PY_GIL_DISABLED, reason="requires GIL disabled")
108
@pytest.mark.skipif(not m.has_barrier, reason="no <barrier>")
119
def test_scoped_critical_section() -> None:
1210
for _ in range(64):
13-
assert m.test_scoped_critical_section() is True
11+
m.test_scoped_critical_section()
1412

1513

16-
@pytest.mark.skipif(not env.PY_GIL_DISABLED, reason="requires GIL disabled")
1714
@pytest.mark.skipif(not m.has_barrier, reason="no <barrier>")
1815
def test_scoped_critical_section2() -> None:
1916
for _ in range(64):
20-
assert m.test_scoped_critical_section2() == (True, True)
17+
m.test_scoped_critical_section2()
2118

2219

23-
@pytest.mark.skipif(not env.PY_GIL_DISABLED, reason="requires GIL disabled")
2420
@pytest.mark.skipif(not m.has_barrier, reason="no <barrier>")
2521
def test_scoped_critical_section2_same_object_no_deadlock() -> None:
2622
for _ in range(64):
27-
assert m.test_scoped_critical_section2_same_object_no_deadlock() is True
23+
m.test_scoped_critical_section2_same_object_no_deadlock()

0 commit comments

Comments
 (0)