Skip to content

Implement IsTestThread to fix flaky TestFlushExceptions - #1382

Merged
paulirwin merged 2 commits into
apache:masterfrom
paulirwin:issue/cms-flush-flaky
Jun 24, 2026
Merged

Implement IsTestThread to fix flaky TestFlushExceptions#1382
paulirwin merged 2 commits into
apache:masterfrom
paulirwin:issue/cms-flush-flaky

Conversation

@paulirwin

Copy link
Copy Markdown
Contributor
  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a change, please open an issue to discuss the change or find an existing issue.

Implement IsTestThread to fix flaky TestFlushExceptions

(Related to flaky failure on #1311 / #1284)

Description

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 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.

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>
@paulirwin
paulirwin requested review from NightOwl888 and Copilot June 23, 2026 14:56
@paulirwin paulirwin added the notes:bug-fix Contains a fix for a bug label Jun 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 a finally block).
  • Implement IsTestThread to return true only for the captured test thread.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@NightOwl888 NightOwl888 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved with optional additional support.

Comment thread src/Lucene.Net.TestFramework/Util/LuceneTestCase.cs Outdated
@paulirwin
paulirwin merged commit 3fe4c42 into apache:master Jun 24, 2026
209 checks passed
@paulirwin
paulirwin deleted the issue/cms-flush-flaky branch June 24, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:bug-fix Contains a fix for a bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants