Skip to content

Commit

Permalink
test(NODE-6052): support assertions on topologyDescriptionChanged in …
Browse files Browse the repository at this point in the history
…expectEvents (#4089)

Co-authored-by: Aditi Khare <106987683+aditi-khare-mongoDB@users.noreply.github.com>
  • Loading branch information
W-A-James and aditi-khare-mongoDB committed Apr 25, 2024
1 parent 6abc074 commit ca2bfb0
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 21 deletions.
Expand Up @@ -3,11 +3,26 @@ import * as path from 'path';
import { loadSpecTests } from '../../spec';
import { runUnifiedSuite } from '../../tools/unified-spec-runner/runner';

const skipTable: { pattern: string; reason: string }[] = [
{
pattern: 'Topology lifecycle',
reason: 'TODO(NODE-5723): Need to implement DRIVERS-2711 spec change'
},
{
pattern: 'connect with serverMonitoringMode=stream >=4.4',
reason: 'TODO(NODE-6045): Ensure that first server hearbeat does not report that it is awaited'
},
{
pattern: 'connect with serverMonitoringMode=auto >=4.4',
reason: 'TODO(NODE-6045): Ensure that first server hearbeat does not report that it is awaited'
}
];

describe('SDAM Unified Tests (Spec)', function () {
const specTests = loadSpecTests(path.join('server-discovery-and-monitoring', 'unified'));
runUnifiedSuite(specTests, test => {
if (['Topology lifecycle'].includes(test.description)) {
return 'see NODE-5723';
for (const { pattern, reason } of skipTable) {
if (test.description.includes(pattern)) return reason;
}
return false;
});
Expand Down
@@ -0,0 +1,68 @@
{
"description": "expectedEventsForClient-topologyDescriptionChangedEvent",
"schemaVersion": "1.20",
"runOnRequirements": [
{
"topologies": [
"replicaset"
],
"minServerVersion": "4.4"
}
],
"tests": [
{
"description": "can assert on values of newDescription and previousDescription fields",
"operations": [
{
"name": "createEntities",
"object": "testRunner",
"arguments": {
"entities": [
{
"client": {
"id": "client",
"uriOptions": {
"directConnection": true
},
"observeEvents": [
"topologyDescriptionChangedEvent"
]
}
}
]
}
},
{
"name": "waitForEvent",
"object": "testRunner",
"arguments": {
"client": "client",
"event": {
"topologyDescriptionChangedEvent": {}
},
"count": 1
}
}
],
"expectEvents": [
{
"client": "client",
"eventType": "sdam",
"ignoreExtraEvents": true,
"events": [
{
"topologyDescriptionChangedEvent": {
"previousDescription": {
"type": "Unknown"
},
"newDescription": {
"type": "Single"
}
}
}
]
}
]
}
]
}
@@ -0,0 +1,40 @@
description: "expectedEventsForClient-topologyDescriptionChangedEvent"

schemaVersion: "1.20"

runOnRequirements:
- topologies:
- replicaset
minServerVersion: "4.4" # awaitable hello

tests:
- description: "can assert on values of newDescription and previousDescription fields"
operations:
- name: createEntities
object: testRunner
arguments:
entities:
- client:
id: &client client
uriOptions:
directConnection: true
observeEvents:
- topologyDescriptionChangedEvent
- name: waitForEvent
object: testRunner
arguments:
client: *client
event:
topologyDescriptionChangedEvent: {}
count: 1
expectEvents:
- client: *client
eventType: sdam
ignoreExtraEvents: true
events:
- topologyDescriptionChangedEvent:
previousDescription:
type: "Unknown"
newDescription:
type: "Single"

28 changes: 15 additions & 13 deletions test/tools/unified-spec-runner/match.ts
Expand Up @@ -561,63 +561,65 @@ function compareEvents(
);
}
}
return;
} else if (expectedEvent.serverHeartbeatStartedEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatStartedEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatStartedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.serverHeartbeatFailedEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatFailedEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatFailedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.serverHeartbeatSucceededEvent) {
expect(actualEvent).to.be.instanceOf(ServerHeartbeatSucceededEvent);
const expectedSdamEvent = expectedEvent.serverHeartbeatSucceededEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.serverOpeningEvent) {
expect(actualEvent).to.be.instanceOf(ServerOpeningEvent);
const expectedSdamEvent = expectedEvent.serverOpeningEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.serverClosedEvent) {
expect(actualEvent).to.be.instanceOf(ServerClosedEvent);
const expectedSdamEvent = expectedEvent.serverClosedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.topologyOpeningEvent) {
expect(actualEvent).to.be.instanceOf(TopologyOpeningEvent);
const expectedSdamEvent = expectedEvent.topologyOpeningEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.topologyClosingEvent) {
} else if (expectedEvent.topologyClosedEvent) {
expect(actualEvent).to.be.instanceOf(TopologyClosedEvent);
const expectedSdamEvent = expectedEvent.topologyClosingEvent;
const expectedSdamEvent = expectedEvent.topologyClosedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);
}
return;
} else if (expectedEvent.topologyDescriptionChangedEvent) {
expect(actualEvent).to.be.instanceOf(TopologyDescriptionChangedEvent);

const actualTopChangedEvent = actualEvent as TopologyDescriptionChangedEvent;
const expectedSdamEvent = expectedEvent.topologyDescriptionChangedEvent;
for (const property of Object.keys(expectedSdamEvent)) {
expect(actualEvent[property]).to.equal(expectedSdamEvent[property]);

if (expectedSdamEvent.previousDescription?.type) {
expect(actualTopChangedEvent.previousDescription.type).to.equal(
expectedSdamEvent.previousDescription.type
);
}

if (expectedSdamEvent.newDescription?.type) {
expect(actualTopChangedEvent.newDescription.type).to.equal(
expectedSdamEvent.newDescription.type
);
}
return;
} else {
expect.fail(`Encountered unexpected event - ${inspect(actualEvent)}`);
}
Expand Down
14 changes: 8 additions & 6 deletions test/tools/unified-spec-runner/schema.ts
Expand Up @@ -7,6 +7,7 @@ import type {
ServerApiVersion,
SeverityLevel,
TagSet,
TopologyType,
W
} from '../../mongodb';
import { type TestConfiguration } from '../runner/config';
Expand Down Expand Up @@ -95,21 +96,21 @@ export interface UnifiedSuite {
tests: Test[];
_yamlAnchors?: Document;
}
export const TopologyType = Object.freeze({
export const TopologyName = Object.freeze({
single: 'single',
replicaset: 'replicaset',
sharded: 'sharded',
shardedReplicaset: 'sharded-replicaset',
loadBalanced: 'load-balanced'
} as const);

export type TopologyId = (typeof TopologyType)[keyof typeof TopologyType];
export type TopologyName = (typeof TopologyName)[keyof typeof TopologyName];
export interface RunOnRequirement {
serverless?: 'forbid' | 'allow' | 'require';
auth?: boolean;
maxServerVersion?: string;
minServerVersion?: string;
topologies?: TopologyId[];
topologies?: TopologyName[];
serverParameters?: Document;
csfle?: boolean;
}
Expand Down Expand Up @@ -314,6 +315,7 @@ export interface ExpectedCmapEvent {
connectionCheckedOutEvent?: Record<string, never>;
connectionCheckedInEvent?: Record<string, never>;
}

export interface ExpectedSdamEvent {
serverDescriptionChangedEvent?: {
previousDescription?: {
Expand All @@ -336,16 +338,16 @@ export interface ExpectedSdamEvent {
topologyDescriptionChangedEvent?: {
topologyId?: any;
previousDescription?: {
type?: string;
type?: TopologyType;
};
newDescription?: {
type?: string;
type?: TopologyType;
};
};
topologyOpeningEvent?: {
topologyId?: any;
};
topologyClosingEvent?: {
topologyClosedEvent?: {
topologyId?: any;
};
serverOpeningEvent?: {
Expand Down

0 comments on commit ca2bfb0

Please sign in to comment.