Skip to content

Commit

Permalink
feat(NODE-3793): Remove offensive language from code and tests (#3082)
Browse files Browse the repository at this point in the history
  • Loading branch information
ljhaywar committed Dec 17, 2021
1 parent 8a3bab7 commit 91a67e0
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 51 deletions.
12 changes: 6 additions & 6 deletions src/cmap/commands.ts
Expand Up @@ -12,7 +12,7 @@ let _requestId = 0;

// Query flags
const OPTS_TAILABLE_CURSOR = 2;
const OPTS_SLAVE = 4;
const OPTS_SECONDARY = 4;
const OPTS_OPLOG_REPLAY = 8;
const OPTS_NO_CURSOR_TIMEOUT = 16;
const OPTS_AWAIT_DATA = 32;
Expand Down Expand Up @@ -41,7 +41,7 @@ export interface OpQueryOptions extends CommandOptions {
ignoreUndefined?: boolean;
maxBsonSize?: number;
checkKeys?: boolean;
slaveOk?: boolean;
secondaryOk?: boolean;

requestId?: number;
moreToCome?: boolean;
Expand All @@ -67,7 +67,7 @@ export class Query {
checkKeys: boolean;
batchSize: number;
tailable: boolean;
slaveOk: boolean;
secondaryOk: boolean;
oplogReplay: boolean;
noCursorTimeout: boolean;
awaitData: boolean;
Expand Down Expand Up @@ -112,7 +112,7 @@ export class Query {

// Flags
this.tailable = false;
this.slaveOk = typeof options.slaveOk === 'boolean' ? options.slaveOk : false;
this.secondaryOk = typeof options.secondaryOk === 'boolean' ? options.secondaryOk : false;
this.oplogReplay = false;
this.noCursorTimeout = false;
this.awaitData = false;
Expand Down Expand Up @@ -146,8 +146,8 @@ export class Query {
flags |= OPTS_TAILABLE_CURSOR;
}

if (this.slaveOk) {
flags |= OPTS_SLAVE;
if (this.secondaryOk) {
flags |= OPTS_SECONDARY;
}

if (this.oplogReplay) {
Expand Down
6 changes: 3 additions & 3 deletions src/cmap/connection.ts
Expand Up @@ -96,7 +96,7 @@ export interface QueryOptions extends BSONSerializeOptions {
/** @internal */
export interface CommandOptions extends BSONSerializeOptions {
command?: boolean;
slaveOk?: boolean;
secondaryOk?: boolean;
/** Specify read preference if command supports it */
readPreference?: ReadPreferenceLike;
raw?: boolean;
Expand Down Expand Up @@ -427,7 +427,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
numberToReturn: -1,
checkKeys: false,
// This value is not overridable
slaveOk: readPreference.slaveOk()
secondaryOk: readPreference.secondaryOk()
},
options
);
Expand Down Expand Up @@ -472,7 +472,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
numberToReturn,
pre32Limit: typeof limit === 'number' ? limit : undefined,
checkKeys: false,
slaveOk: readPreference.slaveOk()
secondaryOk: readPreference.secondaryOk()
};

if (options.projection) {
Expand Down
7 changes: 4 additions & 3 deletions src/collection.ts
Expand Up @@ -112,6 +112,9 @@ export interface CollectionOptions
extends BSONSerializeOptions,
WriteConcernOptions,
LoggerOptions {
/**
* @deprecated Use readPreference instead
*/
slaveOk?: boolean;
/** Specify a read concern for the collection. (only MongoDB 3.2 or higher supported) */
readConcern?: ReadConcernLike;
Expand All @@ -127,7 +130,6 @@ export interface CollectionPrivate {
namespace: MongoDBNamespace;
readPreference?: ReadPreference;
bsonOptions: BSONSerializeOptions;
slaveOk?: boolean;
collectionHint?: Hint;
readConcern?: ReadConcern;
writeConcern?: WriteConcern;
Expand Down Expand Up @@ -181,8 +183,7 @@ export class Collection<TSchema extends Document = Document> {
readPreference: ReadPreference.fromOptions(options),
bsonOptions: resolveBSONOptions(options, db),
readConcern: ReadConcern.fromOptions(options),
writeConcern: WriteConcern.fromOptions(options),
slaveOk: options == null || options.slaveOk == null ? db.slaveOk : options.slaveOk
writeConcern: WriteConcern.fromOptions(options)
};
}

Expand Down
12 changes: 11 additions & 1 deletion src/db.ts
Expand Up @@ -185,8 +185,18 @@ export class Db {
return this.s.options;
}

// slaveOk specified
/**
* slaveOk specified
* @deprecated Use secondaryOk instead
*/
get slaveOk(): boolean {
return this.secondaryOk;
}

/**
* Check if a secondary can be used (because the read preference is *not* set to primary)
*/
get secondaryOk(): boolean {
return this.s.readPreference?.preference !== 'primary' || false;
}

Expand Down
16 changes: 12 additions & 4 deletions src/read_preference.ts
Expand Up @@ -232,19 +232,27 @@ export class ReadPreference {
}

/**
* Indicates that this readPreference needs the "slaveOk" bit when sent over the wire
*
* Indicates that this readPreference needs the "secondaryOk" bit when sent over the wire
* @deprecated Use secondaryOk instead
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
*/
slaveOk(): boolean {
const NEEDS_SLAVEOK = new Set<string>([
return this.secondaryOk();
}

/**
* Indicates that this readPreference needs the "SecondaryOk" bit when sent over the wire
* @see https://docs.mongodb.com/manual/reference/mongodb-wire-protocol/#op-query
*/
secondaryOk(): boolean {
const NEEDS_SECONDARYOK = new Set<string>([
ReadPreference.PRIMARY_PREFERRED,
ReadPreference.SECONDARY,
ReadPreference.SECONDARY_PREFERRED,
ReadPreference.NEAREST
]);

return NEEDS_SLAVEOK.has(this.mode);
return NEEDS_SECONDARYOK.has(this.mode);
}

/**
Expand Down
33 changes: 0 additions & 33 deletions test/functional/cursor.test.js
Expand Up @@ -11,7 +11,6 @@ const { Writable } = require('stream');
const { ReadPreference } = require('../../src/read_preference');
const { ServerType } = require('../../src/sdam/common');
const { formatSort } = require('../../src/sort');
const { FindCursor } = require('../../src/cursor/find_cursor');

describe('Cursor', function () {
before(function () {
Expand Down Expand Up @@ -3732,38 +3731,6 @@ describe('Cursor', function () {
}
});

// NOTE: This is skipped because I don't think its correct or adds value. The expected error
// is not an error with hasNext (from server), but rather a local TypeError which should
// be caught anyway. The only solution here would be to wrap the entire top level call
// in a try/catch which is not going to happen.
it.skip('Should propagate hasNext errors when using a callback', {
metadata: {
requires: { topology: ['single', 'replicaset', 'sharded', 'ssl', 'heap', 'wiredtiger'] }
},
test: function (done) {
const configuration = this.configuration;
var client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
client.connect((err, client) => {
expect(err).to.not.exist;
this.defer(() => client.close());

const db = client.db(configuration.db);
const cursor = new FindCursor(
db.s.topology,
db.s.namespace,
{},
{ limit: 0, skip: 0, slaveOk: false, readPreference: 42 }
);

cursor.hasNext(err => {
test.ok(err !== null);
test.equal(err.message, 'readPreference must be a ReadPreference instance');
done();
});
});
}
});

it(
'should return implicit session to pool when client-side cursor exhausts results on initial query',
{
Expand Down
53 changes: 53 additions & 0 deletions test/unit/db.test.ts
@@ -0,0 +1,53 @@
import { expect } from 'chai';

import { MongoClient } from '../../src';
import { Db, DbOptions } from '../../src/db';
import { ReadPreference } from '../../src/read_preference';

describe('class Db', function () {
describe('secondaryOk', function () {
const client = new MongoClient('mongodb://localhost:27017');
const legacy_secondary_ok = 'slaveOk';
const secondary_ok = 'secondaryOk';

it('should be false when readPreference is Primary', function () {
const options: DbOptions = { readPreference: ReadPreference.PRIMARY };
const mydb = new Db(client, 'mydb', options);

expect(mydb).property(secondary_ok).to.be.false;
expect(mydb).property(legacy_secondary_ok).to.be.false;
});

it('should be true when readPreference is Primary Preferred', function () {
const options: DbOptions = { readPreference: ReadPreference.PRIMARY_PREFERRED };
const mydb = new Db(client, 'mydb', options);

expect(mydb).property(secondary_ok).to.be.true;
expect(mydb).property(legacy_secondary_ok).to.be.true;
});

it('should be true when readPreference is Secondary', function () {
const options: DbOptions = { readPreference: ReadPreference.SECONDARY };
const mydb = new Db(client, 'mydb', options);

expect(mydb).property(secondary_ok).to.be.true;
expect(mydb).property(legacy_secondary_ok).to.be.true;
});

it('should be true when readPreference is Secondary Preferred', function () {
const options: DbOptions = { readPreference: ReadPreference.SECONDARY_PREFERRED };
const mydb = new Db(client, 'mydb', options);

expect(mydb).property(secondary_ok).to.be.true;
expect(mydb).property(legacy_secondary_ok).to.be.true;
});

it('should be true when readPreference is Nearest', function () {
const options: DbOptions = { readPreference: ReadPreference.NEAREST };
const mydb = new Db(client, 'mydb', options);

expect(mydb).property(secondary_ok).to.be.true;
expect(mydb).property(legacy_secondary_ok).to.be.true;
});
});
});
44 changes: 43 additions & 1 deletion test/unit/read_preference.test.ts
Expand Up @@ -4,7 +4,7 @@ import { ReadPreference } from '../../src';

describe('class ReadPreference', function () {
const maxStalenessSeconds = 1234;
const { PRIMARY, SECONDARY, NEAREST } = ReadPreference;
const { PRIMARY, PRIMARY_PREFERRED, SECONDARY, SECONDARY_PREFERRED, NEAREST } = ReadPreference;
const TAGS = [{ loc: 'dc' }];
describe('::constructor', function () {
it('should accept (mode)', function () {
Expand Down Expand Up @@ -132,4 +132,46 @@ describe('class ReadPreference', function () {
).to.throw('Primary read preference cannot be combined with maxStalenessSeconds');
});
});

describe('secondaryOk()', function () {
it('should be false when readPreference is Primary', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: PRIMARY
});
expect(readPreference.secondaryOk()).to.be.false;
expect(readPreference.slaveOk()).to.be.false;
});

it('should be true when readPreference is Primary Preferred', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: PRIMARY_PREFERRED
});
expect(readPreference.secondaryOk()).to.be.true;
expect(readPreference.slaveOk()).to.be.true;
});

it('should be true when readPreference is Secondary', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: SECONDARY
});
expect(readPreference.secondaryOk()).to.be.true;
expect(readPreference.slaveOk()).to.be.true;
});

it('should be true when readPreference is Secondary Preferred', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: SECONDARY_PREFERRED
});
expect(readPreference.secondaryOk()).to.be.true;
expect(readPreference.slaveOk()).to.be.true;
});

it('should be true when readPreference is Nearest', function () {
const readPreference = ReadPreference.fromOptions({
readPreference: NEAREST
});
expect(readPreference.secondaryOk()).to.be.true;
expect(readPreference.slaveOk()).to.be.true;
});
});
});

0 comments on commit 91a67e0

Please sign in to comment.