Skip to content

Commit 2f234a3

Browse files
committed
More fix up and aligning with spec
1 parent d91b21f commit 2f234a3

File tree

5 files changed

+268
-120
lines changed

5 files changed

+268
-120
lines changed

src/sdam/topology_description.ts

+1
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export class TopologyDescription {
201201
let { type: topologyType, setName, maxSetVersion, maxElectionId, commonWireVersion } = this;
202202

203203
if (serverDescription.setName && setName && serverDescription.setName !== setName) {
204+
// TODO(NODE-XXXX): servers with an incorrect setName should be removed not marked Unknown
204205
serverDescription = new ServerDescription(address, undefined);
205206
}
206207

test/spec/server-discovery-and-monitoring/rs/secondary_ignore_ok_0.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "New primary",
2+
"description": "Secondary ignored when ok is zero",
33
"uri": "mongodb://a,b/?replicaSet=rs",
44
"phases": [
55
{

test/spec/server-discovery-and-monitoring/rs/secondary_ignore_ok_0.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
description: "New primary"
1+
description: "Secondary ignored when ok is zero"
22

33
uri: "mongodb://a,b/?replicaSet=rs"
44

test/tools/utils.ts

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { EJSON } from 'bson';
22
import { expect } from 'chai';
3-
import util from 'util';
3+
import { inspect } from 'util';
44

55
import { Logger } from '../../src/logger';
6-
import { deprecateOptions, DeprecateOptionsConfig } from '../../src/utils';
6+
import { deprecateOptions, DeprecateOptionsConfig, setDifference } from '../../src/utils';
77

88
export function makeTestFunction(config: DeprecateOptionsConfig) {
99
const fn = (options: any) => {
@@ -189,15 +189,15 @@ export function shouldRunServerlessTest(testRequirement: any, isServerless: any)
189189
* Attempts to use EJSON (to make type information obvious)
190190
* falls back to util.inspect if there's an error (circular reference)
191191
*/
192-
export function ejson(strings: any[], ...values: any[]) {
192+
export function ejson(strings: TemplateStringsArray, ...values: any[]) {
193193
const stringParts = [strings[0]];
194194
for (const [idx, value] of values.entries()) {
195195
if (typeof value === 'object') {
196196
let stringifiedObject: string;
197197
try {
198198
stringifiedObject = EJSON.stringify(value, { relaxed: false });
199199
} catch (error) {
200-
stringifiedObject = util.inspect(value, {
200+
stringifiedObject = inspect(value, {
201201
depth: Infinity,
202202
showHidden: true,
203203
compact: true
@@ -270,6 +270,31 @@ export function extractAuthFromConnectionString(connectionString: string | any[]
270270
return connectionString.slice(indices.start, indices.end);
271271
}
272272

273+
/**
274+
* Ensures no extra keys are provided on an object
275+
* @param knownKeys - the super set of keys allowed to exist on objectWithKeys
276+
* @param objectWithKeys - An object (usually from JSON) that has an unknown amount of keys
277+
*
278+
* Users should combine the usage of this helper with an interface
279+
* ```ts
280+
* interface Pet { name: string }
281+
* const maybePet: any = { name: 'spot', age: number };
282+
* assertSubsetOfKeys<Pet>(['name'], maybePet); // throws!
283+
* assertSubsetOfKeys<Pet>(['name', 'age'], maybePet); // compiler error! (age not defined)
284+
* assertSubsetOfKeys<Pet>([], maybePet); // compiler error! (missing keys)
285+
* ```
286+
*/
287+
export function assertNoExtraKeys<T extends { [k: string]: any }>(
288+
knownKeys: Array<keyof Required<T>>,
289+
objectWithKeys: Partial<T>
290+
): asserts objectWithKeys is { [P in keyof T]-?: T[P] } {
291+
expect(knownKeys).to.have.lengthOf(knownKeys.length);
292+
const subsetKeys = Object.keys(objectWithKeys);
293+
const difference = Array.from(setDifference(subsetKeys, knownKeys));
294+
const message = `Expected no extra keys. Found: [${difference.join(', ')}]`;
295+
expect(difference, message).to.be.empty;
296+
}
297+
273298
export interface FailPoint {
274299
configureFailPoint: 'failCommand';
275300
mode: { activationProbability: number } | { times: number } | 'alwaysOn' | 'off';

0 commit comments

Comments
 (0)