Add concurrency test for processReadyKeys() to detect potential thread-safety issues #380
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.
This PR introduces a unit test that verifies the thread-safety of NioClient.processReadyKeys() under concurrent channel registration and readiness.
The test assumes correct behavior — i.e., no ConcurrentModificationException should be thrown when new channels become ready while processReadyKeys() is iterating over selector.selectedKeys(). If such an exception is thrown, the test fails, indicating a concurrency flaw in the current implementation.
Key points:
Uses a real selector loop via NioClient.selector(), with no changes to production logic.
Concurrently registers channels and makes them ready using Pipe.sink().write() to trigger select() updates.
Catches ConcurrentModificationException using a custom Thread.UncaughtExceptionHandler.
Includes processing delay to increase overlap and expose the race condition.
| Note: As this is a concurrency test, it may not fail consistently across runs or environments. Flakiness is expected until the underlying issue is fixed.
This test is intended as a reproducible regression check for an existing bug and will serve as a validation gate once a thread-safe fix is applied to processReadyKeys().