|
| 1 | +package ch |
| 2 | + |
| 3 | +import ( |
| 4 | + "strconv" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestConsistentHashing_NodeAddition(t *testing.T) { |
| 9 | + ch := New[string](3, nil) |
| 10 | + ch.AddNode("NodeA") |
| 11 | + ch.AddNode("NodeB") |
| 12 | + ch.AddNode("NodeC") |
| 13 | + |
| 14 | + key := "my-key" |
| 15 | + node := ch.GetNode(key) |
| 16 | + |
| 17 | + if node == "" { |
| 18 | + t.Errorf("Expected a valid node, but got an empty string") |
| 19 | + } |
| 20 | + |
| 21 | + sameNode := ch.GetNode(key) |
| 22 | + if node != sameNode { |
| 23 | + t.Errorf("Expected consistent mapping, but got different results") |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +func TestConsistentHashing_AddGetKey(t *testing.T) { |
| 28 | + ch := New[int](3, nil) |
| 29 | + ch.AddNode("NodeA") |
| 30 | + ch.AddNode("NodeB") |
| 31 | + |
| 32 | + ch.AddKey("user123", 99) |
| 33 | + ch.AddKey("user456", 42) |
| 34 | + |
| 35 | + value, exists := ch.GetKey("user123") |
| 36 | + if !exists || value != 99 { |
| 37 | + t.Errorf("Expected 99, but got %d", value) |
| 38 | + } |
| 39 | + |
| 40 | + value, exists = ch.GetKey("user456") |
| 41 | + if !exists || value != 42 { |
| 42 | + t.Errorf("Expected 42, but got %d", value) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestConsistentHashing_RemoveKey(t *testing.T) { |
| 47 | + ch := New[string](3, nil) |
| 48 | + ch.AddNode("NodeA") |
| 49 | + ch.AddKey("user123", "Data1") |
| 50 | + |
| 51 | + ch.RemoveKey("user123") |
| 52 | + |
| 53 | + _, exists := ch.GetKey("user123") |
| 54 | + if exists { |
| 55 | + t.Errorf("Expected key to be removed, but it still exists") |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +type TestStruct struct { |
| 60 | + Name string |
| 61 | + Score int |
| 62 | +} |
| 63 | + |
| 64 | +func TestConsistentHashing_WithStruct(t *testing.T) { |
| 65 | + ch := New[TestStruct](3, nil) |
| 66 | + ch.AddNode("NodeA") |
| 67 | + ch.AddNode("NodeB") |
| 68 | + |
| 69 | + data := TestStruct{Name: "Alice", Score: 100} |
| 70 | + ch.AddKey("user123", data) |
| 71 | + |
| 72 | + retrieved, exists := ch.GetKey("user123") |
| 73 | + if !exists || retrieved.Name != "Alice" || retrieved.Score != 100 { |
| 74 | + t.Errorf("Expected Alice with score 100, but got %+v", retrieved) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func BenchmarkConsistentHashing_AddNode(b *testing.B) { |
| 79 | + ch := New[string](100, nil) |
| 80 | + |
| 81 | + b.ReportAllocs() |
| 82 | + b.ResetTimer() |
| 83 | + for i := 0; i < b.N; i++ { |
| 84 | + ch.AddNode("Node" + strconv.Itoa(i)) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func BenchmarkConsistentHashing_RemoveNode(b *testing.B) { |
| 89 | + ch := New[string](100, nil) |
| 90 | + for i := 0; i < 1000; i++ { |
| 91 | + ch.AddNode("Node" + strconv.Itoa(i)) |
| 92 | + } |
| 93 | + |
| 94 | + b.ReportAllocs() |
| 95 | + b.ResetTimer() |
| 96 | + for i := 0; i < b.N; i++ { |
| 97 | + ch.RemoveNode("Node" + strconv.Itoa(i%1000)) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func BenchmarkConsistentHashing_GetNode(b *testing.B) { |
| 102 | + ch := New[string](100, nil) |
| 103 | + for i := 0; i < 1000; i++ { |
| 104 | + ch.AddNode("Node" + strconv.Itoa(i)) |
| 105 | + } |
| 106 | + |
| 107 | + b.ReportAllocs() |
| 108 | + b.ResetTimer() |
| 109 | + for i := 0; i < b.N; i++ { |
| 110 | + _ = ch.GetNode("key" + strconv.Itoa(i)) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func BenchmarkConsistentHashing_AddKey(b *testing.B) { |
| 115 | + ch := New[string](100, nil) |
| 116 | + for i := 0; i < 1000; i++ { |
| 117 | + ch.AddNode("Node" + strconv.Itoa(i)) |
| 118 | + } |
| 119 | + |
| 120 | + b.ReportAllocs() |
| 121 | + b.ResetTimer() |
| 122 | + for i := 0; i < b.N; i++ { |
| 123 | + ch.AddKey("key"+strconv.Itoa(i), "value"+strconv.Itoa(i)) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func BenchmarkConsistentHashing_RemoveKey(b *testing.B) { |
| 128 | + ch := New[string](100, nil) |
| 129 | + for i := 0; i < 1000; i++ { |
| 130 | + ch.AddNode("Node" + strconv.Itoa(i)) |
| 131 | + } |
| 132 | + for i := 0; i < 10000; i++ { |
| 133 | + ch.AddKey("key"+strconv.Itoa(i), "value"+strconv.Itoa(i)) |
| 134 | + } |
| 135 | + |
| 136 | + b.ReportAllocs() |
| 137 | + b.ResetTimer() |
| 138 | + for i := 0; i < b.N; i++ { |
| 139 | + ch.RemoveKey("key" + strconv.Itoa(i%10000)) |
| 140 | + } |
| 141 | +} |
0 commit comments