k8s.io/client-go@v0.31.1/tools/cache/undelta_store_test.go (about) 1 /* 2 Copyright 2015 The Kubernetes 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 http://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 cache 18 19 import ( 20 "reflect" 21 "testing" 22 ) 23 24 // store_test.go checks that UndeltaStore conforms to the Store interface 25 // behavior. This test just tests that it calls the push func in addition. 26 27 type testUndeltaObject struct { 28 name string 29 val interface{} 30 } 31 32 func testUndeltaKeyFunc(obj interface{}) (string, error) { 33 return obj.(testUndeltaObject).name, nil 34 } 35 36 /* 37 var ( 38 o1 interface{} = t{1} 39 o2 interface{} = t{2} 40 l1 []interface{} = []interface{}{t{1}} 41 ) 42 */ 43 44 func TestUpdateCallsPush(t *testing.T) { 45 mkObj := func(name string, val interface{}) testUndeltaObject { 46 return testUndeltaObject{name: name, val: val} 47 } 48 49 var got []interface{} 50 var callcount = 0 51 push := func(m []interface{}) { 52 callcount++ 53 got = m 54 } 55 56 u := NewUndeltaStore(push, testUndeltaKeyFunc) 57 58 u.Add(mkObj("a", 2)) 59 u.Update(mkObj("a", 1)) 60 if callcount != 2 { 61 t.Errorf("Expected 2 calls, got %d", callcount) 62 } 63 64 l := []interface{}{mkObj("a", 1)} 65 if !reflect.DeepEqual(l, got) { 66 t.Errorf("Expected %#v, Got %#v", l, got) 67 } 68 } 69 70 func TestDeleteCallsPush(t *testing.T) { 71 mkObj := func(name string, val interface{}) testUndeltaObject { 72 return testUndeltaObject{name: name, val: val} 73 } 74 75 var got []interface{} 76 var callcount = 0 77 push := func(m []interface{}) { 78 callcount++ 79 got = m 80 } 81 82 u := NewUndeltaStore(push, testUndeltaKeyFunc) 83 84 u.Add(mkObj("a", 2)) 85 u.Delete(mkObj("a", "")) 86 if callcount != 2 { 87 t.Errorf("Expected 2 calls, got %d", callcount) 88 } 89 expected := []interface{}{} 90 if !reflect.DeepEqual(expected, got) { 91 t.Errorf("Expected %#v, Got %#v", expected, got) 92 } 93 } 94 95 func TestReadsDoNotCallPush(t *testing.T) { 96 push := func(m []interface{}) { 97 t.Errorf("Unexpected call to push!") 98 } 99 100 u := NewUndeltaStore(push, testUndeltaKeyFunc) 101 102 // These should not call push. 103 _ = u.List() 104 _, _, _ = u.Get(testUndeltaObject{"a", ""}) 105 } 106 107 func TestReplaceCallsPush(t *testing.T) { 108 mkObj := func(name string, val interface{}) testUndeltaObject { 109 return testUndeltaObject{name: name, val: val} 110 } 111 112 var got []interface{} 113 var callcount = 0 114 push := func(m []interface{}) { 115 callcount++ 116 got = m 117 } 118 119 u := NewUndeltaStore(push, testUndeltaKeyFunc) 120 121 m := []interface{}{mkObj("a", 1)} 122 123 u.Replace(m, "0") 124 if callcount != 1 { 125 t.Errorf("Expected 1 calls, got %d", callcount) 126 } 127 expected := []interface{}{mkObj("a", 1)} 128 if !reflect.DeepEqual(expected, got) { 129 t.Errorf("Expected %#v, Got %#v", expected, got) 130 } 131 }