[CSHARP-3515] Sporadic tests failures: TestGuidRefId Created: 31/Mar/21  Updated: 28/Oct/23  Resolved: 16/Apr/21

Status: Closed
Project: C# Driver
Component/s: BSON, Testing
Affects Version/s: None
Fix Version/s: 2.13.0

Type: Bug Priority: Major - P3
Reporter: Mikalai Mazurenka (Inactive) Assignee: Robert Stam
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Epic Link: Improve Testing

 Description   

MongoDB.Driver.Tests.MongoDBRefTests.TestGuidRefId fails occasionally.

[2021/03/30 02:09:17.782] [xUnit.net 00:03:25.16]     MongoDB.Driver.Tests.MongoDBRefTests.TestGuidRefId [FAIL]
[2021/03/30 02:09:17.788]   X MongoDB.Driver.Tests.MongoDBRefTests.TestGuidRefId [1ms]
[2021/03/30 02:09:17.788]   Error Message:
[2021/03/30 02:09:17.788]    MongoDB.Bson.BsonSerializationException : Cannot serialize BsonBinaryData with GuidRepresentation Unspecified to destination with GuidRepresentation CSharpLegacy.

Example1
Example2
Example3



 Comments   
Comment by Githook User [ 16/Apr/21 ]

Author:

{'name': 'rstam', 'email': 'robert@robertstam.org', 'username': 'rstam'}

Message: CSHARP-3515: Sporadic tests failures: TestGuidRefId
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/ec00a97e2a60e2867c85ff870083bf79a2967fbe

Comment by Robert Stam [ 07/Apr/21 ]

I have not been able to reproduce this, but the theory is that one of the following global state properties has an unexpected value when the test is run:

BsonBinaryReaderSettings.Defaults
BsonBinaryWriterSettings.Defaults
BsonDocumentReaderSettings.Defaults
BsonDocumentWriterSettings.Defaults
JsonReaderSettings.Defaults
JsonWriterSettings.Defaults 

These static properties were defined a very long time ago, and are lazily initialized. When initialized they capture some current values from BsonDefaults (which is also global state).

Today we know that we should avoid global state, but this global state exists for historical reasons.

The reason this global state might have unexpected values is because xUnit runs tests in random order, so depending on the order the tests were run in this global state might have different values.

The proposed fix for this test is to ensure that this global state is saved at the beginning of the test, is set to a well defined state before the test is run, and restored to the prior state at the end of the test. This is accomplished by adding these values to the existing `GuidMode` class.

This fix is general and can apply to any other test that also depends on this global state.

Generated at Wed Feb 07 21:45:30 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.