@@ -98,6 +98,50 @@ describe('CRUD API', function () {
9898 await collection . drop ( ) . catch ( ( ) => null ) ;
9999 await client . close ( ) ;
100100 } ) ;
101+
102+ describe ( 'when the operation succeeds' , ( ) => {
103+ it ( 'the cursor for findOne is closed' , async function ( ) {
104+ const spy = sinon . spy ( Collection . prototype , 'find' ) ;
105+ const result = await collection . findOne ( { } ) ;
106+ expect ( result ) . to . deep . equal ( { _id : 1 } ) ;
107+ expect ( events . at ( 0 ) ) . to . be . instanceOf ( CommandSucceededEvent ) ;
108+ expect ( spy . returnValues . at ( 0 ) ) . to . have . property ( 'closed' , true ) ;
109+ expect ( spy . returnValues . at ( 0 ) ) . to . have . nested . property ( 'session.hasEnded' , true ) ;
110+ } ) ;
111+ } ) ;
112+
113+ describe ( 'when the find operation fails' , ( ) => {
114+ beforeEach ( async function ( ) {
115+ const failPoint : FailPoint = {
116+ configureFailPoint : 'failCommand' ,
117+ mode : 'alwaysOn' ,
118+ data : {
119+ failCommands : [ 'find' ] ,
120+ // 1 == InternalError, but this value not important to the test
121+ errorCode : 1
122+ }
123+ } ;
124+ await client . db ( ) . admin ( ) . command ( failPoint ) ;
125+ } ) ;
126+
127+ afterEach ( async function ( ) {
128+ const failPoint : FailPoint = {
129+ configureFailPoint : 'failCommand' ,
130+ mode : 'off' ,
131+ data : { failCommands : [ 'find' ] }
132+ } ;
133+ await client . db ( ) . admin ( ) . command ( failPoint ) ;
134+ } ) ;
135+
136+ it ( 'the cursor for findOne is closed' , async function ( ) {
137+ const spy = sinon . spy ( Collection . prototype , 'find' ) ;
138+ const error = await collection . findOne ( { } ) . catch ( error => error ) ;
139+ expect ( error ) . to . be . instanceOf ( MongoServerError ) ;
140+ expect ( events . at ( 0 ) ) . to . be . instanceOf ( CommandFailedEvent ) ;
141+ expect ( spy . returnValues . at ( 0 ) ) . to . have . property ( 'closed' , true ) ;
142+ expect ( spy . returnValues . at ( 0 ) ) . to . have . nested . property ( 'session.hasEnded' , true ) ;
143+ } ) ;
144+ } ) ;
101145 } ) ;
102146
103147 describe ( 'countDocuments()' , ( ) => {
0 commit comments