Skip to content

Commit 0a57ca6

Browse files
authored
Improvements for connection desynchronization and hardening of error handling with connections (#1998)
Close desynchronized connections and harden response handling Detect responseTo mismatches before processing response bodies so session state is not advanced from an unrelated reply, and close the connection to prevent desynchronized streams from returning to the pool. Ensure read and post-read failures close connections only when appropriate, release buffers before closing, preserve fatal Error propagation, and keep command monitoring to one started event and one terminal event across sync and async paths. Add unit coverage for responseTo mismatch handling, reusable exception paths, buffer release ordering, and sync/async error handling. Document related JUnit test conventions. JAVA-6210
1 parent 0276f63 commit 0a57ca6

4 files changed

Lines changed: 2059 additions & 48 deletions

File tree

.agents/references/testing-guide.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ description: Testing frameworks, conventions, and commands for the MongoDB Java
2929
- Descriptive method names: `shouldReturnEmptyListWhenNoDocumentsMatch()` not `test1()`
3030
- Use `@DisplayName` for human-readable names
3131
- Clean up test data in `@AfterEach` / `cleanup()` to prevent pollution
32+
- Import types at the top of the file — do not use inline fully-qualified names
33+
(`com.mongodb.connection.AsyncCompletionHandler`, `java.util.List`) in signatures or bodies
34+
- Use `AtomicInteger` / `AtomicBoolean` for mutable state captured by lambdas or anonymous
35+
classes — not single-element array wrappers (`int[]`, `boolean[]`)
36+
- Assert the most specific exception type the code guarantees
37+
(`assertInstanceOf(MongoCommandException.class, e)`, not `Exception.class`)
38+
- When testing that a side effect happens on condition X (e.g. connection closed on stream
39+
desync), also test that it does NOT happen on the neighbouring condition Y (e.g. connection
40+
stays open on a command error) — one-sided tests let behavioral regressions through
3241

3342
## Running Tests
3443

0 commit comments

Comments
 (0)