Skip to content

Commit

Permalink
GODRIVER-234 Specify write concern in listDatabases test to fix failu…
Browse files Browse the repository at this point in the history
…res on sharded clusters

Change-Id: I14173c603a9bd7a04ed64b266d3108ffaa945851
  • Loading branch information
saghm committed Feb 13, 2018
1 parent 78d9b29 commit a2de2b1
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 16 deletions.
14 changes: 11 additions & 3 deletions mongo/internal/testutil/ops.go
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/mongodb/mongo-go-driver/mongo/private/conn"
"github.com/mongodb/mongo-go-driver/mongo/private/msg"
"github.com/mongodb/mongo-go-driver/mongo/readpref"
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
"github.com/stretchr/testify/require"
)

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

// AutoInsertDocs inserts the docs into the test cluster.
func AutoInsertDocs(t *testing.T, docs ...*bson.Document) {
InsertDocs(t, DBName(t), ColName(t), docs...)
func AutoInsertDocs(t *testing.T, writeConcern *writeconcern.WriteConcern, docs ...*bson.Document) {
InsertDocs(t, DBName(t), ColName(t), writeConcern, docs...)
}

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

if writeConcern != nil {
wc, err := writeConcern.MarshalBSONElement()
require.NoError(t, err)

insertCommand.Append(wc)
}

request := msg.NewCommand(
msg.NextRequestID(),
dbname,
Expand Down
6 changes: 4 additions & 2 deletions mongo/private/ops/aggregate_test.go
Expand Up @@ -44,7 +44,7 @@ func TestAggregateWithMultipleBatches(t *testing.T) {
bson.NewDocument(bson.C.Int32("_id", 4)),
bson.NewDocument(bson.C.Int32("_id", 5)),
}
testutil.AutoInsertDocs(t, documents...)
testutil.AutoInsertDocs(t, nil, documents...)

readers := make([]bson.Reader, 0, len(documents))
for _, doc := range documents {
Expand Down Expand Up @@ -100,6 +100,7 @@ func TestAggregateWithAllowDiskUse(t *testing.T) {
t.Parallel()
testutil.Integration(t)
testutil.AutoInsertDocs(t,
nil,
bson.NewDocument(bson.C.Int32("_id", 1)),
bson.NewDocument(bson.C.Int32("_id", 2)),
)
Expand Down Expand Up @@ -161,7 +162,7 @@ func TestLegacyAggregateWithMultipleBatches(t *testing.T) {
bson.NewDocument(bson.C.Int32("_id", 4)),
bson.NewDocument(bson.C.Int32("_id", 5)),
}
testutil.AutoInsertDocs(t, documents...)
testutil.AutoInsertDocs(t, nil, documents...)

readers := make([]bson.Reader, 0, len(documents))
for _, doc := range documents {
Expand Down Expand Up @@ -225,6 +226,7 @@ func TestLegacyAggregateWithAllowDiskUse(t *testing.T) {
t.Parallel()
testutil.Integration(t)
testutil.AutoInsertDocs(t,
nil,
bson.NewDocument(bson.C.Int32("_id", 1)),
bson.NewDocument(bson.C.Int32("_id", 2)),
)
Expand Down
7 changes: 4 additions & 3 deletions mongo/private/ops/cursor_test.go
Expand Up @@ -56,7 +56,7 @@ func TestCursorSingleBatch(t *testing.T) {
bson.NewDocument(bson.C.Int32("_id", 1)),
bson.NewDocument(bson.C.Int32("_id", 2)),
}
testutil.AutoInsertDocs(t, documents...)
testutil.AutoInsertDocs(t, nil, documents...)

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

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

s := getServer(t)
cursorResult := find(t, s, 2)
Expand All @@ -177,6 +177,7 @@ func TestCursorError(t *testing.T) {
testutil.Integration(t)
testutil.AutoDropCollection(t)
testutil.AutoInsertDocs(t,
nil,
bson.NewDocument(bson.C.Int32("_id", 1)),
bson.NewDocument(bson.C.Int32("_id", 2)),
bson.NewDocument(bson.C.Int32("_id", 3)),
Expand Down
12 changes: 6 additions & 6 deletions mongo/private/ops/list_collections_test.go
Expand Up @@ -37,9 +37,9 @@ func TestListCollections(t *testing.T) {
testutil.DropCollection(t, dbname, collectionNameOne)
testutil.DropCollection(t, dbname, collectionNameTwo)
testutil.DropCollection(t, dbname, collectionNameThree)
testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1)))

s := getServer(t)
cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{})
Expand Down Expand Up @@ -74,9 +74,9 @@ func TestListCollectionsMultipleBatches(t *testing.T) {
testutil.DropCollection(t, dbname, collectionNameOne)
testutil.DropCollection(t, dbname, collectionNameTwo)
testutil.DropCollection(t, dbname, collectionNameThree)
testutil.InsertDocs(t, dbname, collectionNameOne, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameTwo, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameThree, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameOne, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameTwo, nil, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.InsertDocs(t, dbname, collectionNameThree, nil, bson.NewDocument(bson.C.Int32("_id", 1)))

s := getServer(t)
cursor, err := ListCollections(context.Background(), s, dbname, ListCollectionsOptions{
Expand Down
3 changes: 2 additions & 1 deletion mongo/private/ops/list_databases_test.go
Expand Up @@ -14,14 +14,15 @@ import (
"github.com/mongodb/mongo-go-driver/bson"
"github.com/mongodb/mongo-go-driver/mongo/internal/testutil"
. "github.com/mongodb/mongo-go-driver/mongo/private/ops"
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
"github.com/stretchr/testify/require"
)

func TestListDatabases(t *testing.T) {
t.Parallel()
testutil.Integration(t)
testutil.AutoDropCollection(t)
testutil.AutoInsertDocs(t, bson.NewDocument(bson.C.Int32("_id", 1)))
testutil.AutoInsertDocs(t, writeconcern.New(writeconcern.WMajority()), bson.NewDocument(bson.C.Int32("_id", 1)))

s := getServer(t)
cursor, err := ListDatabases(context.Background(), s, ListDatabasesOptions{})
Expand Down
2 changes: 1 addition & 1 deletion mongo/writeconcern/writeconcern.go
Expand Up @@ -54,7 +54,7 @@ func W(w int) Option {

// WMajority requests acknowledgement that write operations propagate to the majority of mongod
// instances.
func WMajority(w int) Option {
func WMajority() Option {
return func(concern *WriteConcern) {
concern.w = "majority"
}
Expand Down

0 comments on commit a2de2b1

Please sign in to comment.