@@ -13,6 +13,7 @@ const { ReadPreference } = require('../../../src/read_preference');
1313const { ServerType } = require ( '../../../src/sdam/common' ) ;
1414const { formatSort } = require ( '../../../src/sort' ) ;
1515const { getSymbolFrom } = require ( '../../tools/utils' ) ;
16+ const { MongoExpiredSessionError } = require ( '../../../src/error' ) ;
1617
1718describe ( 'Cursor' , function ( ) {
1819 before ( function ( ) {
@@ -1905,61 +1906,31 @@ describe('Cursor', function () {
19051906 }
19061907 } ) ;
19071908
1908- it ( 'should close dead tailable cursors' , {
1909- metadata : {
1910- os : '!win32' // NODE-2943: timeout on windows
1911- } ,
1912-
1913- test : function ( done ) {
1914- // http://www.mongodb.org/display/DOCS/Tailable+Cursors
1915-
1916- const configuration = this . configuration ;
1917- client . connect ( ( err , client ) => {
1918- expect ( err ) . to . not . exist ;
1919- this . defer ( ( ) => client . close ( ) ) ;
1920-
1921- const db = client . db ( configuration . db ) ;
1922- const options = { capped : true , size : 10000000 } ;
1923- db . createCollection (
1924- 'test_if_dead_tailable_cursors_close' ,
1925- options ,
1926- function ( err , collection ) {
1927- expect ( err ) . to . not . exist ;
1909+ it ( 'closes cursors when client is closed even if it has not been exhausted' , async function ( ) {
1910+ await client
1911+ . db ( )
1912+ . dropCollection ( 'test_cleanup_tailable' )
1913+ . catch ( ( ) => null ) ;
19281914
1929- let closeCount = 0 ;
1930- const docs = Array . from ( { length : 100 } ) . map ( ( ) => ( { a : 1 } ) ) ;
1931- collection . insertMany ( docs , { w : 'majority' , wtimeoutMS : 5000 } , err => {
1932- expect ( err ) . to . not . exist ;
1933-
1934- const cursor = collection . find ( { } , { tailable : true , awaitData : true } ) ;
1935- const stream = cursor . stream ( ) ;
1915+ const collection = await client
1916+ . db ( )
1917+ . createCollection ( 'test_cleanup_tailable' , { capped : true , size : 1000 , max : 3 } ) ;
19361918
1937- stream . resume ( ) ;
1938-
1939- var validator = ( ) => {
1940- closeCount ++ ;
1941- if ( closeCount === 2 ) {
1942- done ( ) ;
1943- }
1944- } ;
1919+ // insert only 2 docs in capped coll of 3
1920+ await collection . insertMany ( [ { a : 1 } , { a : 1 } ] ) ;
19451921
1946- // we validate that the stream "ends" either cleanly or with an error
1947- stream . on ( 'end' , validator ) ;
1948- stream . on ( 'error' , validator ) ;
1922+ const cursor = collection . find ( { } , { tailable : true , awaitData : true , maxAwaitTimeMS : 2000 } ) ;
19491923
1950- cursor . on ( 'close' , validator ) ;
1924+ await cursor . next ( ) ;
1925+ await cursor . next ( ) ;
1926+ // will block for maxAwaitTimeMS (except we are closing the client)
1927+ const rejectedEarlyBecauseClientClosed = cursor . next ( ) . catch ( error => error ) ;
19511928
1952- const docs = Array . from ( { length : 100 } ) . map ( ( ) => ( { a : 1 } ) ) ;
1953- collection . insertMany ( docs , err => {
1954- expect ( err ) . to . not . exist ;
1929+ await client . close ( ) ;
1930+ expect ( cursor ) . to . have . property ( 'killed' , true ) ;
19551931
1956- setTimeout ( ( ) => client . close ( ) ) ;
1957- } ) ;
1958- } ) ;
1959- }
1960- ) ;
1961- } ) ;
1962- }
1932+ const error = await rejectedEarlyBecauseClientClosed ;
1933+ expect ( error ) . to . be . instanceOf ( MongoExpiredSessionError ) ;
19631934 } ) ;
19641935
19651936 it ( 'shouldAwaitData' , {
0 commit comments