summaryrefslogtreecommitdiff
path: root/common/util/write_concern_test.go
blob: ec9a8e7861270bd347e576f8f016c604a6e24355 (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
package util

import (
	. "github.com/smartystreets/goconvey/convey"
	"testing"
)

func TestBuildWriteConcern(t *testing.T) {
	Convey("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...", t, func() {
		Convey("no error should be returned if the write concern is valid", func() {
			writeConcern, err := BuildWriteConcern(`{w:34}`, true)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 34)
			writeConcern, err = BuildWriteConcern(`{w:"majority"}`, true)
			So(err, ShouldBeNil)
			So(writeConcern.WMode, ShouldEqual, "majority")
			writeConcern, err = BuildWriteConcern(`majority`, true)
			So(err, ShouldBeNil)
			So(writeConcern.WMode, ShouldEqual, "majority")
			writeConcern, err = BuildWriteConcern(`tagset`, true)
			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}`, false)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 1)
			writeConcern, err = BuildWriteConcern(`{w:"majority"}`, false)
			So(err, ShouldBeNil)
			So(writeConcern.W, ShouldEqual, 1)
			writeConcern, err = BuildWriteConcern(`tagset`, false)
			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}`, false)
			So(err, ShouldBeNil)
			So(writeConcern, ShouldBeNil)
		})
		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}`, false)
			So(err, ShouldBeNil)
			So(writeConcern.J, ShouldBeTrue)
		})
	})
}

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)
		})
	})
}