MCPcopy
hub / github.com/kubernetes/client-go / TestReflectorWatchHandler

Function TestReflectorWatchHandler

tools/cache/reflector_test.go:141–195  ·  tools/cache/reflector_test.go::TestReflectorWatchHandler
(t *testing.T)

Source from the content-addressed store, hash-verified

139}
140
141func TestReflectorWatchHandler(t *testing.T) {
142 s := NewStore(MetaNamespaceKeyFunc)
143 g := NewReflector(&testLW{}, &v1.Pod{}, s, 0)
144 fw := watch.NewFake()
145 s.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
146 s.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar"}})
147 go func() {
148 fw.Add(&v1.Service{ObjectMeta: metav1.ObjectMeta{Name: "rejected"}})
149 fw.Delete(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "foo"}})
150 fw.Modify(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "bar", ResourceVersion: "55"}})
151 fw.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "baz", ResourceVersion: "32"}})
152 fw.Stop()
153 }()
154 var resumeRV string
155 err := g.watchHandler(fw, &resumeRV, nevererrc, wait.NeverStop)
156 if err != nil {
157 t.Errorf("unexpected error %v", err)
158 }
159
160 mkPod := func(id string, rv string) *v1.Pod {
161 return &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: id, ResourceVersion: rv}}
162 }
163
164 table := []struct {
165 Pod *v1.Pod
166 exists bool
167 }{
168 {mkPod("foo", ""), false},
169 {mkPod("rejected", ""), false},
170 {mkPod("bar", "55"), true},
171 {mkPod("baz", "32"), true},
172 }
173 for _, item := range table {
174 obj, exists, _ := s.Get(item.Pod)
175 if e, a := item.exists, exists; e != a {
176 t.Errorf("%v: expected %v, got %v", item.Pod, e, a)
177 }
178 if !exists {
179 continue
180 }
181 if e, a := item.Pod.ResourceVersion, obj.(*v1.Pod).ResourceVersion; e != a {
182 t.Errorf("%v: expected %v, got %v", item.Pod, e, a)
183 }
184 }
185
186 // RV should send the last version we see.
187 if e, a := "32", resumeRV; e != a {
188 t.Errorf("expected %v, got %v", e, a)
189 }
190
191 // last sync resource version should be the last version synced with store
192 if e, a := "32", g.LastSyncResourceVersion(); e != a {
193 t.Errorf("expected %v, got %v", e, a)
194 }
195}
196
197func TestReflectorStopWatch(t *testing.T) {
198 s := NewStore(MetaNamespaceKeyFunc)

Callers

nothing calls this directly

Calls 10

AddMethod · 0.95
watchHandlerMethod · 0.95
GetMethod · 0.95
NewStoreFunction · 0.85
NewReflectorFunction · 0.85
ModifyMethod · 0.80
DeleteMethod · 0.65
StopMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected