Skip to content

Commit 733dc8b

Browse files
committed
Ensure default write concern is not appended
GODRIVER-667 Change-Id: Ia289f0a894ca7ce97025bc33ba324b6f9e86efd7
1 parent a776a30 commit 733dc8b

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

mongo/read_write_concern_spec_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,14 @@ func runConnectionStringTest(t *testing.T, testName string, testCase *connection
144144
}
145145

146146
wcBSON, err := wc.MarshalBSONElement()
147+
if err == writeconcern.ErrEmptyWriteConcern {
148+
if len(testCase.WriteConcern) == 0 {
149+
return
150+
}
151+
if _, exists := testCase.WriteConcern["journal"]; exists && len(testCase.WriteConcern) == 1 {
152+
return
153+
}
154+
}
147155
require.NoError(t, err)
148156

149157
wcDoc := wcBSON.Value.Document()
@@ -227,6 +235,14 @@ func runDocumentTest(t *testing.T, testName string, testCase *documentTest) {
227235
return
228236
}
229237

238+
if err == writeconcern.ErrEmptyWriteConcern {
239+
if len(testCase.WriteConcernDocument) == 0 {
240+
return
241+
}
242+
if _, exists := testCase.WriteConcernDocument["j"]; exists && len(testCase.WriteConcernDocument) == 1 {
243+
return
244+
}
245+
}
230246
require.NoError(t, err)
231247

232248
wcBytes, _ := wcDoc.Value.Document().MarshalBSON()

mongo/writeconcern/writeconcern.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ import (
1818
// ErrInconsistent indicates that an inconsistent write concern was specified.
1919
var ErrInconsistent = errors.New("a write concern cannot have both w=0 and j=true")
2020

21+
// ErrEmptyWriteConcern indicates that a write concern has no fields set.
22+
var ErrEmptyWriteConcern = errors.New("a write concern must have at least one field set")
23+
2124
// ErrNegativeW indicates that a negative integer `w` field was specified.
2225
var ErrNegativeW = errors.New("write concern `w` field cannot be a negative number")
2326

@@ -118,6 +121,9 @@ func (wc *WriteConcern) MarshalBSONElement() (bsonx.Elem, error) {
118121
elems = append(elems, bsonx.Elem{"wtimeout", bsonx.Int64(int64(wc.wTimeout / time.Millisecond))})
119122
}
120123

124+
if len(elems) == 0 {
125+
return bsonx.Elem{}, ErrEmptyWriteConcern
126+
}
121127
return bsonx.Elem{"writeConcern", bsonx.Document(elems)}, nil
122128
}
123129

x/network/command/command.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"context"
1313

1414
"fmt"
15+
1516
"github.com/mongodb/mongo-go-driver/bson"
1617
"github.com/mongodb/mongo-go-driver/bson/bsontype"
1718
"github.com/mongodb/mongo-go-driver/bson/primitive"
@@ -299,6 +300,9 @@ func addWriteConcern(cmd bsonx.Doc, wc *writeconcern.WriteConcern) (bsonx.Doc, e
299300

300301
element, err := wc.MarshalBSONElement()
301302
if err != nil {
303+
if err == writeconcern.ErrEmptyWriteConcern {
304+
return cmd, nil
305+
}
302306
return cmd, err
303307
}
304308

x/network/command/write_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package command
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/mongodb/mongo-go-driver/mongo/writeconcern"
8+
"github.com/mongodb/mongo-go-driver/x/bsonx"
9+
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
10+
"github.com/mongodb/mongo-go-driver/x/network/description"
11+
"github.com/mongodb/mongo-go-driver/x/network/wiremessage"
12+
)
13+
14+
func TestWrite(t *testing.T) {
15+
t.Run("Encode", func(t *testing.T) {
16+
t.Run("should not encode empty write concern", func(t *testing.T) {
17+
cmd := bsonx.Doc{{"fakeCommand", bsonx.Int32(1)}}
18+
want, err := append(cmd, bsonx.Elem{"$db", bsonx.String("foobar")}).MarshalBSON()
19+
noerr(t, err)
20+
w := Write{
21+
DB: "foobar",
22+
Command: cmd,
23+
WriteConcern: writeconcern.New(),
24+
}
25+
wm, err := w.Encode(description.SelectedServer{
26+
Server: description.Server{
27+
WireVersion: &description.VersionRange{Min: 0, Max: wiremessage.OpmsgWireVersion},
28+
},
29+
})
30+
noerr(t, err)
31+
msg, ok := wm.(wiremessage.Msg)
32+
if !ok {
33+
t.Errorf("Expected an OP_MSG wire message, but got something else. got %v", wm)
34+
}
35+
got := msg.Sections[0].(wiremessage.SectionBody).Document
36+
if !bytes.Equal(got, want) {
37+
t.Errorf("Command documents do not match. got %v; want %v", bsoncore.Document(got), bsoncore.Document(want))
38+
}
39+
})
40+
})
41+
}

0 commit comments

Comments
 (0)