Skip to content

Commit 4bed279

Browse files
async-await refactor
1 parent 5272d63 commit 4bed279

File tree

1 file changed

+73
-74
lines changed

1 file changed

+73
-74
lines changed

test/integration/client-side-encryption/client_side_encryption.prose.deadlock.ts

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { getEncryptExtraOptions } from '../../tools/utils';
1010
import { dropCollection } from '../shared';
1111

1212
/* REFERENCE: (note commit hash) */
13-
/* https://github.com/mongodb/specifications/blob/b3beada72ae1c992294ae6a8eea572003a274c35/source/client-side-encryption/tests/README.rst#deadlock-tests */
13+
/* https://github.com/mongodb/specifications/blob/b3beada 72ae1c992294ae6a8eea572003a274c35/source/client-side-encryption/tests/README.rst#deadlock-tests */
1414

1515
const LOCAL_KEY = Buffer.from(
1616
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk',
@@ -35,8 +35,8 @@ const kClientsCreated = Symbol('clientsCreated');
3535
class CapturingMongoClient extends MongoClient {
3636
[kEvents]: Array<CommandStartedEvent> = [];
3737
[kClientsCreated] = 0;
38-
constructor(url, options: MongoClientOptions = {}) {
39-
options = { ...options, monitorCommands: true };
38+
constructor(url: string, options: MongoClientOptions = {}) {
39+
options = { ...options, monitorCommands: true, [Symbol.for('@@mdb.skipPingOnConnect')]: true };
4040
if (process.env.MONGODB_API_VERSION) {
4141
options.serverApi = process.env.MONGODB_API_VERSION as MongoClientOptions['serverApi'];
4242
}
@@ -48,8 +48,15 @@ class CapturingMongoClient extends MongoClient {
4848
}
4949
}
5050

51-
function deadlockTest(options, assertions) {
52-
return function () {
51+
function deadlockTest(
52+
{
53+
maxPoolSize,
54+
bypassAutoEncryption,
55+
useKeyVaultClient
56+
}: { maxPoolSize: number; useKeyVaultClient: boolean; bypassAutoEncryption: boolean },
57+
assertions
58+
) {
59+
return async function () {
5360
const url = this.configuration.url();
5461
const clientTest = this.clientTest;
5562
const ciphertext = this.ciphertext;
@@ -58,46 +65,44 @@ function deadlockTest(options, assertions) {
5865
autoEncryption: {
5966
keyVaultNamespace: 'keyvault.datakeys',
6067
kmsProviders: { local: { key: LOCAL_KEY } },
61-
bypassAutoEncryption: options.bypassAutoEncryption,
62-
keyVaultClient: options.useKeyVaultClient ? this.clientKeyVault : undefined,
68+
bypassAutoEncryption,
69+
keyVaultClient: useKeyVaultClient ? this.clientKeyVault : undefined,
6370
extraOptions: getEncryptExtraOptions()
6471
},
65-
maxPoolSize: options.maxPoolSize
72+
maxPoolSize
6673
};
74+
6775
const clientEncrypted = new CapturingMongoClient(url, clientEncryptedOpts);
6876

69-
return clientEncrypted
70-
.connect()
71-
.then(() => {
72-
if (clientEncryptedOpts.autoEncryption.bypassAutoEncryption === true) {
73-
return clientTest
74-
.db('db')
75-
.collection('coll')
76-
.insertOne({ _id: 0, encrypted: ciphertext });
77-
}
78-
return clientEncrypted
77+
await clientEncrypted.connect();
78+
79+
try {
80+
if (bypassAutoEncryption) {
81+
await clientTest.db('db').collection('coll').insertOne({ _id: 0, encrypted: ciphertext });
82+
} else {
83+
await clientEncrypted
7984
.db('db')
8085
.collection('coll')
8186
.insertOne({ _id: 0, encrypted: 'string0' });
82-
})
83-
.then(() => clientEncrypted.db('db').collection('coll').findOne({ _id: 0 }))
84-
.then(res => {
85-
expect(res).to.have.property('_id', 0);
86-
expect(res).to.have.property('encrypted', 'string0');
87-
assertions(clientEncrypted, this.clientKeyVault);
88-
return clientEncrypted.close();
89-
});
87+
}
88+
89+
const res = await clientEncrypted.db('db').collection('coll').findOne({ _id: 0 });
90+
91+
expect(res).to.have.property('_id', 0);
92+
expect(res).to.have.property('encrypted', 'string0');
93+
assertions(clientEncrypted, this.clientKeyVault);
94+
} finally {
95+
await clientEncrypted.close();
96+
}
9097
};
9198
}
9299

93-
function deadlockTests(_metadata) {
94-
const metadata = { ..._metadata, requires: { ..._metadata.requires, auth: 'disabled' } };
95-
metadata.skipReason = 'TODO: NODE-3891 - fix tests broken when AUTH enabled';
96-
describe('Connection Pool Deadlock Prevention', function () {
100+
function deadlockTests(metadata) {
101+
describe.only('Connection Pool Deadlock Prevention', function () {
97102
installNodeDNSWorkaroundHooks();
98-
beforeEach(function () {
103+
beforeEach(async function () {
99104
const mongodbClientEncryption = this.configuration.mongodbClientEncryption;
100-
const url = this.configuration.url();
105+
const url: string = this.configuration.url();
101106

102107
this.clientTest = new CapturingMongoClient(url);
103108
this.clientKeyVault = new CapturingMongoClient(url, {
@@ -108,41 +113,34 @@ function deadlockTests(_metadata) {
108113
this.clientEncryption = undefined;
109114
this.ciphertext = undefined;
110115

111-
return this.clientTest
112-
.connect()
113-
.then(() => this.clientKeyVault.connect())
114-
.then(() => dropCollection(this.clientTest.db('keyvault'), 'datakeys'))
115-
.then(() => dropCollection(this.clientTest.db('db'), 'coll'))
116-
.then(() =>
117-
this.clientTest
118-
.db('keyvault')
119-
.collection('datakeys')
120-
.insertOne(externalKey, {
121-
writeConcern: { w: 'majority' }
122-
})
123-
)
124-
.then(() =>
125-
this.clientTest.db('db').createCollection('coll', { validator: { $jsonSchema } })
126-
)
127-
.then(() => {
128-
this.clientEncryption = new mongodbClientEncryption.ClientEncryption(this.clientTest, {
129-
kmsProviders: { local: { key: LOCAL_KEY } },
130-
keyVaultNamespace: 'keyvault.datakeys',
131-
keyVaultClient: this.keyVaultClient,
132-
extraOptions: getEncryptExtraOptions()
133-
});
134-
this.clientEncryption.encryptPromisified = util.promisify(
135-
this.clientEncryption.encrypt.bind(this.clientEncryption)
136-
);
137-
138-
return this.clientEncryption.encryptPromisified('string0', {
139-
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
140-
keyAltName: 'local'
141-
});
142-
})
143-
.then(ciphertext => {
144-
this.ciphertext = ciphertext;
116+
await this.clientTest.connect();
117+
await this.clientKeyVault.connect();
118+
await dropCollection(this.clientTest.db('keyvault'), 'datakeys');
119+
await dropCollection(this.clientTest.db('db'), 'coll');
120+
121+
await this.clientTest
122+
.db('keyvault')
123+
.collection('datakeys')
124+
.insertOne(externalKey, {
125+
writeConcern: { w: 'majority' }
145126
});
127+
128+
await this.clientTest.db('db').createCollection('coll', { validator: { $jsonSchema } });
129+
130+
this.clientEncryption = new mongodbClientEncryption.ClientEncryption(this.clientTest, {
131+
kmsProviders: { local: { key: LOCAL_KEY } },
132+
keyVaultNamespace: 'keyvault.datakeys',
133+
keyVaultClient: this.keyVaultClient,
134+
extraOptions: getEncryptExtraOptions()
135+
});
136+
this.clientEncryption.encryptPromisified = util.promisify(
137+
this.clientEncryption.encrypt.bind(this.clientEncryption)
138+
);
139+
140+
this.ciphertext = await this.clientEncryption.encryptPromisified('string0', {
141+
algorithm: 'AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic',
142+
keyAltName: 'local'
143+
});
146144
});
147145

148146
afterEach(function () {
@@ -155,7 +153,7 @@ function deadlockTests(_metadata) {
155153

156154
const CASE1 = { maxPoolSize: 1, bypassAutoEncryption: false, useKeyVaultClient: false };
157155
it(
158-
'Case 1',
156+
`Case 1: ${JSON.stringify(CASE1)}`,
159157
metadata,
160158
deadlockTest(CASE1, clientEncrypted => {
161159
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(2);
@@ -179,7 +177,7 @@ function deadlockTests(_metadata) {
179177

180178
const CASE2 = { maxPoolSize: 1, bypassAutoEncryption: false, useKeyVaultClient: true };
181179
it(
182-
'Case 2',
180+
`Case 2: ${JSON.stringify(CASE2)}`,
183181
metadata,
184182
deadlockTest(CASE2, (clientEncrypted, clientKeyVault) => {
185183
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(2);
@@ -206,7 +204,7 @@ function deadlockTests(_metadata) {
206204

207205
const CASE3 = { maxPoolSize: 1, bypassAutoEncryption: true, useKeyVaultClient: false };
208206
it(
209-
'Case 3',
207+
`Case 3: ${JSON.stringify(CASE3)}`,
210208
metadata,
211209
deadlockTest(CASE3, clientEncrypted => {
212210
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(2);
@@ -224,7 +222,7 @@ function deadlockTests(_metadata) {
224222

225223
const CASE4 = { maxPoolSize: 1, bypassAutoEncryption: true, useKeyVaultClient: true };
226224
it(
227-
'Case 4',
225+
`Case 4: ${JSON.stringify(CASE4)}`,
228226
metadata,
229227
deadlockTest(CASE4, (clientEncrypted, clientKeyVault) => {
230228
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(1);
@@ -245,7 +243,7 @@ function deadlockTests(_metadata) {
245243

246244
const CASE5 = { maxPoolSize: 0, bypassAutoEncryption: false, useKeyVaultClient: false };
247245
it(
248-
'Case 5',
246+
`Case 5: ${JSON.stringify(CASE5)}`,
249247
metadata,
250248
deadlockTest(CASE5, clientEncrypted => {
251249
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(1);
@@ -272,7 +270,8 @@ function deadlockTests(_metadata) {
272270

273271
const CASE6 = { maxPoolSize: 0, bypassAutoEncryption: false, useKeyVaultClient: true };
274272
it(
275-
'Case 6',
273+
`Case 6: ${JSON.stringify(CASE6)}`,
274+
276275
metadata,
277276
deadlockTest(CASE6, (clientEncrypted, clientKeyVault) => {
278277
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(1);
@@ -299,7 +298,7 @@ function deadlockTests(_metadata) {
299298

300299
const CASE7 = { maxPoolSize: 0, bypassAutoEncryption: true, useKeyVaultClient: false };
301300
it(
302-
'Case 7',
301+
`Case 7: ${JSON.stringify(CASE7)}`,
303302
metadata,
304303
deadlockTest(CASE7, clientEncrypted => {
305304
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(1);
@@ -317,7 +316,7 @@ function deadlockTests(_metadata) {
317316

318317
const CASE8 = { maxPoolSize: 0, bypassAutoEncryption: true, useKeyVaultClient: true };
319318
it(
320-
'Case 8',
319+
`Case 8: ${JSON.stringify(CASE8)}`,
321320
metadata,
322321
deadlockTest(CASE8, (clientEncrypted, clientKeyVault) => {
323322
expect(clientEncrypted[kClientsCreated], 'Incorrect number of clients created').to.equal(1);

0 commit comments

Comments
 (0)