Skip to content

Commit b44f550

Browse files
authored
Run the tests with assertions disabled ~25% of the time (#15325)
* Fix the code so that testing with -Ptests.asserts=false passes. * More tests that don't work with assertions disabled. * Pick tests.asserts randomly based on the main seed. Use assertions 75% of the time. * Changes entry. * Clean up duplicate assertion-verifying code.
1 parent f1e151e commit b44f550

35 files changed

+215
-131
lines changed

build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/TestsAndRandomizationPlugin.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,13 @@ public void apply(Project project) {
223223

224224
Provider<Boolean> assertsOption =
225225
buildOptions.addBooleanOption(
226-
"tests.asserts", "Enables or disables assertions mode.", true);
226+
"tests.asserts",
227+
"Enables or disables assertions mode.",
228+
project.provider(
229+
() -> {
230+
// Run with assertions for ~75% of all seeds.
231+
return new Random(buildGlobals.getProjectSeedAsLong().get()).nextInt(100) > 25;
232+
}));
227233
optionsInheritedAsProperties.add("tests.asserts");
228234

229235
buildOptions.addBooleanOption(

lucene/CHANGES.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Changes in Runtime Behavior
106106

107107
Build
108108
---------------------
109+
* GITHUB#15325: Run the tests with assertions disabled ~25% of the time #15325 (Dawid Weiss)
110+
109111
* GITHUB#14804: Detect and ban wildcard imports in Java (Robert Muir, Dawid Weiss)
110112

111113
* GITHUB#14808: Add support for validating sources using ast-grep tool rules #14808

lucene/core/src/test/org/apache/lucene/TestAssertions.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ public boolean incrementToken() {
4646
public void testTokenStreams() {
4747
new TestTokenStream1();
4848
new TestTokenStream2();
49-
try {
50-
new TestTokenStream3();
51-
if (assertsAreEnabled) {
52-
fail("TestTokenStream3 should fail assertion");
53-
}
54-
} catch (AssertionError _) {
55-
// expected
49+
50+
if (TEST_ASSERTS_ENABLED) {
51+
expectThrows(
52+
AssertionError.class,
53+
() -> {
54+
new TestTokenStream3();
55+
});
5656
}
5757
}
5858
}

lucene/core/src/test/org/apache/lucene/internal/hppc/TestCharHashSet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ public void testIndexMethods() {
9090
MatcherAssert.assertThat(set.indexGet(set.indexOf(keyE)), is(equalTo(keyE)));
9191
MatcherAssert.assertThat(set.indexGet(set.indexOf(key1)), is(equalTo(key1)));
9292

93-
expectThrows(
94-
AssertionError.class,
95-
() -> {
96-
set.indexGet(set.indexOf(key2));
97-
});
93+
if (TEST_ASSERTS_ENABLED) {
94+
expectThrows(
95+
AssertionError.class,
96+
() -> {
97+
set.indexGet(set.indexOf(key2));
98+
});
99+
}
98100

99101
MatcherAssert.assertThat(set.indexReplace(set.indexOf(keyE), keyE), is(equalTo(keyE)));
100102
MatcherAssert.assertThat(set.indexReplace(set.indexOf(key1), key1), is(equalTo(key1)));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestCharObjectHashMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,13 @@ public void testIndexMethods() {
177177
assertEquals(value1, map.indexGet(map.indexOf(keyE)));
178178
assertEquals(value2, map.indexGet(map.indexOf(key1)));
179179

180-
expectThrows(
181-
AssertionError.class,
182-
() -> {
183-
map.indexGet(map.indexOf(key2));
184-
});
180+
if (TEST_ASSERTS_ENABLED) {
181+
expectThrows(
182+
AssertionError.class,
183+
() -> {
184+
map.indexGet(map.indexOf(key2));
185+
});
186+
}
185187

186188
assertEquals(value1, map.indexReplace(map.indexOf(keyE), value3));
187189
assertEquals(value2, map.indexReplace(map.indexOf(key1), value4));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestIntDoubleHashMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ public void testIndexMethods() {
173173
assertEquals2(value1, map.indexGet(map.indexOf(keyE)));
174174
assertEquals2(value2, map.indexGet(map.indexOf(key1)));
175175

176-
expectThrows(
177-
AssertionError.class,
178-
() -> {
179-
map.indexGet(map.indexOf(key2));
180-
});
176+
if (TEST_ASSERTS_ENABLED) {
177+
expectThrows(
178+
AssertionError.class,
179+
() -> {
180+
map.indexGet(map.indexOf(key2));
181+
});
182+
}
181183

182184
assertEquals2(value1, map.indexReplace(map.indexOf(keyE), value3));
183185
assertEquals2(value2, map.indexReplace(map.indexOf(key1), value4));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestIntFloatHashMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,13 @@ public void testIndexMethods() {
173173
assertEquals2(value1, map.indexGet(map.indexOf(keyE)));
174174
assertEquals2(value2, map.indexGet(map.indexOf(key1)));
175175

176-
expectThrows(
177-
AssertionError.class,
178-
() -> {
179-
map.indexGet(map.indexOf(key2));
180-
});
176+
if (TEST_ASSERTS_ENABLED) {
177+
expectThrows(
178+
AssertionError.class,
179+
() -> {
180+
map.indexGet(map.indexOf(key2));
181+
});
182+
}
181183

182184
assertEquals2(value1, map.indexReplace(map.indexOf(keyE), value3));
183185
assertEquals2(value2, map.indexReplace(map.indexOf(key1), value4));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestIntHashSet.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,13 @@ public void testIndexMethods() {
9090
MatcherAssert.assertThat(set.indexGet(set.indexOf(keyE)), is(equalTo(keyE)));
9191
MatcherAssert.assertThat(set.indexGet(set.indexOf(key1)), is(equalTo(key1)));
9292

93-
expectThrows(
94-
AssertionError.class,
95-
() -> {
96-
set.indexGet(set.indexOf(key2));
97-
});
93+
if (TEST_ASSERTS_ENABLED) {
94+
expectThrows(
95+
AssertionError.class,
96+
() -> {
97+
set.indexGet(set.indexOf(key2));
98+
});
99+
}
98100

99101
MatcherAssert.assertThat(set.indexReplace(set.indexOf(keyE), keyE), is(equalTo(keyE)));
100102
MatcherAssert.assertThat(set.indexReplace(set.indexOf(key1), key1), is(equalTo(key1)));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestIntIntHashMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,13 @@ public void testIndexMethods() {
152152
assertEquals(value1, map.indexGet(map.indexOf(keyE)));
153153
assertEquals(value2, map.indexGet(map.indexOf(key1)));
154154

155-
expectThrows(
156-
AssertionError.class,
157-
() -> {
158-
map.indexGet(map.indexOf(key2));
159-
});
155+
if (TEST_ASSERTS_ENABLED) {
156+
expectThrows(
157+
AssertionError.class,
158+
() -> {
159+
map.indexGet(map.indexOf(key2));
160+
});
161+
}
160162

161163
assertEquals(value1, map.indexReplace(map.indexOf(keyE), value3));
162164
assertEquals(value2, map.indexReplace(map.indexOf(key1), value4));

lucene/core/src/test/org/apache/lucene/internal/hppc/TestIntLongHashMap.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,13 @@ public void testIndexMethods() {
184184
assertEquals(value1, map.indexGet(map.indexOf(keyE)));
185185
assertEquals(value2, map.indexGet(map.indexOf(key1)));
186186

187-
expectThrows(
188-
AssertionError.class,
189-
() -> {
190-
map.indexGet(map.indexOf(key2));
191-
});
187+
if (TEST_ASSERTS_ENABLED) {
188+
expectThrows(
189+
AssertionError.class,
190+
() -> {
191+
map.indexGet(map.indexOf(key2));
192+
});
193+
}
192194

193195
assertEquals(value1, map.indexReplace(map.indexOf(keyE), value3));
194196
assertEquals(value2, map.indexReplace(map.indexOf(key1), value4));

0 commit comments

Comments
 (0)