Skip to content

Commit

Permalink
Merge branch 'pr/87'
Browse files Browse the repository at this point in the history
GODRIVER-538

Change-Id: I5ebe2e74b6f86a19e9d5ee05ee2d97bc0356fd85
  • Loading branch information
skriptble committed Sep 6, 2018
2 parents 3496960 + 5bde9d0 commit 771435c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
9 changes: 5 additions & 4 deletions core/readpref/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 8 additions & 8 deletions core/readpref/readpref_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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())
}

0 comments on commit 771435c

Please sign in to comment.