| 23 | ) |
| 24 | |
| 25 | func TestHashing(t *testing.T) { |
| 26 | |
| 27 | // Override the hash function to return easier to reason about values. Assumes |
| 28 | // the keys can be converted to an integer. |
| 29 | hash := New(3, func(key []byte) uint32 { |
| 30 | i, err := strconv.Atoi(string(key)) |
| 31 | if err != nil { |
| 32 | panic(err) |
| 33 | } |
| 34 | return uint32(i) |
| 35 | }) |
| 36 | |
| 37 | // Given the above hash function, this will give replicas with "hashes": |
| 38 | // 2, 4, 6, 12, 14, 16, 22, 24, 26 |
| 39 | hash.Add("6", "4", "2") |
| 40 | |
| 41 | testCases := map[string]string{ |
| 42 | "2": "2", |
| 43 | "11": "2", |
| 44 | "23": "4", |
| 45 | "27": "2", |
| 46 | } |
| 47 | |
| 48 | for k, v := range testCases { |
| 49 | if hash.Get(k) != v { |
| 50 | t.Errorf("Asking for %s, should have yielded %s", k, v) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | // Adds 8, 18, 28 |
| 55 | hash.Add("8") |
| 56 | |
| 57 | // 27 should now map to 8. |
| 58 | testCases["27"] = "8" |
| 59 | |
| 60 | for k, v := range testCases { |
| 61 | if hash.Get(k) != v { |
| 62 | t.Errorf("Asking for %s, should have yielded %s", k, v) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | } |
| 67 | |
| 68 | func TestConsistency(t *testing.T) { |
| 69 | hash1 := New(1, nil) |