Skip to content

Conversation

@medhatiwari
Copy link
Contributor

Jira Ticket

This PR fixes a test case failure in BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException_when_message_is_an_invalid_size when run on Big Endian systems like s390x.

Root Cause:
The test uses BitConverter.GetBytes(length) which encodes the integer in system-native endianness. MongoDB’s wire protocol, however, requires that all integer fields be little-endian. When the test runs on a Big Endian system, the message size is encoded incorrectly, leading the driver to misinterpret the message size and wait for more data, resulting in a TimeoutException.

Fix:
fix the test by reversing the byte array explicitly if BitConverter.IsLittleEndian == false. This ensures consistent little-endian encoding regardless of the platform.

Alternative:
Instead of using BitConverter and reversing bytes manually, we could replace the logic with BinaryPrimitives.WriteInt32LittleEndian, which guarantees correct little-endian encoding

Span<byte> bytes = stackalloc byte[4];
BinaryPrimitives.WriteInt32LittleEndian(bytes, length);
stream.Write(bytes);

cc: @giritrivedi

…ormatException fails with TimeoutException

Signed-off-by: Medha Tiwari <[email protected]>
@medhatiwari medhatiwari requested a review from a team as a code owner June 19, 2025 09:55
@medhatiwari medhatiwari requested review from papafe and removed request for a team June 19, 2025 09:55
@papafe papafe added the chore Non–user-facing code changes (tests, build scripts, etc.). label Jun 23, 2025
using (var stream = new BlockingMemoryStream())
{
var bytes = BitConverter.GetBytes(length);
if (!BitConerter.IsLittleEndian)
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is a spelling mistake here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, typo indeed, fixed it

@medhatiwari medhatiwari requested a review from papafe June 24, 2025 05:30
@papafe papafe requested a review from BorisDog June 24, 2025 16:25
@medhatiwari medhatiwari changed the title CSHARP-5621 Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException CSHARP-5621: Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException Jun 24, 2025
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM

@BorisDog BorisDog merged commit 72e05dd into mongodb:main Jun 24, 2025
32 of 36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Non–user-facing code changes (tests, build scripts, etc.).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants