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

hash_test.TestConsistentHash_Updated   F

Complexity

Conditions 14

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 38
nop 1
dl 0
loc 62
rs 3.6
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A hash.TestAddAndRemove 0 31 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like hash_test.TestConsistentHash_Updated often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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