Skip to content

Commit

Permalink
chore: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnabass committed Dec 26, 2024
1 parent 72469ab commit ba8c156
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions consistent/builder_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package consistent

import (
"hash/fnv"
"testing"

"github.com/stretchr/testify/suite"
"github.com/xmidt-org/medley"
)

type BuilderSuite struct {
suite.Suite

object []byte
}

func (suite *BuilderSuite) SetupTest() {
suite.object = []byte("test value")
}

func (suite *BuilderSuite) testStringsDefault() {
services := []string{"service1", "service2", "service3"}
ring := Strings(services...).Build()
suite.Require().NotNil(ring)

result, err := ring.Find(suite.object)
suite.NoError(err)
suite.Contains(services, result)
}

func (suite *BuilderSuite) testStringsCustom() {
services := []string{"service1", "service2", "service3", "additional1", "additional2"}
ring := Strings(services[:3]...).
VNodes(100).
Services(services[3:]...).
Algorithm(medley.Algorithm{New64: fnv.New64}).
ServiceHasher(nil). // force the default
Build()

suite.Require().NotNil(ring)

result, err := ring.Find(suite.object)
suite.NoError(err)
suite.Contains(services, result)
}

func (suite *BuilderSuite) TestStrings() {
suite.Run("Default", suite.testStringsDefault)
suite.Run("Custom", suite.testStringsCustom)
}

func (suite *BuilderSuite) TestServices() {
services := []string{"service1", "service2", "service3"}
ring := Services(services...).Build()
suite.Require().NotNil(ring)

result, err := ring.Find(suite.object)
suite.NoError(err)
suite.Contains(services, result)
}

func (suite *BuilderSuite) TestBasicServices() {
services := []medley.BasicService{
{Host: "service1.net"},
{Host: "service2.net", Port: 8080},
{Host: "service3.net", Path: "/foo/bar"},
}

ring := BasicServices(services...).Build()
suite.Require().NotNil(ring)

result, err := ring.Find(suite.object)
suite.NoError(err)
suite.Contains(services, result)
}

func TestBuilder(t *testing.T) {
suite.Run(t, new(BuilderSuite))
}

0 comments on commit ba8c156

Please sign in to comment.