github.com/blend/go-sdk@v1.20220411.3/consistenthash/main_test.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package consistenthash 9 10 import ( 11 "encoding/json" 12 "fmt" 13 "sort" 14 "strings" 15 16 "github.com/blend/go-sdk/assert" 17 ) 18 19 func spew(v interface{}) string { 20 output, _ := json.Marshal(v) 21 return string(output) 22 } 23 24 func assignmentCounts(assignments map[string][]string) map[string]int { 25 output := make(map[string]int) 26 for bucket, items := range assignments { 27 output[bucket] = len(items) 28 } 29 return output 30 } 31 32 // setsAreDisjoint asserts that a given list of sets has no overlapping items with any of the other sets. 33 // 34 // this is helpful to prove that there are no duplicated items within sets. 35 func setsAreDisjoint(its *assert.Assertions, itemSets ...[]string) { 36 for thisSetIndex, thisSet := range itemSets { 37 for _, thisItem := range thisSet { 38 for otherSetIndex, otherSet := range itemSets { 39 if thisSetIndex != otherSetIndex { 40 for _, otherItem := range otherSet { 41 its.NotEqual(thisItem, otherItem) 42 } 43 } 44 } 45 } 46 } 47 } 48 49 func setExistsInOtherSets(its *assert.Assertions, items []string, otherSets ...[]string) { 50 for _, item := range items { 51 its.True(findItem(item, otherSets...), fmt.Sprintf("Item was not found in any supplied assignments: %s", item)) 52 } 53 } 54 55 func setsAreEvenlySized(its *assert.Assertions, itemCount int, sets ...[]string) { 56 // assert that the item count is represented in the sets somewhat evenly 57 setCount := len(sets) 58 itemsPerSet := itemCount / setCount 59 minSize := 0 60 maxSize := 2 * itemsPerSet 61 62 for _, set := range sets { 63 its.True(len(set) > minSize && len(set) < maxSize, fmt.Sprintf("sets should be between %d and %d in size, actual: %d", minSize, maxSize, len(set))) 64 } 65 } 66 67 // setIsSorted asserts the set, and the set modified by `sort.Strings` are the same. 68 func setIsSorted(its *assert.Assertions, set []string) { 69 its.NotEmpty(set) 70 trialSorted := make([]string, len(set)) 71 copy(trialSorted, set) 72 sort.Strings(trialSorted) 73 its.Equal(trialSorted, set) 74 } 75 76 // itsConsistent asserts that a given before and after set differ by a given maximum delta. 77 func itsConsistent(its *assert.Assertions, old, new []string, maxDelta int) { 78 var delta int 79 for _, oldItem := range old { 80 if !findItem(oldItem, new) { 81 delta++ 82 } 83 } 84 its.True(delta < maxDelta, fmt.Sprintf("The item arrays should differ only by %d items (differed by %d items)\n\told: %s\n\tnew: %s\n", maxDelta, delta, strings.Join(old, ", "), strings.Join(new, ", "))) 85 } 86 87 func findItem(item string, assignments ...[]string) bool { 88 for _, assignment := range assignments { 89 for _, assignmentItem := range assignment { 90 if item == assignmentItem { 91 return true 92 } 93 } 94 } 95 return false 96 } 97 98 func assignmentsAreEqual(its *assert.Assertions, oldAssignments, newAssignments map[string][]string) { 99 if len(oldAssignments) != len(newAssignments) { 100 its.Fail(fmt.Sprintf("assignments are not equal; length of maps differ: %d vs. %d", len(oldAssignments), len(newAssignments))) 101 } 102 103 for oldBucketName, oldBucket := range oldAssignments { 104 newBucket, ok := newAssignments[oldBucketName] 105 if !ok { 106 its.Fail(fmt.Sprintf("assignments are not equal; old bucket %s not found in new assignments", oldBucketName)) 107 } 108 if len(oldBucket) != len(newBucket) { 109 its.Fail(fmt.Sprintf("assignments are not equal; length of assignment lists for %s differ: %d vs. %d", oldBucketName, len(oldBucket), len(newBucket))) 110 } 111 for _, oldBucketItem := range oldBucket { 112 if !findItem(oldBucketItem, newBucket) { 113 its.Fail(fmt.Sprintf("assignments are not equal; old bucket %s item %s not found in new bucket", oldBucketName, oldBucketItem)) 114 } 115 } 116 } 117 }