1
+ import { BSON , Int32 , Long , serialize } from 'bson' ;
1
2
import { expect } from 'chai' ;
2
3
import { EventEmitter , once } from 'events' ;
3
4
import * as fs from 'fs/promises' ;
4
5
import { type MongoCryptKMSRequest } from 'mongodb-client-encryption' ;
5
6
import * as net from 'net' ;
6
7
import * as sinon from 'sinon' ;
7
- import { setTimeout } from 'timers' ;
8
8
import { setTimeout as setTimeoutAsync } from 'timers/promises' ;
9
9
import * as tls from 'tls' ;
10
10
11
11
import { StateMachine } from '../../../src/client-side-encryption/state_machine' ;
12
+ import { Collection } from '../../../src/collection' ;
13
+ import { CursorTimeoutContext } from '../../../src/cursor/abstract_cursor' ;
12
14
import { Db } from '../../../src/db' ;
13
- import {
14
- BSON ,
15
- Collection ,
16
- CSOTTimeoutContext ,
17
- CursorTimeoutContext ,
18
- type FindOptions ,
19
- Int32 ,
20
- Long ,
21
- MongoClient ,
22
- serialize ,
23
- squashError
24
- } from '../../mongodb' ;
15
+ import { MongoClient } from '../../../src/mongo_client' ;
16
+ import { type FindOptions } from '../../../src/operations/find' ;
17
+ import { CSOTTimeoutContext } from '../../../src/timeout' ;
18
+ import { squashError } from '../../../src/utils' ;
25
19
import { sleep } from '../../tools/utils' ;
26
20
27
21
describe ( 'StateMachine' , function ( ) {
@@ -129,7 +123,7 @@ describe('StateMachine', function () {
129
123
} ) ;
130
124
} ) ;
131
125
132
- it ( 'should only resolve once bytesNeeded drops to zero' , function ( done ) {
126
+ it ( 'should only resolve once bytesNeeded drops to zero' , async function ( ) {
133
127
const stateMachine = new StateMachine ( { } as any ) ;
134
128
const request = new MockRequest ( Buffer . from ( 'foobar' ) , 500 ) ;
135
129
let status = 'pending' ;
@@ -143,22 +137,21 @@ describe('StateMachine', function () {
143
137
. catch ( ( ) => { } ) ;
144
138
145
139
this . fakeSocket . emit ( 'connect' ) ;
146
- setTimeout ( ( ) => {
147
- expect ( status ) . to . equal ( 'pending' ) ;
148
- expect ( request . bytesNeeded ) . to . equal ( 500 ) ;
149
- expect ( request . kmsProvider ) . to . equal ( 'aws' ) ;
150
- this . fakeSocket . emit ( 'data' , Buffer . alloc ( 300 ) ) ;
151
- setTimeout ( ( ) => {
152
- expect ( status ) . to . equal ( 'pending' ) ;
153
- expect ( request . bytesNeeded ) . to . equal ( 200 ) ;
154
- this . fakeSocket . emit ( 'data' , Buffer . alloc ( 200 ) ) ;
155
- setTimeout ( ( ) => {
156
- expect ( status ) . to . equal ( 'resolved' ) ;
157
- expect ( request . bytesNeeded ) . to . equal ( 0 ) ;
158
- done ( ) ;
159
- } ) ;
160
- } ) ;
161
- } ) ;
140
+ await sleep ( ) ;
141
+
142
+ expect ( status ) . to . equal ( 'pending' ) ;
143
+ expect ( request . bytesNeeded ) . to . equal ( 500 ) ;
144
+ expect ( request . kmsProvider ) . to . equal ( 'aws' ) ;
145
+ this . fakeSocket . emit ( 'data' , Buffer . alloc ( 300 ) ) ;
146
+ await sleep ( ) ;
147
+
148
+ expect ( status ) . to . equal ( 'pending' ) ;
149
+ expect ( request . bytesNeeded ) . to . equal ( 200 ) ;
150
+ this . fakeSocket . emit ( 'data' , Buffer . alloc ( 200 ) ) ;
151
+ await sleep ( ) ;
152
+
153
+ expect ( status ) . to . equal ( 'resolved' ) ;
154
+ expect ( request . bytesNeeded ) . to . equal ( 0 ) ;
162
155
} ) ;
163
156
} ) ;
164
157
@@ -196,13 +189,9 @@ describe('StateMachine', function () {
196
189
} as any ) ;
197
190
const request = new MockRequest ( Buffer . from ( 'foobar' ) , 500 ) ;
198
191
199
- it ( 'rejects with the validation error' , function ( done ) {
200
- stateMachine . kmsRequest ( request ) . catch ( err => {
201
- expect ( err . message ) . to . equal (
202
- `Insecure TLS options prohibited for aws: ${ option } `
203
- ) ;
204
- done ( ) ;
205
- } ) ;
192
+ it ( 'rejects with the validation error' , async function ( ) {
193
+ const err = await stateMachine . kmsRequest ( request ) . catch ( e => e ) ;
194
+ expect ( err . message ) . to . equal ( `Insecure TLS options prohibited for aws: ${ option } ` ) ;
206
195
} ) ;
207
196
} ) ;
208
197
}
0 commit comments