@@ -9,6 +9,7 @@ var co = require('co');
9
9
var mock = require ( 'mongodb-mock-server' ) ;
10
10
const chai = require ( 'chai' ) ;
11
11
const expect = chai . expect ;
12
+ const sinon = require ( 'sinon' ) ;
12
13
13
14
chai . use ( require ( 'chai-subset' ) ) ;
14
15
@@ -1876,6 +1877,48 @@ describe('Change Streams', function() {
1876
1877
. then ( ( ) => teardown ( ) , teardown ) ;
1877
1878
} ) ;
1878
1879
1880
+ it ( 'should emit close event after error event' , {
1881
+ metadata : { requires : { topology : 'replicaset' , mongodb : '>=3.5.10' } } ,
1882
+ test : function ( done ) {
1883
+ const configuration = this . configuration ;
1884
+ const client = configuration . newClient ( ) ;
1885
+ const closeSpy = sinon . spy ( ) ;
1886
+
1887
+ client . connect ( function ( err , client ) {
1888
+ expect ( err ) . to . not . exist ;
1889
+
1890
+ const db = client . db ( 'integration_tests' ) ;
1891
+ const coll = db . collection ( 'event_test' ) ;
1892
+
1893
+ // This will cause an error because the _id will be projected out, which causes the following error:
1894
+ // "A change stream document has been received that lacks a resume token (_id)."
1895
+ const changeStream = coll . watch ( [ { $project : { _id : false } } ] ) ;
1896
+
1897
+ changeStream . on ( 'change' , changeDoc => {
1898
+ expect ( changeDoc ) . to . be . null ;
1899
+ } ) ;
1900
+
1901
+ changeStream . on ( 'error' , err => {
1902
+ expect ( err ) . to . exist ;
1903
+ changeStream . close ( ( ) => {
1904
+ expect ( closeSpy . calledOnce ) . to . be . true ;
1905
+ client . close ( done ) ;
1906
+ } ) ;
1907
+ } ) ;
1908
+
1909
+ changeStream . on ( 'close' , closeSpy ) ;
1910
+
1911
+ // Trigger the first database event
1912
+ setTimeout ( ( ) => {
1913
+ coll . insertOne ( { a : 1 } , ( err , result ) => {
1914
+ expect ( err ) . to . not . exist ;
1915
+ expect ( result . insertedCount ) . to . equal ( 1 ) ;
1916
+ } ) ;
1917
+ } ) ;
1918
+ } ) ;
1919
+ }
1920
+ } ) ;
1921
+
1879
1922
describe ( 'should properly handle a changeStream event being processed mid-close' , function ( ) {
1880
1923
let client , coll ;
1881
1924
0 commit comments