summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/common/db/write_concern_test.go
blob: 31a5baf1bfb8879397f5a02fb5b3249412f53c6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package db

import (
	"github.com/mongodb/mongo-tools/common/connstring"
	. "github.com/smartystreets/goconvey/convey"

	"fmt"
	"testing"
	"time"
)

func TestBuildWriteConcern(t *testing.T) {
	Convey("When building write concern object", t, func() {
		Convey("and given a write concern string value, and a boolean indicating if the "+
			"write concern is to be used on a replica set, on calling BuildWriteConcern...", func() {
			Convey("no error should be returned if the write concern is valid", func() {
				writeConcern, err := BuildWriteConcern(`{w:34}`, ReplSet, nil)
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 34)
				writeConcern, err = BuildWriteConcern(`{w:"majority"}`, ReplSet, nil)
				So(err, ShouldBeNil)
				So(writeConcern.WMode, ShouldEqual, "majority")
				writeConcern, err = BuildWriteConcern(`majority`, ReplSet, nil)
				So(err, ShouldBeNil)
				So(writeConcern.WMode, ShouldEqual, "majority")
				writeConcern, err = BuildWriteConcern(`tagset`, ReplSet, nil)
				So(err, ShouldBeNil)
				So(writeConcern.WMode, ShouldEqual, "tagset")
			})
			Convey("on replica sets, only a write concern of 1 or 0 should be returned", func() {
				writeConcern, err := BuildWriteConcern(`{w:34}`, Standalone, nil)
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
				writeConcern, err = BuildWriteConcern(`{w:"majority"}`, Standalone, nil)
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
				writeConcern, err = BuildWriteConcern(`tagset`, Standalone, nil)
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
			})
			Convey("with a w value of 0, without j set, a nil write concern should be returned", func() {
				writeConcern, err := BuildWriteConcern(`{w:0}`, Standalone, nil)
				So(err, ShouldBeNil)
				So(writeConcern, ShouldBeNil)
			})
			Convey("with a negative w value, an error should be returned", func() {
				_, err := BuildWriteConcern(`{w:-1}`, ReplSet, nil)
				So(err, ShouldNotBeNil)
				_, err = BuildWriteConcern(`{w:-2}`, ReplSet, nil)
				So(err, ShouldNotBeNil)
			})
			Convey("with a w value of 0, with j set, a non-nil write concern should be returned", func() {
				writeConcern, err := BuildWriteConcern(`{w:0, j:true}`, Standalone, nil)
				So(err, ShouldBeNil)
				So(writeConcern.J, ShouldBeTrue)
			})
			// Regression test for TOOLS-1741
			Convey("When passing an empty writeConcern and empty URI"+
				"then write concern should default to being majority", func() {
				writeConcern, err := BuildWriteConcern("", ReplSet, nil)
				So(err, ShouldBeNil)
				So(writeConcern.WMode, ShouldEqual, "majority")
			})
		})
		Convey("and given a connection string", func() {
			Convey("with a w value of 0, without j set, a nil write concern should be returned", func() {
				writeConcern, err := BuildWriteConcern(``, Standalone, &connstring.ConnString{W: "0"})
				So(err, ShouldBeNil)
				So(writeConcern, ShouldBeNil)
			})
			Convey("with a negative w value, an error should be returned", func() {
				_, err := BuildWriteConcern(``, ReplSet, &connstring.ConnString{W: "-1"})
				So(err, ShouldNotBeNil)
				_, err = BuildWriteConcern(``, ReplSet, &connstring.ConnString{W: "-2"})
				So(err, ShouldNotBeNil)
			})
			Convey("on replica sets, only a write concern of 1 or 0 should be returned", func() {
				writeConcern, err := BuildWriteConcern(``, Standalone, &connstring.ConnString{W: "34"})
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
				writeConcern, err = BuildWriteConcern(``, Standalone, &connstring.ConnString{W: "majority"})
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
				writeConcern, err = BuildWriteConcern(``, Standalone, &connstring.ConnString{W: "tagset"})
				So(err, ShouldBeNil)
				So(writeConcern.W, ShouldEqual, 1)
			})
		})
		Convey("and given both, should error", func() {
			_, err := BuildWriteConcern(`ab`, ReplSet, &connstring.ConnString{W: "-1"})
			So(err, ShouldNotBeNil)
		})
	})
}

func TestConstructWCObject(t *testing.T) {
	Convey("Given a write concern string value, on calling constructWCObject...", t, func() {

		Convey("non-JSON string values should be assigned to the 'WMode' "+
			"field in their entirety", func() {
			writeConcernString := "majority"
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.WMode, ShouldEqual, writeConcernString)
		})

		Convey("non-JSON int values should be assigned to the 'w' field "+
			"in their entirety", func() {
			writeConcernString := `{w: 4}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 4)
		})

		Convey("JSON strings with valid j, wtimeout, fsync and w, should be "+
			"assigned accordingly", func() {
			writeConcernString := `{w: 3, j: true, fsync: false, wtimeout: 43}`
			expectedW := 3
			expectedWTimeout := 43
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, expectedW)
			So(writeConcern.J, ShouldBeTrue)
			So(writeConcern.FSync, ShouldBeFalse)
			So(writeConcern.WTimeout, ShouldEqual, expectedWTimeout)
		})

		Convey("JSON strings with an argument for j that is not false should set j true", func() {
			writeConcernString := `{w: 3, j: "rue"}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 3)
			So(writeConcern.J, ShouldBeTrue)
		})

		Convey("JSON strings with an argument for fsync that is not false should set fsync true", func() {
			writeConcernString := `{w: 3, fsync: "rue"}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 3)
			So(writeConcern.FSync, ShouldBeTrue)
		})

		Convey("JSON strings with an invalid wtimeout argument should error out", func() {
			writeConcernString := `{w: 3, wtimeout: "rue"}`
			_, err := constructWCObject(writeConcernString)
			So(err, ShouldNotBeNil)
			writeConcernString = `{w: 3, wtimeout: "43"}`
			_, err = constructWCObject(writeConcernString)
			So(err, ShouldNotBeNil)
		})

		Convey("JSON strings with any non-false j argument should not error out", func() {
			writeConcernString := `{w: 3, j: "t"}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.J, ShouldBeTrue)
			writeConcernString = `{w: 3, j: "f"}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.J, ShouldBeTrue)
			writeConcernString = `{w: 3, j: false}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.J, ShouldBeFalse)
			writeConcernString = `{w: 3, j: 0}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.J, ShouldBeFalse)
		})

		Convey("JSON strings with a shorthand fsync argument should not error out", func() {
			writeConcernString := `{w: 3, fsync: "t"}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.FSync, ShouldBeTrue)
			writeConcernString = `{w: "3", fsync: "f"}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.FSync, ShouldBeTrue)
			writeConcernString = `{w: "3", fsync: false}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.FSync, ShouldBeFalse)
			writeConcernString = `{w: "3", fsync: 0}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern.FSync, ShouldBeFalse)
		})

		Convey("Unacknowledge write concern strings should return a nil object "+
			"if journaling is not required", func() {
			writeConcernString := `{w: 0}`
			writeConcern, err := constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern, ShouldBeNil)
			writeConcernString = `{w: 0}`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern, ShouldBeNil)
			writeConcernString = `0`
			writeConcern, err = constructWCObject(writeConcernString)
			So(err, ShouldBeNil)
			So(writeConcern, ShouldBeNil)
		})
	})
}

func TestConstructSafetyFromConnString(t *testing.T) {
	Convey("Given a parsed &connstring, on calling constructSafetyFromConnString...", t, func() {

		Convey("non string values should be assigned to the 'WMode' "+
			"field in their entirety", func() {
			writeConcernString := "majority"
			cs := &connstring.ConnString{
				W: writeConcernString,
			}
			writeConcern, err := constructSafetyFromConnString(cs)
			So(err, ShouldBeNil)
			So(writeConcern.WMode, ShouldEqual, writeConcernString)
		})

		Convey("Int values should be assigned to the 'w' field ", func() {
			cs := &connstring.ConnString{
				W: "4",
			}
			writeConcern, err := constructSafetyFromConnString(cs)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 4)
		})

		Convey("&connstrings with valid j, wtimeout, fsync and w, should be "+
			"assigned accordingly", func() {
			expectedW := 3
			expectedWTimeout := 43
			cs := &connstring.ConnString{
				W:        "3",
				Journal:  true,
				FSync:    false,
				WTimeout: time.Second * 43,
			}
			writeConcern, err := constructSafetyFromConnString(cs)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, expectedW)
			So(writeConcern.J, ShouldBeTrue)
			So(writeConcern.FSync, ShouldBeFalse)
			So(writeConcern.WTimeout, ShouldEqual, expectedWTimeout)
		})

		Convey("Unacknowledge write concern strings should return a nil object "+
			"if journaling is not required", func() {
			cs := &connstring.ConnString{
				W: "0",
			}
			writeConcern, err := constructSafetyFromConnString(cs)
			fmt.Println(writeConcern)
			So(err, ShouldBeNil)
			So(writeConcern, ShouldBeNil)
		})
	})
}