knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/hash/bucketer_test.go (about) 1 /* 2 Copyright 2020 The Knative Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 https://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package hash 18 19 import ( 20 "reflect" 21 "testing" 22 23 "github.com/google/go-cmp/cmp" 24 "k8s.io/apimachinery/pkg/types" 25 "k8s.io/apimachinery/pkg/util/sets" 26 ) 27 28 const ( 29 thisBucket = "monsoon" 30 knownKey = "hagel" 31 otherBucket = "chubasco" 32 unknownKey = "snow" // snow maps to "chubasco", originally. 33 ) 34 35 var buckets = sets.New[string](thisBucket, otherBucket, "aguacero", "chaparrón") 36 37 func TestBucketSetOwner(t *testing.T) { 38 b := NewBucketSet(buckets) 39 if got := b.Owner(knownKey); got != thisBucket { 40 t.Errorf("Owner = %q, want: %q", got, thisBucket) 41 } 42 if l := b.cache.Len(); l != 1 { 43 t.Errorf("|Cache| = %d, want: 1", l) 44 } 45 if n, ok := b.cache.Get(knownKey); !ok || n.(string) != thisBucket { 46 t.Errorf("Cache[%s] = %q, want: %q", knownKey, n, thisBucket) 47 } 48 // Verify nothing is added to the cache. 49 if got := b.Owner(knownKey); got != thisBucket { 50 t.Errorf("Owner = %q, want: %q", got, thisBucket) 51 } 52 if l := b.cache.Len(); l != 1 { 53 t.Errorf("|Cache| = %d, want: 1", l) 54 } 55 56 if got := b.Owner(unknownKey); got != otherBucket { 57 t.Errorf("Owner = %q, want: %q", got, otherBucket) 58 } 59 if l := b.cache.Len(); l != 2 { 60 t.Errorf("|Cache| = %d, want: 2", l) 61 } 62 if n, ok := b.cache.Get(unknownKey); !ok || n.(string) != otherBucket { 63 t.Errorf("Cache[%s] = %q, want: %q", unknownKey, n, otherBucket) 64 } 65 } 66 67 func TestBucketSetList(t *testing.T) { 68 bs := NewBucketSet(buckets) 69 70 got := bs.BucketList() 71 if want := sets.List(buckets); !reflect.DeepEqual(got, want) { 72 t.Errorf("Name = %q, want: %q, diff(-want,+got):\n%s", got, want, cmp.Diff(want, got)) 73 } 74 } 75 76 func TestBucketSetUpdate(t *testing.T) { 77 b := NewBucketSet(buckets) 78 b.Owner(knownKey) 79 80 // Need a clone. 81 newNames := buckets.Difference(sets.New[string](otherBucket)) 82 b.Update(newNames) 83 if b.cache.Len() != 0 { 84 t.Error("cache was not emptied") 85 } 86 87 // Verify the mapping is stable. 88 if got := b.Owner(knownKey); got != thisBucket { 89 t.Errorf("Owner = %q, want: %q", got, thisBucket) 90 } 91 if l := b.cache.Len(); l != 1 { 92 t.Errorf("|Cache| = %d, want: 1", l) 93 } 94 if n, ok := b.cache.Get(knownKey); !ok || n.(string) != thisBucket { 95 t.Errorf("Cache[%s] = %q, want: %q", knownKey, n, thisBucket) 96 } 97 // unknownKey should've migrated. 98 if got := b.Owner(unknownKey); got == otherBucket { 99 t.Errorf("Owner = %q, don't want: %q", got, otherBucket) 100 } 101 } 102 103 func TestBucketSetBuckets(t *testing.T) { 104 bs := NewBucketSet(buckets) 105 bkts := bs.Buckets() 106 107 // Sorted 108 want := []string{"aguacero", "chaparrón", "chubasco", "monsoon"} 109 got := make([]string, len(bkts)) 110 for i, b := range bkts { 111 got[i] = b.Name() 112 } 113 114 if !reflect.DeepEqual(got, want) { 115 t.Errorf("Got %q, want %q, diff(-want,+got):\n%s", got, want, cmp.Diff(want, got)) 116 } 117 } 118 119 func TestBucketSetHasBucket(t *testing.T) { 120 bs := NewBucketSet(sets.New[string](thisBucket, "aguacero", "chaparrón")) 121 if !bs.HasBucket(thisBucket) { 122 t.Errorf("HasBucket(%v) = false", thisBucket) 123 } 124 if bs.HasBucket(otherBucket) { 125 t.Errorf("HasBucket(%v) = true", otherBucket) 126 } 127 } 128 129 func TestBucketHas(t *testing.T) { 130 bs := NewBucketSet(buckets) 131 b := Bucket{ 132 name: thisBucket, 133 buckets: bs, 134 } 135 thisNN := types.NamespacedName{Namespace: "snow", Name: "hail"} 136 if !b.Has(thisNN) { 137 t.Errorf("Has(%v) = false", thisNN) 138 } 139 b = Bucket{ 140 name: otherBucket, 141 buckets: bs, 142 } 143 if b.Has(thisNN) { 144 t.Errorf("Other bucket Has(%v) = true", thisNN) 145 } 146 } 147 148 func TestBucketName(t *testing.T) { 149 bs := NewBucketSet(buckets) 150 b := Bucket{ 151 name: thisBucket, 152 buckets: bs, 153 } 154 if got, want := b.Name(), thisBucket; got != want { 155 t.Errorf("Name = %q, want: %q", got, want) 156 } 157 }