Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NODE-3793): Remove offensive language from code and tests #3082

Merged
merged 16 commits into from
Dec 17, 2021
12 changes: 6 additions & 6 deletions src/cmap/commands.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,18 @@ export class Db {
return this.s.options;
}

// slaveOk specified
/**
* slaveOk specified
dariakp marked this conversation as resolved.
Show resolved Hide resolved
* @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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
50 changes: 50 additions & 0 deletions test/unit/db.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { expect } from 'chai';

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

describe('Db - test secondaryOk matches readPreference', function () {
dariakp marked this conversation as resolved.
Show resolved Hide resolved
const client = new MongoClient('mongodb://localhost:27017');
const legacy_secondary_ok = 'slaveOk';

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

expect(mydb.secondaryOk).to.be.false;
expect(mydb[legacy_secondary_ok]).to.be.false;
});

it.only('secondaryOk 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.secondaryOk).to.be.true;
expect(mydb[legacy_secondary_ok]).to.be.true;
});

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

expect(mydb.secondaryOk).to.be.true;
expect(mydb[legacy_secondary_ok]).to.be.true;
});

it.only('secondaryOk 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.secondaryOk).to.be.true;
expect(mydb[legacy_secondary_ok]).to.be.true;
});

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

expect(mydb.secondaryOk).to.be.true;
expect(mydb[legacy_secondary_ok]).to.be.true;
});
});