Skip to content

CSHARP-5621 Fix BinaryConnectionTests.ReceiveMessage_should_throw_a_FormatException fails with TimeoutException #1715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant