Skip to content

Commit 2cdbaa5

Browse files
authored
CSHARP-4150: Always report 'wallTime' in the change stream event output. (#802)
1 parent fd9e563 commit 2cdbaa5

File tree

6 files changed

+98
-1
lines changed

6 files changed

+98
-1
lines changed

src/MongoDB.Driver.Core/ChangeStreamDocument.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
using System;
1617
using MongoDB.Bson;
1718
using MongoDB.Bson.Serialization;
1819
using MongoDB.Bson.Serialization.Attributes;
@@ -152,5 +153,13 @@ public TDocument FullDocumentBeforeChange
152153
/// The update description.
153154
/// </value>
154155
public ChangeStreamUpdateDescription UpdateDescription => GetValue<ChangeStreamUpdateDescription>(nameof(UpdateDescription), null);
156+
157+
/// <summary>
158+
/// Gets the wall time of the change stream event.
159+
/// </summary>
160+
/// <value>
161+
/// The wall time.
162+
/// </value>
163+
public DateTime? WallTime => GetValue<DateTime?>(nameof(WallTime), null);
155164
}
156165
}

src/MongoDB.Driver.Core/ChangeStreamDocumentSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public ChangeStreamDocumentSerializer(
5050
RegisterMember("RenameTo", "to", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);
5151
RegisterMember("ResumeToken", "_id", BsonDocumentSerializer.Instance);
5252
RegisterMember("UpdateDescription", "updateDescription", ChangeStreamUpdateDescriptionSerializer.Instance);
53+
RegisterMember("WallTime", "wallTime", DateTimeSerializer.UtcInstance);
5354
}
5455

5556
// public methods

tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentSerializerTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public void constructor_should_initialize_instance()
3636
var result = new ChangeStreamDocumentSerializer<BsonDocument>(documentSerializer);
3737

3838
result._documentSerializer().Should().BeSameAs(documentSerializer);
39-
result._memberSerializationInfo().Count.Should().Be(10);
39+
result._memberSerializationInfo().Count.Should().Be(11);
4040
AssertRegisteredMember(result, "ClusterTime", "clusterTime", BsonTimestampSerializer.Instance);
4141
AssertRegisteredMember(result, "CollectionNamespace", "ns", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);
4242
AssertRegisteredMember(result, "DatabaseNamespace", "ns", ChangeStreamDocumentDatabaseNamespaceSerializer.Instance);
@@ -47,6 +47,7 @@ public void constructor_should_initialize_instance()
4747
AssertRegisteredMember(result, "RenameTo", "to", ChangeStreamDocumentCollectionNamespaceSerializer.Instance);
4848
AssertRegisteredMember(result, "ResumeToken", "_id", BsonDocumentSerializer.Instance);
4949
AssertRegisteredMember(result, "UpdateDescription", "updateDescription", ChangeStreamUpdateDescriptionSerializer.Instance);
50+
AssertRegisteredMember(result, "WallTime", "wallTime", DateTimeSerializer.UtcInstance);
5051
}
5152

5253
[Fact]

tests/MongoDB.Driver.Core.Tests/ChangeStreamDocumentTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,29 @@ public void UpdateDescription_should_return_null_when_not_present()
403403
result.Should().BeNull();
404404
}
405405

406+
[Fact]
407+
public void WallTime_should_return_expected_result()
408+
{
409+
var value = new DateTime(2022, 5, 25, 12, 35, 42, 144, DateTimeKind.Utc);
410+
var backingDocument = new BsonDocument { { "other", 1 }, { "wallTime", value } };
411+
var subject = CreateSubject(backingDocument: backingDocument);
412+
413+
var result = subject.WallTime;
414+
415+
result.Should().Be(value);
416+
}
417+
418+
[Fact]
419+
public void WallTime_should_return_null_when_not_present()
420+
{
421+
var backingDocument = new BsonDocument { { "other", 1 } };
422+
var subject = CreateSubject(backingDocument: backingDocument);
423+
424+
var result = subject.WallTime;
425+
426+
result.Should().NotHaveValue();
427+
}
428+
406429
// private methods
407430
private ChangeStreamDocument<BsonDocument> CreateSubject(
408431
BsonDocument backingDocument = null,

tests/MongoDB.Driver.Tests/Specifications/change-streams/tests/unified/change-streams.json

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,48 @@
17481748
]
17491749
}
17501750
]
1751+
},
1752+
{
1753+
"description": "Test wallTime field is set in a change event",
1754+
"runOnRequirements": [
1755+
{
1756+
"minServerVersion": "6.0.0"
1757+
}
1758+
],
1759+
"operations": [
1760+
{
1761+
"name": "createChangeStream",
1762+
"object": "collection0",
1763+
"arguments": {
1764+
"pipeline": []
1765+
},
1766+
"saveResultAsEntity": "changeStream0"
1767+
},
1768+
{
1769+
"name": "insertOne",
1770+
"object": "collection0",
1771+
"arguments": {
1772+
"document": {
1773+
"_id": 1,
1774+
"a": 1
1775+
}
1776+
}
1777+
},
1778+
{
1779+
"name": "iterateUntilDocumentOrError",
1780+
"object": "changeStream0",
1781+
"expectResult": {
1782+
"operationType": "insert",
1783+
"ns": {
1784+
"db": "database0",
1785+
"coll": "collection0"
1786+
},
1787+
"wallTime": {
1788+
"$$exists": true
1789+
}
1790+
}
1791+
}
1792+
]
17511793
}
17521794
]
17531795
}

tests/MongoDB.Driver.Tests/Specifications/change-streams/tests/unified/change-streams.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,24 @@ tests:
904904
pipeline: [ { $changeStream: {} } ]
905905
commandName: aggregate
906906
databaseName: *database0
907+
908+
- description: "Test wallTime field is set in a change event"
909+
runOnRequirements:
910+
- minServerVersion: "6.0.0"
911+
operations:
912+
- name: createChangeStream
913+
object: *collection0
914+
arguments: { pipeline: [] }
915+
saveResultAsEntity: &changeStream0 changeStream0
916+
- name: insertOne
917+
object: *collection0
918+
arguments:
919+
document: { "_id": 1, "a": 1 }
920+
- name: iterateUntilDocumentOrError
921+
object: *changeStream0
922+
expectResult:
923+
operationType: "insert"
924+
ns:
925+
db: *database0
926+
coll: *collection0
927+
wallTime: { $$exists: true }

0 commit comments

Comments
 (0)