Skip to content

Commit 771435c

Browse files
committed
Merge branch 'pr/87'
GODRIVER-538 Change-Id: I5ebe2e74b6f86a19e9d5ee05ee2d97bc0356fd85
2 parents 3496960 + 5bde9d0 commit 771435c

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

core/readpref/options.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ func WithMaxStaleness(ms time.Duration) Option {
3434
// overrides all previous calls to either method.
3535
func WithTags(tags ...string) Option {
3636
return func(rp *ReadPref) error {
37-
if len(tags)%2 != 0 {
37+
length := len(tags)
38+
if length < 2 || length % 2 != 0 {
3839
return ErrInvalidTagSet
3940
}
4041

41-
tagset := make(tag.Set, 0)
42+
tagset := make(tag.Set, 0, length/2)
4243

43-
for i := 0; i < len(tags)/2; i++ {
44-
tagset = append(tagset, tag.Tag{Name: tags[i], Value: tags[i+1]})
44+
for i := 1; i < length; i+=2 {
45+
tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]})
4546
}
4647

4748
return WithTagSets(tagset)(rp)

core/readpref/readpref_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func TestPrimaryPreferred_with_options(t *testing.T) {
3939
require := require.New(t)
4040
subject := PrimaryPreferred(
4141
WithMaxStaleness(time.Duration(10)),
42-
WithTags("a", "1"),
42+
WithTags("a", "1", "b", "2"),
4343
)
4444

4545
require.Equal(PrimaryPreferredMode, subject.Mode())
4646
ms, set := subject.MaxStaleness()
4747
require.True(set)
4848
require.Equal(time.Duration(10), ms)
49-
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets())
49+
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets())
5050
}
5151

5252
func TestSecondaryPreferred(t *testing.T) {
@@ -63,14 +63,14 @@ func TestSecondaryPreferred_with_options(t *testing.T) {
6363
require := require.New(t)
6464
subject := SecondaryPreferred(
6565
WithMaxStaleness(time.Duration(10)),
66-
WithTags("a", "1"),
66+
WithTags("a", "1", "b", "2"),
6767
)
6868

6969
require.Equal(SecondaryPreferredMode, subject.Mode())
7070
ms, set := subject.MaxStaleness()
7171
require.True(set)
7272
require.Equal(time.Duration(10), ms)
73-
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets())
73+
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets())
7474
}
7575

7676
func TestSecondary(t *testing.T) {
@@ -87,14 +87,14 @@ func TestSecondary_with_options(t *testing.T) {
8787
require := require.New(t)
8888
subject := Secondary(
8989
WithMaxStaleness(time.Duration(10)),
90-
WithTags("a", "1"),
90+
WithTags("a", "1", "b", "2"),
9191
)
9292

9393
require.Equal(SecondaryMode, subject.Mode())
9494
ms, set := subject.MaxStaleness()
9595
require.True(set)
9696
require.Equal(time.Duration(10), ms)
97-
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets())
97+
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets())
9898
}
9999

100100
func TestNearest(t *testing.T) {
@@ -111,12 +111,12 @@ func TestNearest_with_options(t *testing.T) {
111111
require := require.New(t)
112112
subject := Nearest(
113113
WithMaxStaleness(time.Duration(10)),
114-
WithTags("a", "1"),
114+
WithTags("a", "1", "b", "2"),
115115
)
116116

117117
require.Equal(NearestMode, subject.Mode())
118118
ms, set := subject.MaxStaleness()
119119
require.True(set)
120120
require.Equal(time.Duration(10), ms)
121-
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets())
121+
require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets())
122122
}

0 commit comments

Comments
 (0)