@@ -13,7 +13,6 @@ const { ReadPreference } = require('../../mongodb');
13
13
const { ServerType } = require ( '../../mongodb' ) ;
14
14
const { formatSort } = require ( '../../mongodb' ) ;
15
15
const { getSymbolFrom } = require ( '../../tools/utils' ) ;
16
- const { MongoExpiredSessionError } = require ( '../../mongodb' ) ;
17
16
18
17
describe ( 'Cursor' , function ( ) {
19
18
before ( function ( ) {
@@ -1868,31 +1867,61 @@ describe('Cursor', function () {
1868
1867
}
1869
1868
} ) ;
1870
1869
1871
- it ( 'closes cursors when client is closed even if it has not been exhausted' , async function ( ) {
1872
- await client
1873
- . db ( )
1874
- . dropCollection ( 'test_cleanup_tailable' )
1875
- . catch ( ( ) => null ) ;
1870
+ it ( 'should close dead tailable cursors' , {
1871
+ metadata : {
1872
+ os : '!win32' // NODE-2943: timeout on windows
1873
+ } ,
1876
1874
1877
- const collection = await client
1878
- . db ( )
1879
- . createCollection ( 'test_cleanup_tailable' , { capped : true , size : 1000 , max : 3 } ) ;
1875
+ test : function ( done ) {
1876
+ // http://www.mongodb.org/display/DOCS/Tailable+Cursors
1880
1877
1881
- // insert only 2 docs in capped coll of 3
1882
- await collection . insertMany ( [ { a : 1 } , { a : 1 } ] ) ;
1878
+ const configuration = this . configuration ;
1879
+ client . connect ( ( err , client ) => {
1880
+ expect ( err ) . to . not . exist ;
1881
+ this . defer ( ( ) => client . close ( ) ) ;
1883
1882
1884
- const cursor = collection . find ( { } , { tailable : true , awaitData : true , maxAwaitTimeMS : 2000 } ) ;
1883
+ const db = client . db ( configuration . db ) ;
1884
+ const options = { capped : true , size : 10000000 } ;
1885
+ db . createCollection (
1886
+ 'test_if_dead_tailable_cursors_close' ,
1887
+ options ,
1888
+ function ( err , collection ) {
1889
+ expect ( err ) . to . not . exist ;
1885
1890
1886
- await cursor . next ( ) ;
1887
- await cursor . next ( ) ;
1888
- // will block for maxAwaitTimeMS (except we are closing the client)
1889
- const rejectedEarlyBecauseClientClosed = cursor . next ( ) . catch ( error => error ) ;
1891
+ let closeCount = 0 ;
1892
+ const docs = Array . from ( { length : 100 } ) . map ( ( ) => ( { a : 1 } ) ) ;
1893
+ collection . insertMany ( docs , { w : 'majority' , wtimeoutMS : 5000 } , err => {
1894
+ expect ( err ) . to . not . exist ;
1890
1895
1891
- await client . close ( ) ;
1892
- expect ( cursor ) . to . have . property ( 'killed' , true ) ;
1896
+ const cursor = collection . find ( { } , { tailable : true , awaitData : true } ) ;
1897
+ const stream = cursor . stream ( ) ;
1898
+
1899
+ stream . resume ( ) ;
1900
+
1901
+ var validator = ( ) => {
1902
+ closeCount ++ ;
1903
+ if ( closeCount === 2 ) {
1904
+ done ( ) ;
1905
+ }
1906
+ } ;
1907
+
1908
+ // we validate that the stream "ends" either cleanly or with an error
1909
+ stream . on ( 'end' , validator ) ;
1910
+ stream . on ( 'error' , validator ) ;
1911
+
1912
+ cursor . on ( 'close' , validator ) ;
1893
1913
1894
- const error = await rejectedEarlyBecauseClientClosed ;
1895
- expect ( error ) . to . be . instanceOf ( MongoExpiredSessionError ) ;
1914
+ const docs = Array . from ( { length : 100 } ) . map ( ( ) => ( { a : 1 } ) ) ;
1915
+ collection . insertMany ( docs , err => {
1916
+ expect ( err ) . to . not . exist ;
1917
+
1918
+ setTimeout ( ( ) => client . close ( ) ) ;
1919
+ } ) ;
1920
+ } ) ;
1921
+ }
1922
+ ) ;
1923
+ } ) ;
1924
+ }
1896
1925
} ) ;
1897
1926
1898
1927
it ( 'shouldAwaitData' , {
0 commit comments