Skip to content

Commit dac4a2b

Browse files
paulirwinclaude
andcommitted
Fix random TestRandomChains failures by excluding broken-offsets producers, #1072
TestRandomChains could fail intermittently (deterministic given a seed) with "startOffset must be non-negative, and endOffset must be >= startOffset" thrown from OffsetAttribute.SetOffset via ShingleFilter.IncrementToken. A broken-offsets producer (e.g. WordDelimiterFilter) emits backwards offsets that a downstream word-combiner (ShingleFilter) then passes to SetOffset, which rejects start > end. The 8 known broken-offsets producers were only listed in brokenOffsetsConstructors, which merely relaxes ValidatingTokenFilter's checks (offsetsAreCorrect=false) but still lets the offending filter run and feed bad offsets downstream. Upstream Lucene resolved this in 9.1 (LUCENE-10352) by excluding these classes from random chains via the @IgnoreRandomChains annotation. Until that is backported, also add them to brokenConstructors with ALWAYS so they are fully excluded from the random chains. Also fix an operator-precedence bug in NewFilterChain's MockGraph/MockRandomLookahead guard (misplaced parenthesis) to match upstream Java, and remove [AwaitsFix] from TestRandomChains_ and TestRandomChainsWithLargeStrings now that they pass reliably. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e2e5d97 commit dac4a2b

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

src/Lucene.Net.Tests.Analysis.Common/Analysis/Core/TestRandomChains.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ static TestRandomChains()
151151
foreach (ConstructorInfo ctor in c.GetConstructors())
152152
{
153153
brokenOffsetsConstructors[ctor] = ALWAYS;
154+
155+
// LUCENENET specific (#1072): Also fully exclude these broken-offsets producers
156+
// from the random chains, not just relax offset validation. brokenOffsetsConstructors
157+
// only flips offsetsAreCorrect=false (which suppresses ValidatingTokenFilter's checks),
158+
// but the offending filter still runs and can feed backwards offsets into a downstream
159+
// word-combiner (e.g. ShingleFilter), which then throws from OffsetAttribute.SetOffset.
160+
// Upstream Lucene resolved this in 9.1 (LUCENE-10352) by excluding these classes via the
161+
// @IgnoreRandomChains annotation. Until that annotation is backported, exclude them here.
162+
// TODO: Remove this when LUCENE-10352 (IgnoreRandomChains) is backported.
163+
brokenConstructors[ctor] = ALWAYS;
154164
}
155165
}
156166
}
@@ -1169,8 +1179,8 @@ private TokenFilterSpec NewFilterChain(Random random, Tokenizer tokenizer, bool
11691179
// hack: MockGraph/MockLookahead has assertions that will trip if they follow
11701180
// an offsets violator. so we can't use them after e.g. wikipediatokenizer
11711181
if (!spec.offsetsAreCorrect &&
1172-
(ctor.DeclaringType.Equals(typeof(MockGraphTokenFilter)))
1173-
|| ctor.DeclaringType.Equals(typeof(MockRandomLookaheadTokenFilter)))
1182+
(ctor.DeclaringType.Equals(typeof(MockGraphTokenFilter))
1183+
|| ctor.DeclaringType.Equals(typeof(MockRandomLookaheadTokenFilter))))
11741184
{
11751185
continue;
11761186
}
@@ -1284,7 +1294,6 @@ private static TEnum RandomEnum<TEnum>(Random random)
12841294
}
12851295

12861296
[Test]
1287-
[AwaitsFix(BugUrl = "https://github.com/apache/lucenenet/issues/271#issuecomment-973005744")] // LUCENENET TODO: this test occasionally fails
12881297
public void TestRandomChains_()
12891298
{
12901299
int numIterations = AtLeast(20);
@@ -1311,7 +1320,6 @@ public void TestRandomChains_()
13111320

13121321
// we might regret this decision...
13131322
[Test]
1314-
[AwaitsFix(BugUrl = "https://github.com/apache/lucenenet/issues/271#issuecomment-973005744")] // LUCENENET TODO: this test occasionally fails
13151323
public void TestRandomChainsWithLargeStrings()
13161324
{
13171325
int numIterations = AtLeast(20);

0 commit comments

Comments
 (0)