Skip to content

Commit 94f08e0

Browse files
committed
Merge remote-tracking branch 'origin/master' into NODE-2820/remove-readable-from-cursor
2 parents 486b4ac + 98162c3 commit 94f08e0

File tree

10 files changed

+131
-9
lines changed

10 files changed

+131
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,6 @@ yarn.lock
5151
lib/
5252
*.tgz
5353
*.d.ts
54+
55+
.vscode
56+
output

etc/update-spec-tests.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
# This script is used to fetch the latest tests for the specified spec.
4+
# It puts the tests in the direcory $spec_root. It should be run from the root of the repository.
5+
6+
set -o errexit
7+
set -o nounset
8+
9+
if [ ! -d ".git" ]; then
10+
echo "$0: This script must be run from the root of the repository" >&2
11+
exit 1
12+
fi
13+
14+
if [ $# -ne 1 ]; then
15+
echo "$0: This script must be passed exactly one argument for which tests to sync" >&2
16+
exit 1
17+
fi
18+
19+
spec_root="test/spec"
20+
21+
tmpdir=$(mktemp -d -t spec_testsXXXX)
22+
curl -sL "https://github.com/mongodb/specifications/archive/master.zip" -o "$tmpdir/specs.zip"
23+
unzip -d "$tmpdir" "$tmpdir/specs.zip" > /dev/null
24+
mkdir -p "$spec_root/$1"
25+
rsync -ah --exclude '*.rst' "$tmpdir/specifications-master/source/$1/tests/" "$spec_root/$1" --delete
26+
rm -rf "$tmpdir"

src/operations/add_user.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export class AddUserOperation extends CommandOperation<AddUserOptions, Document>
5454
}
5555

5656
// Get additional values
57-
let roles = Array.isArray(options.roles) ? options.roles : [];
57+
let roles: string[] = [];
58+
if (Array.isArray(options.roles)) roles = options.roles;
59+
if (typeof options.roles === 'string') roles = [options.roles];
5860

5961
// If not roles defined print deprecated message
6062
// TODO: handle deprecation properly

test/functional/readpreference.test.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const test = require('./shared').assert;
44
const setupDatabase = require('./shared').setupDatabase;
55
const withMonitoredClient = require('./shared').withMonitoredClient;
66
const expect = require('chai').expect;
7-
const { ReadPreference } = require('../../src');
7+
const { ReadPreference, Topology } = require('../../src');
8+
const { withClient } = require('./shared');
89

910
describe('ReadPreference', function () {
1011
before(function () {
@@ -587,4 +588,74 @@ describe('ReadPreference', function () {
587588
})
588589
});
589590
});
591+
592+
context('should enforce fixed primary read preference', function () {
593+
const collectionName = 'ddl_collection';
594+
595+
beforeEach(function () {
596+
const configuration = this.configuration;
597+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
598+
useUnifiedTopology: true,
599+
readPreference: 'primaryPreferred'
600+
});
601+
return withClient(client, (client, done) => {
602+
const db = client.db(configuration.db);
603+
db.addUser('default', 'pass', { roles: 'readWrite' }, () => {
604+
db.createCollection('before_collection', () => {
605+
db.createIndex(collectionName, { aloha: 1 }, done);
606+
});
607+
});
608+
});
609+
});
610+
611+
const methods = {
612+
'Collection#createIndex': [{ quote: 'text' }],
613+
'Db#createIndex': [collectionName, { quote: 'text' }],
614+
'Db#addUser': ['thomas', 'pass', { roles: 'readWrite' }],
615+
'Db#removeUser': ['default'],
616+
'Db#createCollection': ['created_collection'],
617+
'Db#dropCollection': ['before_collection'],
618+
'Collection#dropIndex': ['aloha_1'],
619+
'Collection#rename': ['new_name'],
620+
'Db#dropDatabase': []
621+
};
622+
623+
Object.keys(methods).forEach(operation => {
624+
it(`${operation}`, {
625+
metadata: {
626+
requires: { topology: ['replicaset', 'sharded'] }
627+
},
628+
test: function () {
629+
const configuration = this.configuration;
630+
const client = this.configuration.newClient(configuration.writeConcernMax(), {
631+
useUnifiedTopology: true,
632+
readPreference: 'primaryPreferred'
633+
});
634+
return withClient(client, (client, done) => {
635+
const db = client.db(configuration.db);
636+
const args = methods[operation];
637+
const [parentId, method] = operation.split('#');
638+
const collection = db.collection(collectionName);
639+
const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null;
640+
const selectServerSpy = this.sinon.spy(Topology.prototype, 'selectServer');
641+
const callback = err => {
642+
expect(err).to.not.exist;
643+
expect(selectServerSpy.called).to.equal(true);
644+
if (typeof selectServerSpy.args[0][0] === 'function') {
645+
expect(selectServerSpy)
646+
.nested.property('args[0][1].readPreference.mode')
647+
.to.equal(ReadPreference.PRIMARY);
648+
} else {
649+
expect(selectServerSpy)
650+
.nested.property('args[0][0].readPreference.mode')
651+
.to.equal(ReadPreference.PRIMARY);
652+
}
653+
done();
654+
};
655+
parent[method].apply(parent, [...args, callback]);
656+
});
657+
}
658+
});
659+
});
660+
});
590661
});

test/spec/connection-monitoring-and-pooling/pool-checkout-connection.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,23 @@
1212
"type": "ConnectionCheckOutStarted",
1313
"address": 42
1414
},
15+
{
16+
"type": "ConnectionCreated",
17+
"connectionId": 1,
18+
"address": 42
19+
},
20+
{
21+
"type": "ConnectionReady",
22+
"connectionId": 1,
23+
"address": 42
24+
},
1525
{
1626
"type": "ConnectionCheckedOut",
1727
"connectionId": 1,
1828
"address": 42
1929
}
2030
],
2131
"ignore": [
22-
"ConnectionPoolCreated",
23-
"ConnectionCreated",
24-
"ConnectionReady"
32+
"ConnectionPoolCreated"
2533
]
2634
}

test/spec/connection-monitoring-and-pooling/pool-checkout-connection.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@ operations:
66
events:
77
- type: ConnectionCheckOutStarted
88
address: 42
9+
- type: ConnectionCreated
10+
connectionId: 1
11+
address: 42
12+
- type: ConnectionReady
13+
connectionId: 1
14+
address: 42
915
- type: ConnectionCheckedOut
1016
connectionId: 1
1117
address: 42
1218
ignore:
1319
- ConnectionPoolCreated
14-
- ConnectionCreated
15-
- ConnectionReady

test/spec/connection-monitoring-and-pooling/pool-create-min-size.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
"event": "ConnectionCreated",
1212
"count": 3
1313
},
14+
{
15+
"name": "waitForEvent",
16+
"event": "ConnectionReady",
17+
"count": 3
18+
},
1419
{
1520
"name": "checkOut"
1621
}

test/spec/connection-monitoring-and-pooling/pool-create-min-size.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ operations:
77
- name: waitForEvent
88
event: ConnectionCreated
99
count: 3
10+
- name: waitForEvent
11+
event: ConnectionReady
12+
count: 3
1013
- name: checkOut
1114
events:
1215
- type: ConnectionPoolCreated

test/spec/connection-monitoring-and-pooling/wait-queue-timeout.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "must aggressively timeout threads enqueued longer than waitQueueTimeoutMS",
55
"poolOptions": {
66
"maxPoolSize": 1,
7-
"waitQueueTimeoutMS": 100
7+
"waitQueueTimeoutMS": 20
88
},
99
"operations": [
1010
{

test/spec/connection-monitoring-and-pooling/wait-queue-timeout.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ style: unit
33
description: must aggressively timeout threads enqueued longer than waitQueueTimeoutMS
44
poolOptions:
55
maxPoolSize: 1
6-
waitQueueTimeoutMS: 100
6+
waitQueueTimeoutMS: 20
77
operations:
88
# Check out only possible connection
99
- name: checkOut

0 commit comments

Comments
 (0)