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