@@ -6,11 +6,10 @@ var name = "toptest";
66
77var testDB = db . getSiblingDB ( name ) ;
88var testColl = testDB [ name + "coll" ] ;
9-
10- // Ensure an empty collection exists for first top command
119testColl . drop ( ) ;
12- testColl . insert ( { x : 0 } ) ;
13- testColl . remove ( { x : 0 } ) ;
10+
11+ // Perform an operation on the collection so that it is present in the "top" command's output.
12+ assert . eq ( testColl . find ( { } ) . itcount ( ) , 0 ) ;
1413
1514// get top statistics for the test collection
1615function getTop ( ) {
@@ -23,7 +22,7 @@ var lastTop = getTop();
2322// return the number of operations since the last call to diffTop for the specified key
2423function diffTop ( key ) {
2524 var thisTop = getTop ( ) ;
26- difference = {
25+ var difference = {
2726 time : thisTop [ key ] . time - lastTop [ key ] . time ,
2827 count : thisTop [ key ] . count - lastTop [ key ] . count
2928 } ;
@@ -33,7 +32,7 @@ function diffTop(key) {
3332 assert . gte ( difference . time , 0 , "non-decreasing time" ) ;
3433
3534 // Time should advance iff operations were performed
36- assert . eq ( difference . count != 0 , difference . time > 0 , "non-zero time iff non-zero count" ) ;
35+ assert . eq ( difference . count !== 0 , difference . time > 0 , "non-zero time iff non-zero count" ) ;
3736 return difference ;
3837}
3938
@@ -48,15 +47,15 @@ function checkStats(key, expected) {
4847}
4948
5049// Insert
51- for ( i = 0 ; i < numRecords ; i ++ ) {
52- testColl . insert ( { _id : i } ) ;
50+ for ( var i = 0 ; i < numRecords ; i ++ ) {
51+ assert . writeOK ( testColl . insert ( { _id : i } ) ) ;
5352}
5453checkStats ( "insert" , numRecords ) ;
5554checkStats ( "writeLock" , numRecords ) ;
5655
5756// Update
5857for ( i = 0 ; i < numRecords ; i ++ ) {
59- testColl . update ( { _id : i } , { x : i } ) ;
58+ assert . writeOK ( testColl . update ( { _id : i } , { x : i } ) ) ;
6059}
6160checkStats ( "update" , numRecords ) ;
6261
@@ -79,25 +78,52 @@ checkStats("getmore", numRecords);
7978
8079// Remove
8180for ( i = 0 ; i < numRecords ; i ++ ) {
82- testColl . remove ( { _id : 1 } ) ;
81+ assert . writeOK ( testColl . remove ( { _id : 1 } ) ) ;
8382}
8483checkStats ( "remove" , numRecords ) ;
8584
8685// Upsert, note that these are counted as updates, not inserts
8786for ( i = 0 ; i < numRecords ; i ++ ) {
88- testColl . update ( { _id : i } , { x : i } , { upsert : 1 } ) ;
87+ assert . writeOK ( testColl . update ( { _id : i } , { x : i } , { upsert : 1 } ) ) ;
8988}
9089checkStats ( "update" , numRecords ) ;
9190
9291// Commands
92+ var res ;
93+
94+ // "count" command
9395diffTop ( "commands" ) ; // ignore any commands before this
9496for ( i = 0 ; i < numRecords ; i ++ ) {
95- assert . eq ( testDB . runCommand ( { count : "toptestcoll" } ) . n , numRecords ) ;
97+ res = assert . commandWorked ( testDB . runCommand ( { count : testColl . getName ( ) } ) ) ;
98+ assert . eq ( res . n , numRecords , tojson ( res ) ) ;
99+ }
100+ checkStats ( "commands" , numRecords ) ;
101+
102+ // "findAndModify" command
103+ diffTop ( "commands" ) ;
104+ for ( i = 0 ; i < numRecords ; i ++ ) {
105+ res = assert . commandWorked ( testDB . runCommand ( {
106+ findAndModify : testColl . getName ( ) ,
107+ query : { _id : i } ,
108+ update : { $inc : { x : 1 } } ,
109+ } ) ) ;
110+ assert . eq ( res . value . x , i , tojson ( res ) ) ;
111+ }
112+ checkStats ( "commands" , numRecords ) ;
113+
114+ diffTop ( "commands" ) ;
115+ for ( i = 0 ; i < numRecords ; i ++ ) {
116+ res = assert . commandWorked ( testDB . runCommand ( {
117+ findAndModify : testColl . getName ( ) ,
118+ query : { _id : i } ,
119+ remove : true ,
120+ } ) ) ;
121+ assert . eq ( res . value . x , i + 1 , tojson ( res ) ) ;
96122}
97123checkStats ( "commands" , numRecords ) ;
98124
99- for ( key in lastTop ) {
100- if ( ! ( key in checked ) ) {
125+ for ( var key of Object . keys ( lastTop ) ) {
126+ if ( ! checked . hasOwnProperty ( key ) ) {
101127 printjson ( { key : key , stats : diffTop ( key ) } ) ;
102128 }
103129}
0 commit comments