7
7
package integration
8
8
9
9
import (
10
+ "bytes"
10
11
"errors"
11
12
"testing"
12
13
@@ -15,10 +16,11 @@ import (
15
16
"go.mongodb.org/mongo-driver/mongo"
16
17
"go.mongodb.org/mongo-driver/mongo/integration/mtest"
17
18
"go.mongodb.org/mongo-driver/mongo/options"
19
+ "go.mongodb.org/mongo-driver/mongo/readpref"
18
20
"go.mongodb.org/mongo-driver/mongo/writeconcern"
19
21
)
20
22
21
- func TestCrudProse (t * testing.T ) {
23
+ func TestWriteErrorsWithLabels (t * testing.T ) {
22
24
clientOpts := options .Client ().SetRetryWrites (false ).SetWriteConcern (mtest .MajorityWc ).
23
25
SetReadConcern (mtest .MajorityRc )
24
26
mtOpts := mtest .NewOptions ().ClientOptions (clientOpts ).MinServerVersion ("4.0" ).Topologies (mtest .ReplicaSet ).
@@ -107,6 +109,7 @@ func TestCrudProse(t *testing.T) {
107
109
assert .True (mt , ok , "expected mongo.BulkWriteException, got %T" , err )
108
110
assert .True (mt , we .HasErrorLabel (label ), "expected error to have label: %v" , label )
109
111
})
112
+
110
113
}
111
114
112
115
func TestHintErrors (t * testing.T ) {
@@ -174,3 +177,51 @@ func TestHintWithUnacknowledgedWriteErrors(t *testing.T) {
174
177
assert .Equal (mt , got , expected , "expected: %v got: %v" , expected , got )
175
178
})
176
179
}
180
+
181
+ func TestAggregateSecondaryPreferredReadPreference (t * testing.T ) {
182
+ // Use secondaryPreferred instead of secondary because sharded clusters started up by mongo-orchestration have
183
+ // one-node shards, so a secondary read preference is not satisfiable.
184
+ secondaryPrefClientOpts := options .Client ().
185
+ SetWriteConcern (mtest .MajorityWc ).
186
+ SetReadPreference (readpref .SecondaryPreferred ()).
187
+ SetReadConcern (mtest .MajorityRc )
188
+ mtOpts := mtest .NewOptions ().
189
+ ClientOptions (secondaryPrefClientOpts ).
190
+ MinServerVersion ("4.1.0" ) // Consistent with tests in aggregate-out-readConcern.json
191
+
192
+ mt := mtest .New (t , mtOpts )
193
+ mt .Run ("aggregate $out with read preference secondary" , func (mt * mtest.T ) {
194
+ doc , err := bson .Marshal (bson.D {
195
+ {"_id" , 1 },
196
+ {"x" , 11 },
197
+ })
198
+ assert .Nil (mt , err , "Marshal error: %v" , err )
199
+ _ , err = mt .Coll .InsertOne (mtest .Background , doc )
200
+ assert .Nil (mt , err , "InsertOne error: %v" , err )
201
+
202
+ mt .ClearEvents ()
203
+ outputCollName := "aggregate-read-pref-secondary-output"
204
+ outStage := bson.D {
205
+ {"$out" , outputCollName },
206
+ }
207
+ cursor , err := mt .Coll .Aggregate (mtest .Background , mongo.Pipeline {outStage })
208
+ assert .Nil (mt , err , "Aggregate error: %v" , err )
209
+ _ = cursor .Close (mtest .Background )
210
+
211
+ // Assert that the output collection contains the document we expect.
212
+ outputColl := mt .CreateCollection (mtest.Collection {Name : outputCollName }, false )
213
+ cursor , err = outputColl .Find (mtest .Background , bson.D {})
214
+ assert .Nil (mt , err , "Find error: %v" , err )
215
+ defer cursor .Close (mtest .Background )
216
+
217
+ assert .True (mt , cursor .Next (mtest .Background ), "expected Next to return true, got false" )
218
+ assert .True (mt , bytes .Equal (doc , cursor .Current ), "expected document %s, got %s" , bson .Raw (doc ), cursor .Current )
219
+ assert .False (mt , cursor .Next (mtest .Background ), "unexpected document returned by Find: %s" , cursor .Current )
220
+
221
+ // Assert that no read preference was sent to the server.
222
+ evt := mt .GetStartedEvent ()
223
+ assert .Equal (mt , "aggregate" , evt .CommandName , "expected command 'aggregate', got '%s'" , evt .CommandName )
224
+ _ , err = evt .Command .LookupErr ("$readPreference" )
225
+ assert .NotNil (mt , err , "expected command %s to not contain $readPreference" , evt .Command )
226
+ })
227
+ }
0 commit comments