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

Function TestHammerController

tools/cache/controller_test.go:180–292  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

178}
179
180func TestHammerController(t *testing.T) {
181 // This test executes a bunch of requests through the fake source and
182 // controller framework to make sure there's no locking/threading
183 // errors. If an error happens, it should hang forever or trigger the
184 // race detector.
185
186 // source simulates an apiserver object endpoint.
187 source := fcache.NewFakeControllerSource()
188
189 // Let's do threadsafe output to get predictable test results.
190 outputSetLock := sync.Mutex{}
191 // map of key to operations done on the key
192 outputSet := map[string][]string{}
193
194 recordFunc := func(eventType string, obj interface{}) {
195 key, err := DeletionHandlingMetaNamespaceKeyFunc(obj)
196 if err != nil {
197 t.Errorf("something wrong with key: %v", err)
198 key = "oops something went wrong with the key"
199 }
200
201 // Record some output when items are deleted.
202 outputSetLock.Lock()
203 defer outputSetLock.Unlock()
204 outputSet[key] = append(outputSet[key], eventType)
205 }
206
207 // Make a controller which just logs all the changes it gets.
208 _, controller := NewInformer(
209 source,
210 &v1.Pod{},
211 time.Millisecond*100,
212 ResourceEventHandlerFuncs{
213 AddFunc: func(obj interface{}) { recordFunc("add", obj) },
214 UpdateFunc: func(oldObj, newObj interface{}) { recordFunc("update", newObj) },
215 DeleteFunc: func(obj interface{}) { recordFunc("delete", obj) },
216 },
217 )
218
219 if controller.HasSynced() {
220 t.Errorf("Expected HasSynced() to return false before we started the controller")
221 }
222
223 // Run the controller and run it until we close stop.
224 stop := make(chan struct{})
225 go controller.Run(stop)
226
227 // Let's wait for the controller to do its initial sync
228 wait.Poll(100*time.Millisecond, wait.ForeverTestTimeout, func() (bool, error) {
229 return controller.HasSynced(), nil
230 })
231 if !controller.HasSynced() {
232 t.Errorf("Expected HasSynced() to return true after the initial sync")
233 }
234
235 wg := sync.WaitGroup{}
236 const threads = 3
237 wg.Add(threads)

Callers

nothing calls this directly

Calls 15

AddMethod · 0.95
ModifyMethod · 0.95
DeleteMethod · 0.95
NewInformerFunction · 0.85
ErrorfMethod · 0.65
HasSyncedMethod · 0.65
RunMethod · 0.65
AddMethod · 0.65
DoneMethod · 0.65
LenMethod · 0.65
ListMethod · 0.65

Tested by

no test coverage detected