github.com/andy2046/gopie@v0.7.0/pkg/ringhash/ringhash_test.go (about) 1 package ringhash 2 3 import ( 4 "fmt" 5 "strconv" 6 "testing" 7 ) 8 9 func TestAddNode(t *testing.T) { 10 r := New() 11 12 r.AddNode("127.0.0.1:80") 13 if len(r.hashes) != r.replicas { 14 t.Fatal("wrong vnodes number") 15 } 16 } 17 18 func TestGetNode(t *testing.T) { 19 r := New() 20 21 r.AddNode("127.0.0.1:80") 22 node, err := r.GetNode("127.0.0.1:80") 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 if node != "127.0.0.1:80" { 28 t.Fatalf("wrong node, expected 127.0.0.1:80, got %v\n", node) 29 } 30 } 31 32 func TestRemoveNode(t *testing.T) { 33 r := New() 34 35 r.AddNode("127.0.0.1:80") 36 r.RemoveNode("127.0.0.1:80") 37 38 if len(r.hashes) != 0 && len(r.hashKeyMap) != 0 { 39 t.Fatal(("remove not working")) 40 } 41 } 42 43 func TestGetLeastNode(t *testing.T) { 44 option := func(c *Config) error { 45 c.BalancingFactor = 1.02 46 return nil 47 } 48 r := New(option) 49 50 r.AddNode("127.0.0.1:80") 51 r.AddNode("192.168.0.1:80") 52 r.AddNode("10.0.0.1:80") 53 54 for i := 0; i < 100; i++ { 55 node, err := r.GetLeastNode("192.168.0.1:81") 56 if err != nil { 57 t.Fatal(err) 58 } 59 r.Add(node) 60 } 61 62 for k, v := range r.Loads() { 63 if v > r.MaxLoad() { 64 t.Fatalf("node %s is overloaded, %d > %d\n", k, v, r.MaxLoad()) 65 } 66 } 67 fmt.Println("Max load per node ->", r.MaxLoad()) 68 fmt.Println(r.Loads()) 69 } 70 71 func TestAddDone(t *testing.T) { 72 r := New() 73 74 r.AddNode("127.0.0.1:80") 75 r.AddNode("192.168.0.1:80") 76 77 node, err := r.GetLeastNode("192.168.0.1:81") 78 if err != nil { 79 t.Fatal(err) 80 } 81 82 r.Add(node) 83 if r.keyLoadMap[node].Load != 1 { 84 t.Fatalf("load for node %s should be 1\n", node) 85 } 86 87 r.Done(node) 88 if r.keyLoadMap[node].Load != 0 { 89 t.Fatalf("load for node %s should be 0\n", node) 90 } 91 } 92 93 func BenchmarkGetNode(b *testing.B) { 94 r := New() 95 for i := 0; i < 10; i++ { 96 r.AddNode("start" + strconv.Itoa(i)) 97 } 98 tt := []struct { 99 key string 100 }{ 101 {"test"}, 102 {"test1"}, 103 {"test2"}, 104 {"test3"}, 105 {"test4"}, 106 {"test5"}, 107 } 108 b.ResetTimer() 109 for i := 0; i < b.N; i++ { 110 t := tt[i%len(tt)] 111 r.GetNode(t.key) 112 } 113 } 114 115 func BenchmarkGetLeastNode(b *testing.B) { 116 r := New() 117 for i := 0; i < 10; i++ { 118 r.AddNode("start" + strconv.Itoa(i)) 119 } 120 tt := []struct { 121 key string 122 }{ 123 {"test"}, 124 {"test1"}, 125 {"test2"}, 126 {"test3"}, 127 {"test4"}, 128 {"test5"}, 129 } 130 b.ResetTimer() 131 for i := 0; i < b.N; i++ { 132 t := tt[i%len(tt)] 133 r.GetLeastNode(t.key) 134 } 135 } 136 137 func BenchmarkAddRemoveNode(b *testing.B) { 138 r := New() 139 for i := 0; i < 10; i++ { 140 r.AddNode("start" + strconv.Itoa(i)) 141 } 142 b.ResetTimer() 143 for i := 0; i < b.N; i++ { 144 r.AddNode("foo" + strconv.Itoa(i)) 145 r.RemoveNode("foo" + strconv.Itoa(i)) 146 } 147 } 148 149 func BenchmarkAddDone(b *testing.B) { 150 r := New() 151 for i := 0; i < 10; i++ { 152 r.AddNode("start" + strconv.Itoa(i)) 153 } 154 b.ResetTimer() 155 for i := 0; i < b.N; i++ { 156 node, _ := r.GetLeastNode("start" + strconv.Itoa(i)) 157 r.Add(node) 158 r.Done(node) 159 } 160 }