|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const Topology = require('../../lib/core/sdam/topology').Topology; |
3 | 4 | const test = require('./shared').assert;
|
4 | 5 | const setupDatabase = require('./shared').setupDatabase;
|
5 | 6 | const withMonitoredClient = require('./shared').withMonitoredClient;
|
6 | 7 | const expect = require('chai').expect;
|
7 | 8 | const ReadPreference = require('../../lib/core/topologies/read_preference');
|
| 9 | +const { withClient } = require('./shared'); |
8 | 10 |
|
9 | 11 | describe('ReadPreference', function() {
|
10 | 12 | before(function() {
|
@@ -654,4 +656,74 @@ describe('ReadPreference', function() {
|
654 | 656 | })
|
655 | 657 | });
|
656 | 658 | });
|
| 659 | + |
| 660 | + context('should enforce fixed primary read preference', function() { |
| 661 | + const collectionName = 'ddl_collection'; |
| 662 | + |
| 663 | + beforeEach(function() { |
| 664 | + const configuration = this.configuration; |
| 665 | + const client = this.configuration.newClient(configuration.writeConcernMax(), { |
| 666 | + useUnifiedTopology: true, |
| 667 | + readPreference: 'primaryPreferred' |
| 668 | + }); |
| 669 | + return withClient(client, (client, done) => { |
| 670 | + const db = client.db(configuration.db); |
| 671 | + db.addUser('default', 'pass', { roles: 'readWrite' }, () => { |
| 672 | + db.createCollection('before_collection', () => { |
| 673 | + db.createIndex(collectionName, { aloha: 1 }, done); |
| 674 | + }); |
| 675 | + }); |
| 676 | + }); |
| 677 | + }); |
| 678 | + |
| 679 | + const methods = { |
| 680 | + 'Collection#createIndex': [{ quote: 'text' }], |
| 681 | + 'Db#createIndex': [collectionName, { quote: 'text' }], |
| 682 | + 'Db#addUser': ['thomas', 'pass', { roles: 'readWrite' }], |
| 683 | + 'Db#removeUser': ['default'], |
| 684 | + 'Db#createCollection': ['created_collection'], |
| 685 | + 'Db#dropCollection': ['before_collection'], |
| 686 | + 'Collection#dropIndex': ['aloha_1'], |
| 687 | + 'Collection#rename': ['new_name'], |
| 688 | + 'Db#dropDatabase': [] |
| 689 | + }; |
| 690 | + |
| 691 | + Object.keys(methods).forEach(operation => { |
| 692 | + it(`${operation}`, { |
| 693 | + metadata: { |
| 694 | + requires: { topology: ['replicaset', 'sharded'] } |
| 695 | + }, |
| 696 | + test: function() { |
| 697 | + const configuration = this.configuration; |
| 698 | + const client = this.configuration.newClient(configuration.writeConcernMax(), { |
| 699 | + useUnifiedTopology: true, |
| 700 | + readPreference: 'primaryPreferred' |
| 701 | + }); |
| 702 | + return withClient(client, (client, done) => { |
| 703 | + const db = client.db(configuration.db); |
| 704 | + const args = methods[operation]; |
| 705 | + const [parentId, method] = operation.split('#'); |
| 706 | + const collection = db.collection(collectionName); |
| 707 | + const parent = parentId === 'Collection' ? collection : parentId === 'Db' ? db : null; |
| 708 | + const selectServerSpy = this.sinon.spy(Topology.prototype, 'selectServer'); |
| 709 | + const callback = err => { |
| 710 | + expect(err).to.not.exist; |
| 711 | + expect(selectServerSpy.called).to.equal(true); |
| 712 | + if (typeof selectServerSpy.args[0][0] === 'function') { |
| 713 | + expect(selectServerSpy) |
| 714 | + .nested.property('args[0][1].readPreference.mode') |
| 715 | + .to.equal(ReadPreference.PRIMARY); |
| 716 | + } else { |
| 717 | + expect(selectServerSpy) |
| 718 | + .nested.property('args[0][0].readPreference.mode') |
| 719 | + .to.equal(ReadPreference.PRIMARY); |
| 720 | + } |
| 721 | + done(); |
| 722 | + }; |
| 723 | + parent[method].apply(parent, [...args, callback]); |
| 724 | + }); |
| 725 | + } |
| 726 | + }); |
| 727 | + }); |
| 728 | + }); |
657 | 729 | });
|
0 commit comments