@@ -13,7 +13,6 @@ const { ReadPreference } = require('../../../src/read_preference');
13
13
const { ServerType } = require ( '../../../src/sdam/common' ) ;
14
14
const { formatSort } = require ( '../../../src/sort' ) ;
15
15
const { getSymbolFrom } = require ( '../../tools/utils' ) ;
16
- const { MongoExpiredSessionError } = require ( '../../../src/error' ) ;
17
16
18
17
describe ( 'Cursor' , function ( ) {
19
18
before ( function ( ) {
@@ -1906,31 +1905,61 @@ describe('Cursor', function () {
1906
1905
}
1907
1906
} ) ;
1908
1907
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
+ } ,
1914
1912
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
1918
1915
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 ( ) ) ;
1921
1920
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 ;
1923
1928
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 ;
1928
1933
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 ) ;
1931
1951
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
+ }
1934
1963
} ) ;
1935
1964
1936
1965
it ( 'shouldAwaitData' , {
0 commit comments