Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
252 changes: 105 additions & 147 deletions test/integration/read-write-concern/readconcern.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect } from 'chai';

import { type MongoClient } from '../../../src';
import { type MongoClient, ReadConcernLevel } from '../../../src';
import { filterForCommands, setupDatabase } from '../shared';

describe.only('ReadConcern', function () {
describe('ReadConcern', function () {
let client: MongoClient;

before(function () {
Expand All @@ -29,35 +29,35 @@ describe.only('ReadConcern', function () {
{
description: 'Should set local readConcern on db level when using collection method',
commandName: 'find',
readConcern: { level: 'local' }
readConcern: { level: ReadConcernLevel.local }
},
{
description: 'Should set majority readConcern on db level',
commandName: 'find',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
},
{
description: 'Should set majority readConcern aggregate command',
commandName: 'aggregate',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
},
{
description: 'Should set local readConcern at collection level',
commandName: 'find',
readConcern: { level: 'local' }
readConcern: { level: ReadConcernLevel.local }
},
{
description: 'Should set majority readConcern at collection level',
commandName: 'find',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
}
];

tests.forEach(test => {
it(test.description, {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },

test: function (done) {
test: async function () {
const started = [];
const succeeded = [];
// Get a new instance
Expand All @@ -67,37 +67,29 @@ describe.only('ReadConcern', function () {
{ maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true }
);

client.connect((err, client) => {
expect(err).to.not.exist;
await client.connect();

const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);
const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);

// Get a collection
const collection = db.collection('readConcernCollection');
// Get a collection
const collection = db.collection('readConcernCollection');

// Validate readConcern
expect(collection.readConcern).to.deep.equal(test.readConcern);
// Validate readConcern
expect(collection.readConcern).to.deep.equal(test.readConcern);

// commandMonitoring
client.on('commandStarted', filterForCommands(test.commandName, started));
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));
// commandMonitoring
client.on('commandStarted', filterForCommands(test.commandName, started));
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));

// Execute find
if (test.commandName === 'find') {
collection.find().toArray(err => {
expect(err).to.not.exist;
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
done();
});
} else if (test.commandName === 'aggregate') {
collection.aggregate([{ $match: {} }]).toArray(err => {
expect(err).to.not.exist;
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
done();
});
}
});
// Execute find
if (test.commandName === 'find') {
await collection.find().toArray();
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
} else if (test.commandName === 'aggregate') {
await collection.aggregate([{ $match: {} }]).toArray();
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
}
}
});
});
Expand All @@ -107,23 +99,23 @@ describe.only('ReadConcern', function () {
{
description: 'Should set local readConcern using MongoClient',
urlReadConcernLevel: 'readConcernLevel=local',
readConcern: { level: 'local' }
readConcern: { level: ReadConcernLevel.local }
},
{
description: 'Should set majority readConcern using MongoClient',
urlReadConcernLevel: 'readConcernLevel=majority',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
},
{
description: 'Should set majority readConcern using MongoClient with options',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
}
];

urlTests.forEach(test => {
it(test.description, {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },
test: function (done) {
test: async function () {
const started = [];
const succeeded = [];
// Get a new instance
Expand All @@ -143,29 +135,24 @@ describe.only('ReadConcern', function () {
});
}

client.connect((err, client) => {
expect(err).to.not.exist;
await client.connect();

const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);
const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);

// Get a collection
const collection = db.collection('readConcernCollection');
// Get a collection
const collection = db.collection('readConcernCollection');

// Validate readConcern
expect(collection.readConcern).to.deep.equal(test.readConcern);
// Validate readConcern
expect(collection.readConcern).to.deep.equal(test.readConcern);

// commandMonitoring
client.on('commandStarted', filterForCommands('find', started));
client.on('commandSucceeded', filterForCommands('find', succeeded));
// commandMonitoring
client.on('commandStarted', filterForCommands('find', started));
client.on('commandSucceeded', filterForCommands('find', succeeded));

// Execute find
collection.find().toArray(err => {
expect(err).to.not.exist;
validateTestResults(started, succeeded, 'find', test.readConcern.level);
done();
});
});
// Execute find
await collection.find().toArray();
validateTestResults(started, succeeded, 'find', test.readConcern.level);
}
});
});
Expand All @@ -176,21 +163,21 @@ describe.only('ReadConcern', function () {
description: 'Should set majority readConcern distinct command',
commandName: 'distinct',
mongodbVersion: '>= 3.2',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
},
{
description: 'Should set majority readConcern count command',
commandName: 'count',
mongodbVersion: '>= 3.2',
readConcern: { level: 'majority' }
readConcern: { level: ReadConcernLevel.majority }
}
];

insertTests.forEach(test => {
it(test.description, {
metadata: { requires: { topology: 'replicaset', mongodb: test.mongodbVersion } },

test: function (done) {
test: async function () {
const started = [];
const succeeded = [];
// Get a new instance
Expand All @@ -200,58 +187,47 @@ describe.only('ReadConcern', function () {
{ maxPoolSize: 1, readConcern: test.readConcern, monitorCommands: true }
);

client.connect((err, client) => {
expect(err).to.not.exist;

const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);
await client.connect();

// Get the collection
const collection = db.collection('readConcernCollection');
const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal(test.readConcern);

// Get the collection
const collection = db.collection('readConcernCollection');

// Insert documents to perform distinct against
await collection.insertMany(
[
{ a: 0, b: { c: 'a' } },
{ a: 1, b: { c: 'b' } },
{ a: 1, b: { c: 'c' } },
{ a: 2, b: { c: 'a' } },
{ a: 3 },
{ a: 3 }
],
configuration.writeConcernMax()
);

// Insert documents to perform distinct against
collection.insertMany(
[
{ a: 0, b: { c: 'a' } },
{ a: 1, b: { c: 'b' } },
{ a: 1, b: { c: 'c' } },
{ a: 2, b: { c: 'a' } },
{ a: 3 },
{ a: 3 }
],
configuration.writeConcernMax(),
err => {
expect(err).to.not.exist;

// Listen to apm events
client.on('commandStarted', filterForCommands(test.commandName, started));
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));

// Perform a distinct query against the a field
if (test.commandName === 'distinct') {
collection.distinct('a', err => {
expect(err).to.not.exist;
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
done();
});
} else if (test.commandName === 'count') {
collection.estimatedDocumentCount(err => {
expect(err).to.not.exist;
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
done();
});
}
}
);
});
// Listen to apm events
client.on('commandStarted', filterForCommands(test.commandName, started));
client.on('commandSucceeded', filterForCommands(test.commandName, succeeded));

// Perform a distinct query against the a field
if (test.commandName === 'distinct') {
await collection.distinct('a');
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
} else if (test.commandName === 'count') {
await collection.estimatedDocumentCount();
validateTestResults(started, succeeded, test.commandName, test.readConcern.level);
}
}
});
});

it('Should set majority readConcern aggregate command against server >= 4.1', {
metadata: { requires: { topology: 'replicaset' } },

test: function (done) {
test: async function () {
const started = [];
const succeeded = [];
// Get a new instance
Expand All @@ -261,65 +237,47 @@ describe.only('ReadConcern', function () {
{ maxPoolSize: 1, readConcern: { level: 'majority' }, monitorCommands: true }
);

client
.connect()
.then(() => {
// Get a collection
const collection = client
.db(configuration.db)
.collection('readConcernCollectionAggregate1');
await client.connect();
// Get a collection
const collection = client.db(configuration.db).collection('readConcernCollectionAggregate1');

// Listen to apm events
client.on('commandStarted', filterForCommands('aggregate', started));
client.on('commandSucceeded', filterForCommands('aggregate', succeeded));
// Listen to apm events
client.on('commandStarted', filterForCommands('aggregate', started));
client.on('commandSucceeded', filterForCommands('aggregate', succeeded));

// Execute find
return collection
.aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }])
.toArray()
.then(() => {
validateTestResults(started, succeeded, 'aggregate', 'majority');

// Execute find
return collection
.aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' })
.toArray()
.then(() => {
validateTestResults(started, succeeded, 'aggregate', 'majority');
});
});
})
.then(
() => client.close(done),
e => client.close(() => done(e))
);
// Execute find
await collection
.aggregate([{ $match: {} }, { $out: 'readConcernCollectionAggregate1Output' }])
.toArray();

validateTestResults(started, succeeded, 'aggregate', 'majority');

// Execute find
await collection
.aggregate([{ $match: {} }], { out: 'readConcernCollectionAggregate2Output' })
.toArray();
validateTestResults(started, succeeded, 'aggregate', 'majority');
}
});

it('Should set local readConcern on db level when using createCollection method', {
metadata: { requires: { topology: 'replicaset', mongodb: '>= 3.2' } },

test: function (done) {
test: async function () {
// Get a new instance
const configuration = this.configuration;
client = configuration.newClient(
{ w: 1 },
{ maxPoolSize: 1, readConcern: { level: 'local' } }
);
client.connect((err, client) => {
expect(err).to.not.exist;
const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal({ level: 'local' });

// Get a collection using createCollection
db.createCollection('readConcernCollection_createCollection', (err, collection) => {
expect(err).to.not.exist;

// Validate readConcern
expect(collection.readConcern).to.deep.equal({ level: 'local' });
done();
});
});
await client.connect();
const db = client.db(configuration.db);
expect(db.readConcern).to.deep.equal({ level: 'local' });

// Get a collection using createCollection
const collection = await db.createCollection('readConcernCollection_createCollection');
// Validate readConcern
expect(collection.readConcern).to.deep.equal({ level: 'local' });
}
});
});