From c596cc5e83400d59c65b356a0f9d2090d97c8833 Mon Sep 17 00:00:00 2001 From: Stephan Lanfermann Date: Fri, 31 Aug 2018 21:08:00 +0200 Subject: [PATCH 1/3] fixed tests to display broken WithTags behavior --- core/readpref/readpref_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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()) } From beaed0b2bad8a7fd1bf76b5a24121aa74701c20c Mon Sep 17 00:00:00 2001 From: Stephan Lanfermann Date: Fri, 31 Aug 2018 21:20:23 +0200 Subject: [PATCH 2/3] Fixed with tags behavior - Made sure there are at least two arguments (because 0 % 2 == 0 so that check did not suffice) - Set tagset capacity to correct size because that is exactly how long it must be when everything goes right - Completely restructured for-loop to do what it was intended to do --- core/readpref/options.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/core/readpref/options.go b/core/readpref/options.go index e428ff76ee..148c481675 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 { + l := len(tags) + if l < 2 || l % 2 != 0 { return ErrInvalidTagSet } - tagset := make(tag.Set, 0) + tagset := make(tag.Set, 0, l/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 < l; i+=2 { + tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]}) } return WithTagSets(tagset)(rp) From 5bde9d00b4aa4ca5ef0325289a8df1be22cde912 Mon Sep 17 00:00:00 2001 From: Stephan Lanfermann Date: Thu, 6 Sep 2018 17:40:20 +0200 Subject: [PATCH 3/3] Renamed l variable to length to avoid confusion with number 1 --- core/readpref/options.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/readpref/options.go b/core/readpref/options.go index 148c481675..e7b8e9789a 100644 --- a/core/readpref/options.go +++ b/core/readpref/options.go @@ -34,14 +34,14 @@ func WithMaxStaleness(ms time.Duration) Option { // overrides all previous calls to either method. func WithTags(tags ...string) Option { return func(rp *ReadPref) error { - l := len(tags) - if l < 2 || l % 2 != 0 { + length := len(tags) + if length < 2 || length % 2 != 0 { return ErrInvalidTagSet } - tagset := make(tag.Set, 0, l/2) + tagset := make(tag.Set, 0, length/2) - for i := 1; i < l; i+=2 { + for i := 1; i < length; i+=2 { tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]}) }