Passed
Pull Request — master (#567)
by Murat
02:23
created

hash.TestAddAndRemove   A

Complexity

Conditions 2

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 21
nop 1
dl 0
loc 31
rs 9.376
c 0
b 0
f 0
1
package hash
2
3
import (
4
	"github.com/Permify/permify/internal/config"
5
	"github.com/stretchr/testify/assert"
6
	"google.golang.org/grpc"
7
	"net"
8
	"testing"
9
)
10
11
func TestAddAndRemove(t *testing.T) {
12
	// Mock grpc server
13
	lis, err := net.Listen("tcp", "localhost:0")
14
	assert.NoError(t, err)
15
	grpcServer := grpc.NewServer()
16
	go func() {
17
		_ = grpcServer.Serve(lis)
18
	}()
19
	defer grpcServer.Stop()
20
21
	// Create ConsistentHash instance
22
	hashFunc := Hash
23
	c := NewConsistentHash(100, hashFunc, config.GRPC{})
24
25
	// Test Add
26
	node := lis.Addr().String()
27
	c.Add(node)
28
	_, conn, ok := c.Get("key")
29
	assert.True(t, ok)
30
	assert.NotNil(t, conn)
31
32
	// Test Remove
33
	c.Remove(node)
34
	_, _, ok = c.Get("key")
35
	assert.False(t, ok)
36
37
	// Test AddWithReplicas
38
	c.AddWithReplicas(node, 100)
39
	_, conn, ok = c.Get("key")
40
	assert.True(t, ok)
41
	assert.NotNil(t, conn)
42
}
43