Implement IsTestThread to fix flaky TestFlushExceptions - #1382
Merged
Conversation
LuceneTestCase.IsTestThread was hardcoded to always return true, so the flush-failure injection in TestConcurrentMergeScheduler.TestFlushExceptions fired on background ConcurrentMergeScheduler merge threads (which also flush via CompressingStoredFieldsWriter.Flush()), not just the test thread. When a merge thread happened to be flushing during the SetDoFail()/Flush() window and Random.NextBoolean() returned true, the merge threw, surfacing as a flaky MergeException at teardown. Capture the test-case thread in SetUp() (cleared in TearDown()) in a [ThreadStatic] field and have IsTestThread compare against it, mirroring Java's ThreadAndTestNameRule.testCaseThread. Tests run with LevelOfParallelism(1), and NUnit's TimeoutCommand (active via the assembly-wide [Timeout]) runs SetUp, the test body, and TearDown on the same thread, so the capture lines up with the thread executing the test method. Background merge threads never run SetUp, so they correctly see a null test thread and are excluded. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a long-standing test-framework issue where LuceneTestCase.IsTestThread always returned true, which allowed failure injection in TestConcurrentMergeScheduler.TestFlushExceptions to fire on background merge threads and cause flaky MergeException failures.
Changes:
- Capture the current test-case thread during
LuceneTestCase.SetUp(). - Clear the captured thread during
LuceneTestCase.TearDown()(in afinallyblock). - Implement
IsTestThreadto returntrueonly for the captured test thread.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4 tasks
NightOwl888
approved these changes
Jun 23, 2026
NightOwl888
left a comment
Contributor
There was a problem hiding this comment.
Approved with optional additional support.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implement IsTestThread to fix flaky TestFlushExceptions
(Related to flaky failure on #1311 / #1284)
Description
LuceneTestCase.IsTestThreadwas hardcoded to always return true, so the flush-failure injection inTestConcurrentMergeScheduler.TestFlushExceptionsfired on background ConcurrentMergeScheduler merge threads (which also flush viaCompressingStoredFieldsWriter.Flush()), not just the test thread. When a merge thread happened to be flushing during the SetDoFail/Flush window andRandom.NextBoolean()returned true, the merge threw, surfacing as a flaky MergeException at teardown.Capture the test-case thread in SetUp (cleared in TearDown) in a
[ThreadStatic]field and have IsTestThread compare against it, mirroring Java'sThreadAndTestNameRule.testCaseThread. Tests run withLevelOfParallelism(1), and NUnit runs SetUp, the test body, and TearDown on the same thread, so the capture lines up with the thread executing the test method. Background merge threads never run SetUp, so they correctly see a null test thread and are excluded.