Skip to content

Commit ca2bfb0

Browse files
test(NODE-6052): support assertions on topologyDescriptionChanged in expectEvents (#4089)
Co-authored-by: Aditi Khare <[email protected]>
1 parent 6abc074 commit ca2bfb0

File tree

5 files changed

+148
-21
lines changed

5 files changed

+148
-21
lines changed

test/integration/server-discovery-and-monitoring/server_discovery_and_monitoring.spec.test.ts

+17-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@ import * as path from 'path';
33
import { loadSpecTests } from '../../spec';
44
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';
55

6+
const skipTable: { pattern: string; reason: string }[] = [
7+
{
8+
pattern: 'Topology lifecycle',
9+
reason: 'TODO(NODE-5723): Need to implement DRIVERS-2711 spec change'
10+
},
11+
{
12+
pattern: 'connect with serverMonitoringMode=stream >=4.4',
13+
reason: 'TODO(NODE-6045): Ensure that first server hearbeat does not report that it is awaited'
14+
},
15+
{
16+
pattern: 'connect with serverMonitoringMode=auto >=4.4',
17+
reason: 'TODO(NODE-6045): Ensure that first server hearbeat does not report that it is awaited'
18+
}
19+
];
20+
621
describe('SDAM Unified Tests (Spec)', function () {
722
const specTests = loadSpecTests(path.join('server-discovery-and-monitoring', 'unified'));
823
runUnifiedSuite(specTests, test => {
9-
if (['Topology lifecycle'].includes(test.description)) {
10-
return 'see NODE-5723';
24+
for (const { pattern, reason } of skipTable) {
25+
if (test.description.includes(pattern)) return reason;
1126
}
1227
return false;
1328
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"description": "expectedEventsForClient-topologyDescriptionChangedEvent",
3+
"schemaVersion": "1.20",
4+
"runOnRequirements": [
5+
{
6+
"topologies": [
7+
"replicaset"
8+
],
9+
"minServerVersion": "4.4"
10+
}
11+
],
12+
"tests": [
13+
{
14+
"description": "can assert on values of newDescription and previousDescription fields",
15+
"operations": [
16+
{
17+
"name": "createEntities",
18+
"object": "testRunner",
19+
"arguments": {
20+
"entities": [
21+
{
22+
"client": {
23+
"id": "client",
24+
"uriOptions": {
25+
"directConnection": true
26+
},
27+
"observeEvents": [
28+
"topologyDescriptionChangedEvent"
29+
]
30+
}
31+
}
32+
]
33+
}
34+
},
35+
{
36+
"name": "waitForEvent",
37+
"object": "testRunner",
38+
"arguments": {
39+
"client": "client",
40+
"event": {
41+
"topologyDescriptionChangedEvent": {}
42+
},
43+
"count": 1
44+
}
45+
}
46+
],
47+
"expectEvents": [
48+
{
49+
"client": "client",
50+
"eventType": "sdam",
51+
"ignoreExtraEvents": true,
52+
"events": [
53+
{
54+
"topologyDescriptionChangedEvent": {
55+
"previousDescription": {
56+
"type": "Unknown"
57+
},
58+
"newDescription": {
59+
"type": "Single"
60+
}
61+
}
62+
}
63+
]
64+
}
65+
]
66+
}
67+
]
68+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
description: "expectedEventsForClient-topologyDescriptionChangedEvent"
2+
3+
schemaVersion: "1.20"
4+
5+
runOnRequirements:
6+
- topologies:
7+
- replicaset
8+
minServerVersion: "4.4" # awaitable hello
9+
10+
tests:
11+
- description: "can assert on values of newDescription and previousDescription fields"
12+
operations:
13+
- name: createEntities
14+
object: testRunner
15+
arguments:
16+
entities:
17+
- client:
18+
id: &client client
19+
uriOptions:
20+
directConnection: true
21+
observeEvents:
22+
- topologyDescriptionChangedEvent
23+
- name: waitForEvent
24+
object: testRunner
25+
arguments:
26+
client: *client
27+
event:
28+
topologyDescriptionChangedEvent: {}
29+
count: 1
30+
expectEvents:
31+
- client: *client
32+
eventType: sdam
33+
ignoreExtraEvents: true
34+
events:
35+
- topologyDescriptionChangedEvent:
36+
previousDescription:
37+
type: "Unknown"
38+
newDescription:
39+
type: "Single"
40+

test/tools/unified-spec-runner/match.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -561,63 +561,65 @@ function compareEvents(
561561
);
562562
}
563563
}
564-
return;
565564
} else if (expectedEvent.serverHeartbeatStartedEvent) {
566565
expect(actualEvent).to.be.instanceOf(ServerHeartbeatStartedEvent);
567566
const expectedSdamEvent = expectedEvent.serverHeartbeatStartedEvent;
568567
for (const property of Object.keys(expectedSdamEvent)) {
569568
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
570569
}
571-
return;
572570
} else if (expectedEvent.serverHeartbeatFailedEvent) {
573571
expect(actualEvent).to.be.instanceOf(ServerHeartbeatFailedEvent);
574572
const expectedSdamEvent = expectedEvent.serverHeartbeatFailedEvent;
575573
for (const property of Object.keys(expectedSdamEvent)) {
576574
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
577575
}
578-
return;
579576
} else if (expectedEvent.serverHeartbeatSucceededEvent) {
580577
expect(actualEvent).to.be.instanceOf(ServerHeartbeatSucceededEvent);
581578
const expectedSdamEvent = expectedEvent.serverHeartbeatSucceededEvent;
582579
for (const property of Object.keys(expectedSdamEvent)) {
583580
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
584581
}
585-
return;
586582
} else if (expectedEvent.serverOpeningEvent) {
587583
expect(actualEvent).to.be.instanceOf(ServerOpeningEvent);
588584
const expectedSdamEvent = expectedEvent.serverOpeningEvent;
589585
for (const property of Object.keys(expectedSdamEvent)) {
590586
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
591587
}
592-
return;
593588
} else if (expectedEvent.serverClosedEvent) {
594589
expect(actualEvent).to.be.instanceOf(ServerClosedEvent);
595590
const expectedSdamEvent = expectedEvent.serverClosedEvent;
596591
for (const property of Object.keys(expectedSdamEvent)) {
597592
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
598593
}
599-
return;
600594
} else if (expectedEvent.topologyOpeningEvent) {
601595
expect(actualEvent).to.be.instanceOf(TopologyOpeningEvent);
602596
const expectedSdamEvent = expectedEvent.topologyOpeningEvent;
603597
for (const property of Object.keys(expectedSdamEvent)) {
604598
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
605599
}
606-
return;
607-
} else if (expectedEvent.topologyClosingEvent) {
600+
} else if (expectedEvent.topologyClosedEvent) {
608601
expect(actualEvent).to.be.instanceOf(TopologyClosedEvent);
609-
const expectedSdamEvent = expectedEvent.topologyClosingEvent;
602+
const expectedSdamEvent = expectedEvent.topologyClosedEvent;
610603
for (const property of Object.keys(expectedSdamEvent)) {
611604
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
612605
}
613-
return;
614606
} else if (expectedEvent.topologyDescriptionChangedEvent) {
615607
expect(actualEvent).to.be.instanceOf(TopologyDescriptionChangedEvent);
608+
609+
const actualTopChangedEvent = actualEvent as TopologyDescriptionChangedEvent;
616610
const expectedSdamEvent = expectedEvent.topologyDescriptionChangedEvent;
617-
for (const property of Object.keys(expectedSdamEvent)) {
618-
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
611+
612+
if (expectedSdamEvent.previousDescription?.type) {
613+
expect(actualTopChangedEvent.previousDescription.type).to.equal(
614+
expectedSdamEvent.previousDescription.type
615+
);
616+
}
617+
618+
if (expectedSdamEvent.newDescription?.type) {
619+
expect(actualTopChangedEvent.newDescription.type).to.equal(
620+
expectedSdamEvent.newDescription.type
621+
);
619622
}
620-
return;
621623
} else {
622624
expect.fail(`Encountered unexpected event - ${inspect(actualEvent)}`);
623625
}

test/tools/unified-spec-runner/schema.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type {
77
ServerApiVersion,
88
SeverityLevel,
99
TagSet,
10+
TopologyType,
1011
W
1112
} from '../../mongodb';
1213
import { type TestConfiguration } from '../runner/config';
@@ -95,21 +96,21 @@ export interface UnifiedSuite {
9596
tests: Test[];
9697
_yamlAnchors?: Document;
9798
}
98-
export const TopologyType = Object.freeze({
99+
export const TopologyName = Object.freeze({
99100
single: 'single',
100101
replicaset: 'replicaset',
101102
sharded: 'sharded',
102103
shardedReplicaset: 'sharded-replicaset',
103104
loadBalanced: 'load-balanced'
104105
} as const);
105106

106-
export type TopologyId = (typeof TopologyType)[keyof typeof TopologyType];
107+
export type TopologyName = (typeof TopologyName)[keyof typeof TopologyName];
107108
export interface RunOnRequirement {
108109
serverless?: 'forbid' | 'allow' | 'require';
109110
auth?: boolean;
110111
maxServerVersion?: string;
111112
minServerVersion?: string;
112-
topologies?: TopologyId[];
113+
topologies?: TopologyName[];
113114
serverParameters?: Document;
114115
csfle?: boolean;
115116
}
@@ -314,6 +315,7 @@ export interface ExpectedCmapEvent {
314315
connectionCheckedOutEvent?: Record<string, never>;
315316
connectionCheckedInEvent?: Record<string, never>;
316317
}
318+
317319
export interface ExpectedSdamEvent {
318320
serverDescriptionChangedEvent?: {
319321
previousDescription?: {
@@ -336,16 +338,16 @@ export interface ExpectedSdamEvent {
336338
topologyDescriptionChangedEvent?: {
337339
topologyId?: any;
338340
previousDescription?: {
339-
type?: string;
341+
type?: TopologyType;
340342
};
341343
newDescription?: {
342-
type?: string;
344+
type?: TopologyType;
343345
};
344346
};
345347
topologyOpeningEvent?: {
346348
topologyId?: any;
347349
};
348-
topologyClosingEvent?: {
350+
topologyClosedEvent?: {
349351
topologyId?: any;
350352
};
351353
serverOpeningEvent?: {

0 commit comments

Comments
 (0)