| Conditions | 14 |
| Total Lines | 62 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
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 |
||
| 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 |