@@ -13,7 +13,6 @@ 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' ) ;
1716
1817describe ( 'Cursor' , function ( ) {
1918 before ( function ( ) {
@@ -1906,31 +1905,61 @@ describe('Cursor', function () {
19061905 }
19071906 } ) ;
19081907
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 ) ;
1908+ it ( 'should close dead tailable cursors' , {
1909+ metadata : {
1910+ os : '!win32' // NODE-2943: timeout on windows
1911+ } ,
19141912
1915- const collection = await client
1916- . db ( )
1917- . createCollection ( 'test_cleanup_tailable' , { capped : true , size : 1000 , max : 3 } ) ;
1913+ test : function ( done ) {
1914+ // http://www.mongodb.org/display/DOCS/Tailable+Cursors
19181915
1919- // insert only 2 docs in capped coll of 3
1920- await collection . insertMany ( [ { a : 1 } , { a : 1 } ] ) ;
1916+ const configuration = this . configuration ;
1917+ client . connect ( ( err , client ) => {
1918+ expect ( err ) . to . not . exist ;
1919+ this . defer ( ( ) => client . close ( ) ) ;
19211920
1922- const cursor = collection . find ( { } , { tailable : true , awaitData : true , maxAwaitTimeMS : 2000 } ) ;
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 ;
19231928
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 ) ;
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 ;
19281933
1929- await client . close ( ) ;
1930- expect ( cursor ) . to . have . property ( 'killed' , true ) ;
1934+ const cursor = collection . find ( { } , { tailable : true , awaitData : true } ) ;
1935+ const stream = cursor . stream ( ) ;
1936+
1937+ stream . resume ( ) ;
1938+
1939+ var validator = ( ) => {
1940+ closeCount ++ ;
1941+ if ( closeCount === 2 ) {
1942+ done ( ) ;
1943+ }
1944+ } ;
1945+
1946+ // we validate that the stream "ends" either cleanly or with an error
1947+ stream . on ( 'end' , validator ) ;
1948+ stream . on ( 'error' , validator ) ;
1949+
1950+ cursor . on ( 'close' , validator ) ;
19311951
1932- const error = await rejectedEarlyBecauseClientClosed ;
1933- expect ( error ) . to . be . instanceOf ( MongoExpiredSessionError ) ;
1952+ const docs = Array . from ( { length : 100 } ) . map ( ( ) => ( { a : 1 } ) ) ;
1953+ collection . insertMany ( docs , err => {
1954+ expect ( err ) . to . not . exist ;
1955+
1956+ setTimeout ( ( ) => client . close ( ) ) ;
1957+ } ) ;
1958+ } ) ;
1959+ }
1960+ ) ;
1961+ } ) ;
1962+ }
19341963 } ) ;
19351964
19361965 it ( 'shouldAwaitData' , {
0 commit comments