|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { on, once } from 'events'; |
| 3 | + |
| 4 | +import { Collection } from '../../../src/collection'; |
| 5 | +import { LEGACY_HELLO_COMMAND } from '../../../src/constants'; |
| 6 | +import { Db } from '../../../src/db'; |
| 7 | +import { MongoClient } from '../../../src/mongo_client'; |
| 8 | +import * as mock from '../../tools/mongodb-mock/index'; |
| 9 | + |
| 10 | +describe('Write Concern', function () { |
| 11 | + it('should respect writeConcern from uri', function (done) { |
| 12 | + const client = this.configuration.newClient( |
| 13 | + `${this.configuration.url()}&w=0&monitorCommands=true` |
| 14 | + ); |
| 15 | + const events: any[] = []; |
| 16 | + client.on('commandStarted', event => { |
| 17 | + if (event.commandName === 'insert') { |
| 18 | + events.push(event); |
| 19 | + } |
| 20 | + }); |
| 21 | + |
| 22 | + expect(client.writeConcern).to.eql({ w: 0 }); |
| 23 | + client |
| 24 | + .db('test') |
| 25 | + .collection('test') |
| 26 | + .insertOne({ a: 1 }, (err, result) => { |
| 27 | + expect(err).to.not.exist; |
| 28 | + expect(result).to.exist; |
| 29 | + expect(events).to.be.an('array').with.lengthOf(1); |
| 30 | + expect(events[0]).to.containSubset({ |
| 31 | + commandName: 'insert', |
| 32 | + command: { |
| 33 | + writeConcern: { w: 0 } |
| 34 | + } |
| 35 | + }); |
| 36 | + client.close(done); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('mock server write concern test', () => { |
| 41 | + let server; |
| 42 | + before(() => { |
| 43 | + return mock.createServer().then(s => { |
| 44 | + server = s; |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + after(() => mock.cleanup()); |
| 49 | + |
| 50 | + // TODO(NODE-3816): the mock server response is looking for writeConcern on all messages, but endSessions doesn't have it |
| 51 | + it.skip('should pipe writeConcern from client down to API call', function () { |
| 52 | + server.setMessageHandler(request => { |
| 53 | + if (request.document && request.document[LEGACY_HELLO_COMMAND]) { |
| 54 | + return request.reply(mock.HELLO); |
| 55 | + } |
| 56 | + expect(request.document.writeConcern).to.exist; |
| 57 | + expect(request.document.writeConcern.w).to.equal('majority'); |
| 58 | + return request.reply({ ok: 1 }); |
| 59 | + }); |
| 60 | + |
| 61 | + const uri = `mongodb://${server.uri()}`; |
| 62 | + const client = new MongoClient(uri, { writeConcern: { w: 'majority' } }); |
| 63 | + return client |
| 64 | + .connect() |
| 65 | + .then(() => { |
| 66 | + const db = client.db('wc_test'); |
| 67 | + const collection = db.collection('wc'); |
| 68 | + |
| 69 | + return collection.insertMany([{ a: 2 }]); |
| 70 | + }) |
| 71 | + .then(() => { |
| 72 | + return client.close(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + context('when performing read operations', function () { |
| 78 | + context('when writeConcern = 0', function () { |
| 79 | + describe('cursor creating operations with a getMore', function () { |
| 80 | + let client: MongoClient; |
| 81 | + let db: Db; |
| 82 | + let col: Collection; |
| 83 | + |
| 84 | + beforeEach(async function () { |
| 85 | + client = this.configuration.newClient({ writeConcern: { w: 0 } }); |
| 86 | + await client.connect(); |
| 87 | + db = client.db('writeConcernTest'); |
| 88 | + col = db.collection('writeConcernTest'); |
| 89 | + |
| 90 | + const docs: any[] = []; |
| 91 | + for (let i = 0; i < 100; i++) { |
| 92 | + docs.push({ a: i, b: i + 1 }); |
| 93 | + } |
| 94 | + |
| 95 | + await col.insertMany(docs); |
| 96 | + }); |
| 97 | + |
| 98 | + afterEach(async function () { |
| 99 | + await db.dropDatabase(); |
| 100 | + await client.close(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('succeeds on find', async function () { |
| 104 | + const findResult = col.find({}, { batchSize: 2 }); |
| 105 | + const err = await findResult.toArray().catch(e => e); |
| 106 | + |
| 107 | + expect(err).to.not.be.instanceOf(Error); |
| 108 | + }); |
| 109 | + |
| 110 | + it('succeeds on listCollections', async function () { |
| 111 | + const collections: any[] = []; |
| 112 | + for (let i = 0; i < 10; i++) { |
| 113 | + collections.push(`writeConcernTestCol${i + 1}`); |
| 114 | + } |
| 115 | + |
| 116 | + for (const colName of collections) { |
| 117 | + await db.createCollection(colName).catch(() => null); |
| 118 | + } |
| 119 | + |
| 120 | + const cols = db.listCollections({}, { batchSize: 2 }); |
| 121 | + |
| 122 | + const err = await cols.toArray().catch(e => e); |
| 123 | + |
| 124 | + expect(err).to.not.be.instanceOf(Error); |
| 125 | + }); |
| 126 | + |
| 127 | + it('succeeds on aggregate', async function () { |
| 128 | + const aggResult = col.aggregate([{ $match: { a: { $gte: 0 } } }], { batchSize: 2 }); |
| 129 | + const err = await aggResult.toArray().catch(e => e); |
| 130 | + |
| 131 | + expect(err).to.not.be.instanceOf(Error); |
| 132 | + }); |
| 133 | + |
| 134 | + it('succeeds on listIndexes', async function () { |
| 135 | + await col.createIndex({ a: 1 }); |
| 136 | + await col.createIndex({ b: -1 }); |
| 137 | + await col.createIndex({ a: 1, b: -1 }); |
| 138 | + |
| 139 | + const listIndexesResult = col.listIndexes({ batchSize: 2 }); |
| 140 | + const err = await listIndexesResult.toArray().catch(e => e); |
| 141 | + |
| 142 | + expect(err).to.not.be.instanceOf(Error); |
| 143 | + }); |
| 144 | + |
| 145 | + it('succeeds on changeStream', { |
| 146 | + metadata: { requires: { topology: 'replicaset' } }, |
| 147 | + async test() { |
| 148 | + const changeStream = col.watch(undefined, { batchSize: 2 }); |
| 149 | + const changes = on(changeStream, 'change'); |
| 150 | + await once(changeStream.cursor, 'init'); |
| 151 | + |
| 152 | + await col.insertMany( |
| 153 | + [ |
| 154 | + { a: 10 }, |
| 155 | + { a: 10 }, |
| 156 | + { a: 10 }, |
| 157 | + { a: 10 }, |
| 158 | + { a: 10 }, |
| 159 | + { a: 10 }, |
| 160 | + { a: 10 }, |
| 161 | + { a: 10 }, |
| 162 | + { a: 10 }, |
| 163 | + { a: 10 }, |
| 164 | + { a: 10 }, |
| 165 | + { a: 10 } |
| 166 | + ], |
| 167 | + { writeConcern: { w: 'majority' } } |
| 168 | + ); |
| 169 | + |
| 170 | + const err = await changes.next().catch(e => e); |
| 171 | + expect(err).to.not.be.instanceOf(Error); |
| 172 | + } |
| 173 | + }); |
| 174 | + }); |
| 175 | + }); |
| 176 | + }); |
| 177 | +}); |
0 commit comments