diff --git a/core/readpref/options.go b/core/readpref/options.go index e428ff76ee..e7b8e9789a 100644 --- a/core/readpref/options.go +++ b/core/readpref/options.go @@ -34,14 +34,15 @@ func WithMaxStaleness(ms time.Duration) Option { // overrides all previous calls to either method. func WithTags(tags ...string) Option { return func(rp *ReadPref) error { - if len(tags)%2 != 0 { + length := len(tags) + if length < 2 || length % 2 != 0 { return ErrInvalidTagSet } - tagset := make(tag.Set, 0) + tagset := make(tag.Set, 0, length/2) - for i := 0; i < len(tags)/2; i++ { - tagset = append(tagset, tag.Tag{Name: tags[i], Value: tags[i+1]}) + for i := 1; i < length; i+=2 { + tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]}) } return WithTagSets(tagset)(rp) diff --git a/core/readpref/readpref_test.go b/core/readpref/readpref_test.go index c62be25661..c9257f10ae 100644 --- a/core/readpref/readpref_test.go +++ b/core/readpref/readpref_test.go @@ -39,14 +39,14 @@ func TestPrimaryPreferred_with_options(t *testing.T) { require := require.New(t) subject := PrimaryPreferred( WithMaxStaleness(time.Duration(10)), - WithTags("a", "1"), + WithTags("a", "1", "b", "2"), ) require.Equal(PrimaryPreferredMode, subject.Mode()) ms, set := subject.MaxStaleness() require.True(set) require.Equal(time.Duration(10), ms) - require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets()) + require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets()) } func TestSecondaryPreferred(t *testing.T) { @@ -63,14 +63,14 @@ func TestSecondaryPreferred_with_options(t *testing.T) { require := require.New(t) subject := SecondaryPreferred( WithMaxStaleness(time.Duration(10)), - WithTags("a", "1"), + WithTags("a", "1", "b", "2"), ) require.Equal(SecondaryPreferredMode, subject.Mode()) ms, set := subject.MaxStaleness() require.True(set) require.Equal(time.Duration(10), ms) - require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets()) + require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets()) } func TestSecondary(t *testing.T) { @@ -87,14 +87,14 @@ func TestSecondary_with_options(t *testing.T) { require := require.New(t) subject := Secondary( WithMaxStaleness(time.Duration(10)), - WithTags("a", "1"), + WithTags("a", "1", "b", "2"), ) require.Equal(SecondaryMode, subject.Mode()) ms, set := subject.MaxStaleness() require.True(set) require.Equal(time.Duration(10), ms) - require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets()) + require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets()) } func TestNearest(t *testing.T) { @@ -111,12 +111,12 @@ func TestNearest_with_options(t *testing.T) { require := require.New(t) subject := Nearest( WithMaxStaleness(time.Duration(10)), - WithTags("a", "1"), + WithTags("a", "1", "b", "2"), ) require.Equal(NearestMode, subject.Mode()) ms, set := subject.MaxStaleness() require.True(set) require.Equal(time.Duration(10), ms) - require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"}}}, subject.TagSets()) + require.Equal([]tag.Set{{tag.Tag{Name: "a", Value: "1"},tag.Tag{Name: "b", Value: "2"}}}, subject.TagSets()) }