Skip to content

Commit a2de2b1

Browse files
committed
GODRIVER-234 Specify write concern in listDatabases test to fix failures on sharded clusters
Change-Id: I14173c603a9bd7a04ed64b266d3108ffaa945851
1 parent 78d9b29 commit a2de2b1

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

mongo/internal/testutil/ops.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/mongodb/mongo-go-driver/mongo/private/conn"
1818
"github.com/mongodb/mongo-go-driver/mongo/private/msg"
1919
"github.com/mongodb/mongo-go-driver/mongo/readpref"
20+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
2021
"github.com/stretchr/testify/require"
2122
)
2223

@@ -102,12 +103,12 @@ func autoDropDB(t *testing.T, clstr *cluster.Cluster) {
102103
}
103104

104105
// AutoInsertDocs inserts the docs into the test cluster.
105-
func AutoInsertDocs(t *testing.T, docs ...*bson.Document) {
106-
InsertDocs(t, DBName(t), ColName(t), docs...)
106+
func AutoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...*bson.Document) {
107+
InsertDocs(t, DBName(t), ColName(t), writeConcern, docs...)
107108
}
108109

109110
// InsertDocs inserts the docs into the test cluster.
110-
func InsertDocs(t *testing.T, dbname, colname string, docs ...*bson.Document) {
111+
func InsertDocs(t *testing.T, dbname, colname string, writeConcern *writeconcern.WriteConcern, docs ...*bson.Document) {
111112
arrDocs := make([]*bson.Value, 0, len(docs))
112113
for _, doc := range docs {
113114
arrDocs = append(arrDocs, bson.AC.Document(doc))
@@ -116,6 +117,13 @@ func InsertDocs(t *testing.T, dbname, colname string, docs ...*bson.Document) {
116117
bson.C.String("insert", colname),
117118
bson.C.ArrayFromElements("documents", arrDocs...))
118119

120+
if writeConcern != nil {
121+
wc, err := writeConcern.MarshalBSONElement()
122+
require.NoError(t, err)
123+
124+
insertCommand.Append(wc)
125+
}
126+
119127
request := msg.NewCommand(
120128
msg.NextRequestID(),
121129
dbname,

mongo/private/ops/aggregate_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestAggregateWithMultipleBatches(t *testing.T) {
4444
bson.NewDocument(bson.C.Int32("_id", 4)),
4545
bson.NewDocument(bson.C.Int32("_id", 5)),
4646
}
47-
testutil.AutoInsertDocs(t, documents...)
47+
testutil.AutoInsertDocs(t, nil, documents...)
4848

4949
readers := make([]bson.Reader, 0, len(documents))
5050
for _, doc := range documents {
@@ -100,6 +100,7 @@ func TestAggregateWithAllowDiskUse(t *testing.T) {
100100
t.Parallel()
101101
testutil.Integration(t)
102102
testutil.AutoInsertDocs(t,
103+
nil,
103104
bson.NewDocument(bson.C.Int32("_id", 1)),
104105
bson.NewDocument(bson.C.Int32("_id", 2)),
105106
)
@@ -161,7 +162,7 @@ func TestLegacyAggregateWithMultipleBatches(t *testing.T) {
161162
bson.NewDocument(bson.C.Int32("_id", 4)),
162163
bson.NewDocument(bson.C.Int32("_id", 5)),
163164
}
164-
testutil.AutoInsertDocs(t, documents...)
165+
testutil.AutoInsertDocs(t, nil, documents...)
165166

166167
readers := make([]bson.Reader, 0, len(documents))
167168
for _, doc := range documents {
@@ -225,6 +226,7 @@ func TestLegacyAggregateWithAllowDiskUse(t *testing.T) {
225226
t.Parallel()
226227
testutil.Integration(t)
227228
testutil.AutoInsertDocs(t,
229+
nil,
228230
bson.NewDocument(bson.C.Int32("_id", 1)),
229231
bson.NewDocument(bson.C.Int32("_id", 2)),
230232
)

mongo/private/ops/cursor_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func TestCursorSingleBatch(t *testing.T) {
5656
bson.NewDocument(bson.C.Int32("_id", 1)),
5757
bson.NewDocument(bson.C.Int32("_id", 2)),
5858
}
59-
testutil.AutoInsertDocs(t, documents...)
59+
testutil.AutoInsertDocs(t, nil, documents...)
6060

6161
readers := make([]bson.Reader, 0, len(documents))
6262
for _, doc := range documents {
@@ -98,7 +98,7 @@ func TestCursorMultipleBatches(t *testing.T) {
9898
bson.NewDocument(bson.C.Int32("_id", 4)),
9999
bson.NewDocument(bson.C.Int32("_id", 5)),
100100
}
101-
testutil.AutoInsertDocs(t, documents...)
101+
testutil.AutoInsertDocs(t, nil, documents...)
102102

103103
readers := make([]bson.Reader, 0, len(documents))
104104
for _, doc := range documents {
@@ -159,7 +159,7 @@ func TestCursorClose(t *testing.T) {
159159
bson.NewDocument(bson.C.Int32("_id", 4)),
160160
bson.NewDocument(bson.C.Int32("_id", 5)),
161161
}
162-
testutil.AutoInsertDocs(t, documents...)
162+
testutil.AutoInsertDocs(t, nil, documents...)
163163

164164
s := getServer(t)
165165
cursorResult := find(t, s, 2)
@@ -177,6 +177,7 @@ func TestCursorError(t *testing.T) {
177177
testutil.Integration(t)
178178
testutil.AutoDropCollection(t)
179179
testutil.AutoInsertDocs(t,
180+
nil,
180181
bson.NewDocument(bson.C.Int32("_id", 1)),
181182
bson.NewDocument(bson.C.Int32("_id", 2)),
182183
bson.NewDocument(bson.C.Int32("_id", 3)),

mongo/private/ops/list_collections_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ func TestListCollections(t *testing.T) {
3737
testutil.DropCollection(t, dbname, collectionNameOne)
3838
testutil.DropCollection(t, dbname, collectionNameTwo)
3939
testutil.DropCollection(t, dbname, collectionNameThree)
40-
testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1)))
41-
testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1)))
42-
testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1)))
40+
testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
41+
testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
42+
testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
4343

4444
s := getServer(t)
4545
cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{})
@@ -74,9 +74,9 @@ func TestListCollectionsMultipleBatches(t *testing.T) {
7474
testutil.DropCollection(t, dbname, collectionNameOne)
7575
testutil.DropCollection(t, dbname, collectionNameTwo)
7676
testutil.DropCollection(t, dbname, collectionNameThree)
77-
testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1)))
78-
testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1)))
79-
testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1)))
77+
testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
78+
testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
79+
testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
8080

8181
s := getServer(t)
8282
cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{

mongo/private/ops/list_databases_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ import (
1414
"github.com/mongodb/mongo-go-driver/bson"
1515
"github.com/mongodb/mongo-go-driver/mongo/internal/testutil"
1616
. "github.com/mongodb/mongo-go-driver/mongo/private/ops"
17+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
1718
"github.com/stretchr/testify/require"
1819
)
1920

2021
func TestListDatabases(t *testing.T) {
2122
t.Parallel()
2223
testutil.Integration(t)
2324
testutil.AutoDropCollection(t)
24-
testutil.AutoInsertDocs(t, bson.NewDocument(bson.C.Int32("_id", 1)))
25+
testutil.AutoInsertDocs(t, writeconcern.New(writeconcern.WMajority()), bson.NewDocument(bson.C.Int32("_id", 1)))
2526

2627
s := getServer(t)
2728
cursor, err := ListDatabases(context.Background(), s, ListDatabasesOptions{})

mongo/writeconcern/writeconcern.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func W(w int) Option {
5454

5555
// WMajority requests acknowledgement that write operations propagate to the majority of mongod
5656
// instances.
57-
func WMajority(w int) Option {
57+
func WMajority() Option {
5858
return func(concern *WriteConcern) {
5959
concern.w = "majority"
6060
}

0 commit comments

Comments
 (0)