Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(NODE-1843): bulk operations ignoring provided sessions #2868

Merged
merged 6 commits into from Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/bulk/common.ts
Expand Up @@ -1200,7 +1200,8 @@ export abstract class BulkOperationBase {
}

this.s.executed = true;
return executeLegacyOperation(this.s.topology, executeCommands, [this, options, callback]);
const finalOptions = { ...this.s.options, ...options };
return executeLegacyOperation(this.s.topology, executeCommands, [this, finalOptions, callback]);
}

/**
Expand Down
142 changes: 142 additions & 0 deletions test/functional/bulk.test.js
Expand Up @@ -2003,4 +2003,146 @@ describe('Bulk', function () {
);
});
});

describe('Bulk operation transaction rollback', () => {
/** @type {import('../../src/index').MongoClient} */
let client;
/** @type {import('../../src/index').Collection<{ answer: number }>} */
let collection;

beforeEach(async function () {
const config = this.configuration;
client = config.newClient();
await client.connect();

try {
await client
.db('bulk_operation_writes_test')
.collection('bulk_write_transaction_test')
.drop();
} catch (_) {
// do not care
}

collection = await client
.db('bulk_operation_writes_test')
.createCollection('bulk_write_transaction_test');

await collection.deleteMany({});
});

afterEach(async () => {
if (client) await client.close();
});

it('should abort ordered bulk operation writes', {
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
async test() {
const session = client.startSession();
session.startTransaction({
readConcern: { level: 'local' },
writeConcern: { w: 'majority' }
});

let bulk = undefined;

bulk = collection.initializeOrderedBulkOp({ session });
bulk.insert({ answer: 42 });
await bulk.execute();

await session.abortTransaction();
await session.endSession();

const documents = await collection.find().toArray();

expect(documents).to.have.lengthOf(
0,
'bulk operation writes were made outside of transaction'
);
}
});

it('should abort unordered bulk operation writes', {
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
async test() {
const session = client.startSession();
session.startTransaction({
readConcern: { level: 'local' },
writeConcern: { w: 'majority' }
});

let bulk = undefined;

bulk = collection.initializeUnorderedBulkOp({ session });
bulk.insert({ answer: 42 });
await bulk.execute();

await session.abortTransaction();
await session.endSession();

const documents = await collection.find().toArray();

expect(documents).to.have.lengthOf(
0,
'bulk operation writes were made outside of transaction'
);
}
});

it('should abort unordered bulk operation writes using withTransaction', {
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
async test() {
const session = client.startSession();

await session.withTransaction(
async () => {
let bulk = undefined;

bulk = collection.initializeUnorderedBulkOp({ session });
bulk.insert({ answer: 42 });
await bulk.execute();
await session.abortTransaction();
},
{ readConcern: { level: 'local' }, writeConcern: { w: 'majority' } }
);

await session.endSession();

const documents = await collection.find().toArray();

expect(documents).to.have.lengthOf(
0,
'bulk operation writes were made outside of transaction'
);
}
});

it('should abort ordered bulk operation writes using withTransaction', {
metadata: { requires: { mongodb: '>= 4.2', topology: ['replicaset'] } },
async test() {
const session = client.startSession();

await session.withTransaction(
async () => {
let bulk = undefined;

bulk = collection.initializeOrderedBulkOp({ session });
bulk.insert({ answer: 42 });
await bulk.execute();
await session.abortTransaction();
},
{ readConcern: { level: 'local' }, writeConcern: { w: 'majority' } }
);

await session.endSession();

const documents = await collection.find().toArray();

expect(documents).to.have.lengthOf(
0,
'bulk operation writes were made outside of transaction'
);
}
});
});
});