@@ -787,6 +787,18 @@ func TestCollection(t *testing.T) {
787
787
_ , ok := err .(mongo.WriteConcernError )
788
788
assert .True (mt , ok , "expected error type %v, got %v" , mongo.WriteConcernError {}, err )
789
789
})
790
+ mt .Run ("getMore commands are monitored" , func (mt * mtest.T ) {
791
+ initCollection (mt , mt .Coll )
792
+ assertGetMoreCommandsAreMonitored (mt , "aggregate" , func () (* mongo.Cursor , error ) {
793
+ return mt .Coll .Aggregate (mtest .Background , mongo.Pipeline {}, options .Aggregate ().SetBatchSize (3 ))
794
+ })
795
+ })
796
+ mt .Run ("killCursors commands are monitored" , func (mt * mtest.T ) {
797
+ initCollection (mt , mt .Coll )
798
+ assertKillCursorsCommandsAreMonitored (mt , "aggregate" , func () (* mongo.Cursor , error ) {
799
+ return mt .Coll .Aggregate (mtest .Background , mongo.Pipeline {}, options .Aggregate ().SetBatchSize (3 ))
800
+ })
801
+ })
790
802
})
791
803
mt .RunOpts ("count documents" , noClientOpts , func (mt * mtest.T ) {
792
804
mt .Run ("success" , func (mt * mtest.T ) {
@@ -1054,6 +1066,18 @@ func TestCollection(t *testing.T) {
1054
1066
})
1055
1067
}
1056
1068
})
1069
+ mt .Run ("getMore commands are monitored" , func (mt * mtest.T ) {
1070
+ initCollection (mt , mt .Coll )
1071
+ assertGetMoreCommandsAreMonitored (mt , "find" , func () (* mongo.Cursor , error ) {
1072
+ return mt .Coll .Find (mtest .Background , bson.D {}, options .Find ().SetBatchSize (3 ))
1073
+ })
1074
+ })
1075
+ mt .Run ("killCursors commands are monitored" , func (mt * mtest.T ) {
1076
+ initCollection (mt , mt .Coll )
1077
+ assertKillCursorsCommandsAreMonitored (mt , "find" , func () (* mongo.Cursor , error ) {
1078
+ return mt .Coll .Find (mtest .Background , bson.D {}, options .Find ().SetBatchSize (3 ))
1079
+ })
1080
+ })
1057
1081
})
1058
1082
mt .RunOpts ("find one" , noClientOpts , func (mt * mtest.T ) {
1059
1083
mt .Run ("limit" , func (mt * mtest.T ) {
@@ -1886,3 +1910,41 @@ func create16MBDocument(mt *mtest.T) bsoncore.Document {
1886
1910
assert .Equal (mt , targetDocSize , len (doc ), "expected document length %v, got %v" , targetDocSize , len (doc ))
1887
1911
return doc
1888
1912
}
1913
+
1914
+ // This is a helper function to ensure that sending getMore commands for a cursor results in command monitoring events
1915
+ // being published. The cursorFn parameter should be a function that yields a cursor which is open on the server and
1916
+ // requires at least one getMore to be fully iterated.
1917
+ func assertGetMoreCommandsAreMonitored (mt * mtest.T , cmdName string , cursorFn func () (* mongo.Cursor , error )) {
1918
+ mt .Helper ()
1919
+ mt .ClearEvents ()
1920
+
1921
+ cursor , err := cursorFn ()
1922
+ assert .Nil (mt , err , "error creating cursor: %v" , err )
1923
+ var docs []bson.D
1924
+ err = cursor .All (mtest .Background , & docs )
1925
+ assert .Nil (mt , err , "All error: %v" , err )
1926
+
1927
+ // Only assert that the initial command and at least one getMore were sent. The exact number of getMore's required
1928
+ // is not important.
1929
+ evt := mt .GetStartedEvent ()
1930
+ assert .Equal (mt , cmdName , evt .CommandName , "expected command %q, got %q" , cmdName , evt .CommandName )
1931
+ evt = mt .GetStartedEvent ()
1932
+ assert .Equal (mt , "getMore" , evt .CommandName , "expected command 'getMore', got %q" , evt .CommandName )
1933
+ }
1934
+
1935
+ // This is a helper function to ensure that sending killCursors commands for a cursor results in command monitoring
1936
+ // events being published. The cursorFn parameter should be a function that yields a cursor which is open on the server.
1937
+ func assertKillCursorsCommandsAreMonitored (mt * mtest.T , cmdName string , cursorFn func () (* mongo.Cursor , error )) {
1938
+ mt .Helper ()
1939
+ mt .ClearEvents ()
1940
+
1941
+ cursor , err := cursorFn ()
1942
+ assert .Nil (mt , err , "error creating cursor: %v" , err )
1943
+ err = cursor .Close (mtest .Background )
1944
+ assert .Nil (mt , err , "Close error: %v" , err )
1945
+
1946
+ evt := mt .GetStartedEvent ()
1947
+ assert .Equal (mt , cmdName , evt .CommandName , "expected command %q, got %q" , cmdName , evt .CommandName )
1948
+ evt = mt .GetStartedEvent ()
1949
+ assert .Equal (mt , "killCursors" , evt .CommandName , "expected command 'killCursors', got %q" , evt .CommandName )
1950
+ }
0 commit comments