@@ -1052,3 +1052,108 @@ exports['Ensure killcursor commands are sent on 3.0 or earlier when APM is enabl
10521052 } ) ;
10531053 }
10541054}
1055+
1056+ exports [ 'Correcly decorate the apm result for aggregation with cursorId' ] = {
1057+ metadata : { requires : { topology : [ 'single' , 'replicaset' ] , mongodb : ">=3.0.0" } } ,
1058+
1059+ // The actual test we wish to run
1060+ test : function ( configuration , test ) {
1061+ var started = [ ] ;
1062+ var succeeded = [ ] ;
1063+ var failed = [ ] ;
1064+ var callbackTriggered = false ;
1065+
1066+ var listener = require ( '../..' ) . instrument ( function ( err , instrumentations ) { } ) ;
1067+ listener . on ( 'started' , function ( event ) {
1068+ if ( event . commandName == 'aggregate' || event . commandName == 'getMore' )
1069+ started . push ( event ) ;
1070+ } ) ;
1071+
1072+ listener . on ( 'succeeded' , function ( event ) {
1073+ if ( event . commandName == 'aggregate' || event . commandName == 'getMore' )
1074+ succeeded . push ( event ) ;
1075+ } ) ;
1076+
1077+ var db = configuration . newDbInstance ( { w :1 } , { poolSize :1 , auto_reconnect :false } ) ;
1078+ db . open ( function ( err , db ) {
1079+ test . equal ( null , err ) ;
1080+
1081+ // Generate docs
1082+ var docs = [ ] ;
1083+ for ( var i = 0 ; i < 2500 ; i ++ ) {
1084+ docs . push ( { a :i } ) ;
1085+ }
1086+
1087+ db . collection ( 'apm_test_u_4' ) . insertMany ( docs ) . then ( function ( r ) {
1088+
1089+ db . collection ( 'apm_test_u_4' ) . aggregate ( [ { $match : { } } ] ) . toArray ( ) . then ( function ( r ) {
1090+ test . equal ( 3 , started . length ) ;
1091+ test . equal ( 3 , succeeded . length ) ;
1092+ var cursors = succeeded . map ( x => x . reply . cursor ) ;
1093+
1094+ // Check we have a cursor
1095+ test . ok ( cursors [ 0 ] . id ) ;
1096+ test . equal ( cursors [ 0 ] . id . toString ( ) , cursors [ 1 ] . id . toString ( ) ) ;
1097+ test . equal ( 0 , cursors [ 2 ] . id . toString ( ) ) ;
1098+
1099+ listener . uninstrument ( ) ;
1100+
1101+ db . close ( ) ;
1102+ test . done ( ) ;
1103+ } ) ;
1104+ } ) ;
1105+ } ) ;
1106+ }
1107+ }
1108+
1109+ exports [ 'Correcly decorate the apm result for listCollections with cursorId' ] = {
1110+ metadata : { requires : { topology : [ 'single' , 'replicaset' ] , mongodb : ">=3.0.0" } } ,
1111+
1112+ // The actual test we wish to run
1113+ test : function ( configuration , test ) {
1114+ var started = [ ] ;
1115+ var succeeded = [ ] ;
1116+ var failed = [ ] ;
1117+ var callbackTriggered = false ;
1118+
1119+ var listener = require ( '../..' ) . instrument ( function ( err , instrumentations ) { } ) ;
1120+ listener . on ( 'started' , function ( event ) {
1121+ if ( event . commandName == 'listCollections' )
1122+ started . push ( event ) ;
1123+ } ) ;
1124+
1125+ listener . on ( 'succeeded' , function ( event ) {
1126+ // console.dir(event.commandName)
1127+ if ( event . commandName == 'listCollections' )
1128+ succeeded . push ( event ) ;
1129+ } ) ;
1130+
1131+ var db = configuration . newDbInstance ( { w :1 } , { poolSize :1 , auto_reconnect :false } ) ;
1132+ db . open ( function ( err , db ) {
1133+ test . equal ( null , err ) ;
1134+
1135+ var promises = [ ] ;
1136+
1137+ for ( var i = 0 ; i < 20 ; i ++ ) {
1138+ promises . push ( db . collection ( '_mass_collection_' + i ) . insertOne ( { a :1 } ) ) ;
1139+ }
1140+
1141+ Promise . all ( promises ) . then ( function ( r ) {
1142+ db . listCollections ( ) . batchSize ( 10 ) . toArray ( ) . then ( function ( r ) {
1143+ test . equal ( 1 , started . length ) ;
1144+ test . equal ( 1 , succeeded . length ) ;
1145+
1146+ var cursors = succeeded . map ( x => x . reply . cursor ) ;
1147+
1148+ // Check we have a cursor
1149+ test . ok ( cursors [ 0 ] . id ) ;
1150+
1151+ listener . uninstrument ( ) ;
1152+
1153+ db . close ( ) ;
1154+ test . done ( ) ;
1155+ } ) ;
1156+ } ) ;
1157+ } ) ;
1158+ }
1159+ }
0 commit comments