@@ -10,7 +10,7 @@ import { getEncryptExtraOptions } from '../../tools/utils';
10
10
import { dropCollection } from '../shared' ;
11
11
12
12
/* 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 */
14
14
15
15
const LOCAL_KEY = Buffer . from (
16
16
'Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk' ,
@@ -35,8 +35,8 @@ const kClientsCreated = Symbol('clientsCreated');
35
35
class CapturingMongoClient extends MongoClient {
36
36
[ kEvents ] : Array < CommandStartedEvent > = [ ] ;
37
37
[ 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 } ;
40
40
if ( process . env . MONGODB_API_VERSION ) {
41
41
options . serverApi = process . env . MONGODB_API_VERSION as MongoClientOptions [ 'serverApi' ] ;
42
42
}
@@ -48,8 +48,15 @@ class CapturingMongoClient extends MongoClient {
48
48
}
49
49
}
50
50
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 ( ) {
53
60
const url = this . configuration . url ( ) ;
54
61
const clientTest = this . clientTest ;
55
62
const ciphertext = this . ciphertext ;
@@ -58,46 +65,44 @@ function deadlockTest(options, assertions) {
58
65
autoEncryption : {
59
66
keyVaultNamespace : 'keyvault.datakeys' ,
60
67
kmsProviders : { local : { key : LOCAL_KEY } } ,
61
- bypassAutoEncryption : options . bypassAutoEncryption ,
62
- keyVaultClient : options . useKeyVaultClient ? this . clientKeyVault : undefined ,
68
+ bypassAutoEncryption,
69
+ keyVaultClient : useKeyVaultClient ? this . clientKeyVault : undefined ,
63
70
extraOptions : getEncryptExtraOptions ( )
64
71
} ,
65
- maxPoolSize : options . maxPoolSize
72
+ maxPoolSize
66
73
} ;
74
+
67
75
const clientEncrypted = new CapturingMongoClient ( url , clientEncryptedOpts ) ;
68
76
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
79
84
. db ( 'db' )
80
85
. collection ( 'coll' )
81
86
. 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
+ }
90
97
} ;
91
98
}
92
99
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 ( ) {
97
102
installNodeDNSWorkaroundHooks ( ) ;
98
- beforeEach ( function ( ) {
103
+ beforeEach ( async function ( ) {
99
104
const mongodbClientEncryption = this . configuration . mongodbClientEncryption ;
100
- const url = this . configuration . url ( ) ;
105
+ const url : string = this . configuration . url ( ) ;
101
106
102
107
this . clientTest = new CapturingMongoClient ( url ) ;
103
108
this . clientKeyVault = new CapturingMongoClient ( url , {
@@ -108,41 +113,34 @@ function deadlockTests(_metadata) {
108
113
this . clientEncryption = undefined ;
109
114
this . ciphertext = undefined ;
110
115
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' }
145
126
} ) ;
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
+ } ) ;
146
144
} ) ;
147
145
148
146
afterEach ( function ( ) {
@@ -155,7 +153,7 @@ function deadlockTests(_metadata) {
155
153
156
154
const CASE1 = { maxPoolSize : 1 , bypassAutoEncryption : false , useKeyVaultClient : false } ;
157
155
it (
158
- ' Case 1' ,
156
+ ` Case 1: ${ JSON . stringify ( CASE1 ) } ` ,
159
157
metadata ,
160
158
deadlockTest ( CASE1 , clientEncrypted => {
161
159
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 2 ) ;
@@ -179,7 +177,7 @@ function deadlockTests(_metadata) {
179
177
180
178
const CASE2 = { maxPoolSize : 1 , bypassAutoEncryption : false , useKeyVaultClient : true } ;
181
179
it (
182
- ' Case 2' ,
180
+ ` Case 2: ${ JSON . stringify ( CASE2 ) } ` ,
183
181
metadata ,
184
182
deadlockTest ( CASE2 , ( clientEncrypted , clientKeyVault ) => {
185
183
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 2 ) ;
@@ -206,7 +204,7 @@ function deadlockTests(_metadata) {
206
204
207
205
const CASE3 = { maxPoolSize : 1 , bypassAutoEncryption : true , useKeyVaultClient : false } ;
208
206
it (
209
- ' Case 3' ,
207
+ ` Case 3: ${ JSON . stringify ( CASE3 ) } ` ,
210
208
metadata ,
211
209
deadlockTest ( CASE3 , clientEncrypted => {
212
210
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 2 ) ;
@@ -224,7 +222,7 @@ function deadlockTests(_metadata) {
224
222
225
223
const CASE4 = { maxPoolSize : 1 , bypassAutoEncryption : true , useKeyVaultClient : true } ;
226
224
it (
227
- ' Case 4' ,
225
+ ` Case 4: ${ JSON . stringify ( CASE4 ) } ` ,
228
226
metadata ,
229
227
deadlockTest ( CASE4 , ( clientEncrypted , clientKeyVault ) => {
230
228
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 1 ) ;
@@ -245,7 +243,7 @@ function deadlockTests(_metadata) {
245
243
246
244
const CASE5 = { maxPoolSize : 0 , bypassAutoEncryption : false , useKeyVaultClient : false } ;
247
245
it (
248
- ' Case 5' ,
246
+ ` Case 5: ${ JSON . stringify ( CASE5 ) } ` ,
249
247
metadata ,
250
248
deadlockTest ( CASE5 , clientEncrypted => {
251
249
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 1 ) ;
@@ -272,7 +270,8 @@ function deadlockTests(_metadata) {
272
270
273
271
const CASE6 = { maxPoolSize : 0 , bypassAutoEncryption : false , useKeyVaultClient : true } ;
274
272
it (
275
- 'Case 6' ,
273
+ `Case 6: ${ JSON . stringify ( CASE6 ) } ` ,
274
+
276
275
metadata ,
277
276
deadlockTest ( CASE6 , ( clientEncrypted , clientKeyVault ) => {
278
277
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 1 ) ;
@@ -299,7 +298,7 @@ function deadlockTests(_metadata) {
299
298
300
299
const CASE7 = { maxPoolSize : 0 , bypassAutoEncryption : true , useKeyVaultClient : false } ;
301
300
it (
302
- ' Case 7' ,
301
+ ` Case 7: ${ JSON . stringify ( CASE7 ) } ` ,
303
302
metadata ,
304
303
deadlockTest ( CASE7 , clientEncrypted => {
305
304
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 1 ) ;
@@ -317,7 +316,7 @@ function deadlockTests(_metadata) {
317
316
318
317
const CASE8 = { maxPoolSize : 0 , bypassAutoEncryption : true , useKeyVaultClient : true } ;
319
318
it (
320
- ' Case 8' ,
319
+ ` Case 8: ${ JSON . stringify ( CASE8 ) } ` ,
321
320
metadata ,
322
321
deadlockTest ( CASE8 , ( clientEncrypted , clientKeyVault ) => {
323
322
expect ( clientEncrypted [ kClientsCreated ] , 'Incorrect number of clients created' ) . to . equal ( 1 ) ;
0 commit comments