|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { expect } = require('chai'); |
| 4 | +const { ObjectId } = require('../../../src/bson'); |
| 5 | +const { ReadPreference } = require('../../../src/read_preference'); |
| 6 | +const { |
| 7 | + secondaryWritableServerSelector, |
| 8 | + MIN_SECONDARY_WRITE_WIRE_VERSION |
| 9 | +} = require('../../../src/sdam/server_selection'); |
| 10 | +const { ServerDescription } = require('../../../src/sdam/server_description'); |
| 11 | +const { TopologyDescription } = require('../../../src/sdam/topology_description'); |
| 12 | +const { TopologyType } = require('../../../src/sdam/common'); |
| 13 | + |
| 14 | +describe('ServerSelector', function () { |
| 15 | + describe('#secondaryWritableServerSelector', function () { |
| 16 | + const primary = new ServerDescription('127.0.0.1:27017', { |
| 17 | + setName: 'test', |
| 18 | + isWritablePrimary: true, |
| 19 | + ok: 1 |
| 20 | + }); |
| 21 | + const secondary = new ServerDescription('127.0.0.1:27018', { |
| 22 | + setName: 'test', |
| 23 | + secondary: true, |
| 24 | + ok: 1 |
| 25 | + }); |
| 26 | + const serverDescriptions = new Map(); |
| 27 | + serverDescriptions.set('127.0.0.1:27017', primary); |
| 28 | + serverDescriptions.set('127.0.0.1:27018', secondary); |
| 29 | + |
| 30 | + context('when the common server version is >= 5.0', function () { |
| 31 | + const topologyDescription = new TopologyDescription( |
| 32 | + TopologyType.ReplicaSetWithPrimary, |
| 33 | + serverDescriptions, |
| 34 | + 'test', |
| 35 | + MIN_SECONDARY_WRITE_WIRE_VERSION, |
| 36 | + new ObjectId(), |
| 37 | + MIN_SECONDARY_WRITE_WIRE_VERSION |
| 38 | + ); |
| 39 | + |
| 40 | + context('when a read preference is provided', function () { |
| 41 | + const selector = secondaryWritableServerSelector( |
| 42 | + MIN_SECONDARY_WRITE_WIRE_VERSION, |
| 43 | + ReadPreference.secondary |
| 44 | + ); |
| 45 | + const server = selector(topologyDescription, Array.from(serverDescriptions.values())); |
| 46 | + |
| 47 | + it('uses the provided read preference', function () { |
| 48 | + expect(server).to.deep.equal([secondary]); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + context('when a read preference is not provided', function () { |
| 53 | + const selector = secondaryWritableServerSelector(MIN_SECONDARY_WRITE_WIRE_VERSION); |
| 54 | + const server = selector(topologyDescription, Array.from(serverDescriptions.values())); |
| 55 | + |
| 56 | + it('selects a primary', function () { |
| 57 | + expect(server).to.deep.equal([primary]); |
| 58 | + }); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + context('when the common server version is < 5.0', function () { |
| 63 | + const topologyDescription = new TopologyDescription( |
| 64 | + TopologyType.ReplicaSetWithPrimary, |
| 65 | + serverDescriptions, |
| 66 | + 'test', |
| 67 | + MIN_SECONDARY_WRITE_WIRE_VERSION - 1, |
| 68 | + new ObjectId(), |
| 69 | + MIN_SECONDARY_WRITE_WIRE_VERSION - 1 |
| 70 | + ); |
| 71 | + |
| 72 | + context('when a read preference is provided', function () { |
| 73 | + const selector = secondaryWritableServerSelector( |
| 74 | + MIN_SECONDARY_WRITE_WIRE_VERSION - 1, |
| 75 | + ReadPreference.secondary |
| 76 | + ); |
| 77 | + const server = selector(topologyDescription, Array.from(serverDescriptions.values())); |
| 78 | + |
| 79 | + it('selects a primary', function () { |
| 80 | + expect(server).to.deep.equal([primary]); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + context('when read preference is not provided', function () { |
| 85 | + const selector = secondaryWritableServerSelector(MIN_SECONDARY_WRITE_WIRE_VERSION - 1); |
| 86 | + const server = selector(topologyDescription, Array.from(serverDescriptions.values())); |
| 87 | + |
| 88 | + it('selects a primary', function () { |
| 89 | + expect(server).to.deep.equal([primary]); |
| 90 | + }); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | +}); |
0 commit comments