Skip to content

Commit 0aab8ce

Browse files
CSHARP-3744: Add server connectionId to command monitoring events. (#637)
CSHARP-3744: Add server connectionId to command monitoring events.
1 parent aaf839f commit 0aab8ce

File tree

6 files changed

+335
-1
lines changed

6 files changed

+335
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"description": "pre-42-server-connection-id",
3+
"schemaVersion": "1.6",
4+
"runOnRequirements": [
5+
{
6+
"maxServerVersion": "4.0.99"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client",
13+
"observeEvents": [
14+
"commandStartedEvent",
15+
"commandSucceededEvent",
16+
"commandFailedEvent"
17+
]
18+
}
19+
},
20+
{
21+
"database": {
22+
"id": "database",
23+
"client": "client",
24+
"databaseName": "server-connection-id-tests"
25+
}
26+
},
27+
{
28+
"collection": {
29+
"id": "collection",
30+
"database": "database",
31+
"collectionName": "coll"
32+
}
33+
}
34+
],
35+
"initialData": [
36+
{
37+
"databaseName": "server-connection-id-tests",
38+
"collectionName": "coll",
39+
"documents": []
40+
}
41+
],
42+
"tests": [
43+
{
44+
"description": "command events do not include server connection id",
45+
"operations": [
46+
{
47+
"name": "insertOne",
48+
"object": "collection",
49+
"arguments": {
50+
"document": {
51+
"x": 1
52+
}
53+
}
54+
},
55+
{
56+
"name": "find",
57+
"object": "collection",
58+
"arguments": {
59+
"filter": {
60+
"$or": true
61+
}
62+
},
63+
"expectError": {
64+
"isError": true
65+
}
66+
}
67+
],
68+
"expectEvents": [
69+
{
70+
"client": "client",
71+
"events": [
72+
{
73+
"commandStartedEvent": {
74+
"commandName": "insert",
75+
"hasServerConnectionId": false
76+
}
77+
},
78+
{
79+
"commandSucceededEvent": {
80+
"commandName": "insert",
81+
"hasServerConnectionId": false
82+
}
83+
},
84+
{
85+
"commandStartedEvent": {
86+
"commandName": "find",
87+
"hasServerConnectionId": false
88+
}
89+
},
90+
{
91+
"commandFailedEvent": {
92+
"commandName": "find",
93+
"hasServerConnectionId": false
94+
}
95+
}
96+
]
97+
}
98+
]
99+
}
100+
]
101+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
description: "pre-42-server-connection-id"
2+
3+
schemaVersion: "1.6"
4+
5+
runOnRequirements:
6+
- maxServerVersion: "4.0.99"
7+
8+
createEntities:
9+
- client:
10+
id: &client client
11+
observeEvents:
12+
- commandStartedEvent
13+
- commandSucceededEvent
14+
- commandFailedEvent
15+
- database:
16+
id: &database database
17+
client: *client
18+
databaseName: &databaseName server-connection-id-tests
19+
- collection:
20+
id: &collection collection
21+
database: *database
22+
collectionName: &collectionName coll
23+
24+
initialData:
25+
- databaseName: *databaseName
26+
collectionName: *collectionName
27+
documents: []
28+
29+
tests:
30+
- description: "command events do not include server connection id"
31+
operations:
32+
- name: insertOne
33+
object: *collection
34+
arguments:
35+
document: { x: 1 }
36+
- name: find
37+
object: *collection
38+
arguments:
39+
filter: { $or: true }
40+
expectError:
41+
isError: true
42+
expectEvents:
43+
- client: *client
44+
events:
45+
- commandStartedEvent:
46+
commandName: insert
47+
hasServerConnectionId: false
48+
- commandSucceededEvent:
49+
commandName: insert
50+
hasServerConnectionId: false
51+
- commandStartedEvent:
52+
commandName: find
53+
hasServerConnectionId: false
54+
- commandFailedEvent:
55+
commandName: find
56+
hasServerConnectionId: false
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"description": "server-connection-id",
3+
"schemaVersion": "1.6",
4+
"runOnRequirements": [
5+
{
6+
"minServerVersion": "4.2"
7+
}
8+
],
9+
"createEntities": [
10+
{
11+
"client": {
12+
"id": "client",
13+
"observeEvents": [
14+
"commandStartedEvent",
15+
"commandSucceededEvent",
16+
"commandFailedEvent"
17+
]
18+
}
19+
},
20+
{
21+
"database": {
22+
"id": "database",
23+
"client": "client",
24+
"databaseName": "server-connection-id-tests"
25+
}
26+
},
27+
{
28+
"collection": {
29+
"id": "collection",
30+
"database": "database",
31+
"collectionName": "coll"
32+
}
33+
}
34+
],
35+
"initialData": [
36+
{
37+
"databaseName": "server-connection-id-tests",
38+
"collectionName": "coll",
39+
"documents": []
40+
}
41+
],
42+
"tests": [
43+
{
44+
"description": "command events include server connection id",
45+
"operations": [
46+
{
47+
"name": "insertOne",
48+
"object": "collection",
49+
"arguments": {
50+
"document": {
51+
"x": 1
52+
}
53+
}
54+
},
55+
{
56+
"name": "find",
57+
"object": "collection",
58+
"arguments": {
59+
"filter": {
60+
"$or": true
61+
}
62+
},
63+
"expectError": {
64+
"isError": true
65+
}
66+
}
67+
],
68+
"expectEvents": [
69+
{
70+
"client": "client",
71+
"events": [
72+
{
73+
"commandStartedEvent": {
74+
"commandName": "insert",
75+
"hasServerConnectionId": true
76+
}
77+
},
78+
{
79+
"commandSucceededEvent": {
80+
"commandName": "insert",
81+
"hasServerConnectionId": true
82+
}
83+
},
84+
{
85+
"commandStartedEvent": {
86+
"commandName": "find",
87+
"hasServerConnectionId": true
88+
}
89+
},
90+
{
91+
"commandFailedEvent": {
92+
"commandName": "find",
93+
"hasServerConnectionId": true
94+
}
95+
}
96+
]
97+
}
98+
]
99+
}
100+
]
101+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
description: "server-connection-id"
2+
3+
schemaVersion: "1.6"
4+
5+
runOnRequirements:
6+
- minServerVersion: "4.2"
7+
8+
createEntities:
9+
- client:
10+
id: &client client
11+
observeEvents:
12+
- commandStartedEvent
13+
- commandSucceededEvent
14+
- commandFailedEvent
15+
- database:
16+
id: &database database
17+
client: *client
18+
databaseName: &databaseName server-connection-id-tests
19+
- collection:
20+
id: &collection collection
21+
database: *database
22+
collectionName: &collectionName coll
23+
24+
initialData:
25+
- databaseName: *databaseName
26+
collectionName: *collectionName
27+
documents: []
28+
29+
tests:
30+
- description: "command events include server connection id"
31+
operations:
32+
- name: insertOne
33+
object: *collection
34+
arguments:
35+
document: { x: 1 }
36+
- name: find
37+
object: *collection
38+
arguments:
39+
filter: { $or: true }
40+
expectError:
41+
isError: true
42+
expectEvents:
43+
- client: *client
44+
events:
45+
- commandStartedEvent:
46+
commandName: insert
47+
hasServerConnectionId: true
48+
- commandSucceededEvent:
49+
commandName: insert
50+
hasServerConnectionId: true
51+
- commandStartedEvent:
52+
commandName: find
53+
hasServerConnectionId: true
54+
- commandFailedEvent:
55+
commandName: find
56+
hasServerConnectionId: true

tests/MongoDB.Driver.Tests/UnifiedTestOperations/Matchers/UnifiedEventMatcher.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using MongoDB.Bson;
2121
using MongoDB.Bson.IO;
2222
using MongoDB.Bson.TestHelpers.XunitExtensions;
23+
using MongoDB.Driver.Core.Connections;
2324
using MongoDB.Driver.Core.Events;
2425
using Xunit.Sdk;
2526

@@ -113,6 +114,9 @@ private void AssertEvents(List<object> actualEvents, BsonArray expectedEventsDoc
113114
case "hasServiceId":
114115
commandStartedEvent.ServiceId.Should().Match<ObjectId?>(s => s.HasValue == element.Value.ToBoolean());
115116
break;
117+
case "hasServerConnectionId":
118+
AssertHasServerConnectionId(commandStartedEvent.ConnectionId, element.Value.ToBoolean());
119+
break;
116120
default:
117121
throw new FormatException($"Unexpected commandStartedEvent field: '{element.Name}'.");
118122
}
@@ -133,6 +137,9 @@ private void AssertEvents(List<object> actualEvents, BsonArray expectedEventsDoc
133137
case "hasServiceId":
134138
commandSucceededEvent.ServiceId.Should().Match<ObjectId?>(s => s.HasValue == element.Value.ToBoolean());
135139
break;
140+
case "hasServerConnectionId":
141+
AssertHasServerConnectionId(commandSucceededEvent.ConnectionId, element.Value.ToBoolean());
142+
break;
136143
default:
137144
throw new FormatException($"Unexpected commandStartedEvent field: '{element.Name}'.");
138145
}
@@ -150,6 +157,9 @@ private void AssertEvents(List<object> actualEvents, BsonArray expectedEventsDoc
150157
case "hasServiceId":
151158
commandFailedEvent.ServiceId.Should().Match<ObjectId?>(s => s.HasValue == element.Value.ToBoolean());
152159
break;
160+
case "hasServerConnectionId":
161+
AssertHasServerConnectionId(commandFailedEvent.ConnectionId, element.Value.ToBoolean());
162+
break;
153163
default:
154164
throw new FormatException($"Unexpected commandStartedEvent field: '{element.Name}'.");
155165
}
@@ -221,6 +231,16 @@ private void AssertEvents(List<object> actualEvents, BsonArray expectedEventsDoc
221231
throw new FormatException($"Unrecognized event type: '{expectedEventType}'.");
222232
}
223233
}
234+
235+
void AssertHasServerConnectionId(ConnectionId connectionId, bool value)
236+
{
237+
// in c# we have fallback logic to get a server connectionId based on an additional getLastError call which is not expected by the spec.
238+
// So even though servers less than 4.2 don't provide connectionId, we still have this value through getLastError, so don't assert hasServerConnectionId=false.
239+
if (value)
240+
{
241+
connectionId.ServerValue.Should().HaveValue();
242+
}
243+
}
224244
}
225245

226246
private string GetAssertionErrorMessage(List<object> actualEvents, BsonArray expectedEventsDocuments)

tests/MongoDB.Driver.Tests/UnifiedTestOperations/UnifiedTestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public void Run(
8787

8888
var schemaSemanticVersion = SemanticVersion.Parse(schemaVersion);
8989
if (schemaSemanticVersion < new SemanticVersion(1, 0, 0) ||
90-
schemaSemanticVersion > new SemanticVersion(1, 5, 0))
90+
schemaSemanticVersion > new SemanticVersion(1, 6, 0))
9191
{
9292
throw new FormatException($"Schema version '{schemaVersion}' is not supported.");
9393
}

0 commit comments

Comments
 (0)