diff --git a/go.mod b/go.mod index 54183b9..cfcd433 100644 --- a/go.mod +++ b/go.mod @@ -25,6 +25,7 @@ require ( github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + github.com/stretchr/objx v0.5.0 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/tarantool/go-iproto v1.0.0 // indirect github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect diff --git a/go.sum b/go.sum index e60ebd7..4d7072b 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,7 @@ github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= diff --git a/vshard_test.go b/vshard_test.go index a8b526a..1c81935 100644 --- a/vshard_test.go +++ b/vshard_test.go @@ -2,19 +2,79 @@ package vshard_router_test import ( "context" + "fmt" "testing" + "time" "github.com/google/uuid" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - vshard_router "github.com/KaymeKaydex/go-vshard-router" + vshardrouter "github.com/KaymeKaydex/go-vshard-router" "github.com/KaymeKaydex/go-vshard-router/providers/static" ) +var validRS = map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{ + UUID: uuid.New(), + Name: "test", + }: { + { + Addr: "127.0.0.1:3301", + UUID: uuid.New(), + }, + }, +} + +type MockRouter struct { + mock.Mock +} + +func (m *MockRouter) DiscoveryAllBuckets(ctx context.Context) error { + args := m.Called(ctx) + return args.Error(0) +} + +func TestNewRouter(t *testing.T) { + ctx := context.Background() + + // Создаем мок для Router + mockRouter := new(MockRouter) + mockRouter.On("DiscoveryAllBuckets", mock.Anything).Return(nil) + + router, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{ + DiscoveryTimeout: time.Second, + TopologyProvider: static.NewProvider(validRS), + TotalBucketCount: 128000, + }) + require.NoError(t, err) + require.NotNil(t, router) +} + +func TestRouter_RouterBucketCount(t *testing.T) { + ctx := context.Background() + + // Создаем мок для Router + mockRouter := new(MockRouter) + mockRouter.On("DiscoveryAllBuckets", mock.Anything).Return(nil) + + for i := uint64(1); i < 500; i += 50 { + t.Run(fmt.Sprintf("%d", i), func(t *testing.T) { + router, _ := vshardrouter.NewRouter(ctx, vshardrouter.Config{ + DiscoveryTimeout: time.Second, + TopologyProvider: static.NewProvider(validRS), + TotalBucketCount: i, + }) + + require.Equal(t, i, router.RouterBucketCount()) + }) + } +} + func TestNewRouter_EmptyReplicasets(t *testing.T) { ctx := context.TODO() - router, err := vshard_router.NewRouter(ctx, vshard_router.Config{}) + router, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{}) require.Error(t, err) require.Nil(t, router) } @@ -22,9 +82,9 @@ func TestNewRouter_EmptyReplicasets(t *testing.T) { func TestNewRouter_InvalidReplicasetUUID(t *testing.T) { ctx := context.TODO() - router, err := vshard_router.NewRouter(ctx, vshard_router.Config{ - TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{ - vshard_router.ReplicasetInfo{ + router, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{ + TopologyProvider: static.NewProvider(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{ Name: "123", }: { {Addr: "first.internal:1212"}, @@ -39,9 +99,9 @@ func TestNewRouter_InvalidReplicasetUUID(t *testing.T) { func TestNewRouter_InstanceAddr(t *testing.T) { ctx := context.TODO() - router, err := vshard_router.NewRouter(ctx, vshard_router.Config{ - TopologyProvider: static.NewProvider(map[vshard_router.ReplicasetInfo][]vshard_router.InstanceInfo{ - vshard_router.ReplicasetInfo{ + router, err := vshardrouter.NewRouter(ctx, vshardrouter.Config{ + TopologyProvider: static.NewProvider(map[vshardrouter.ReplicasetInfo][]vshardrouter.InstanceInfo{ + vshardrouter.ReplicasetInfo{ Name: "123", UUID: uuid.New(), }: { @@ -56,12 +116,12 @@ func TestNewRouter_InstanceAddr(t *testing.T) { func TestRouterBucketIDStrCRC32(t *testing.T) { // required values from tarantool example - require.Equal(t, uint64(103202), vshard_router.BucketIDStrCRC32("2707623829", uint64(256000))) - require.Equal(t, uint64(35415), vshard_router.BucketIDStrCRC32("2706201716", uint64(256000))) + require.Equal(t, uint64(103202), vshardrouter.BucketIDStrCRC32("2707623829", uint64(256000))) + require.Equal(t, uint64(35415), vshardrouter.BucketIDStrCRC32("2706201716", uint64(256000))) } func BenchmarkRouterBucketIDStrCRC32(b *testing.B) { for i := 0; i < b.N; i++ { - vshard_router.BucketIDStrCRC32("test_bench_key", uint64(256000)) + vshardrouter.BucketIDStrCRC32("test_bench_key", uint64(256000)) } }