()
| 121 | } |
| 122 | |
| 123 | func ExampleNewInformer() { |
| 124 | // source simulates an apiserver object endpoint. |
| 125 | source := fcache.NewFakeControllerSource() |
| 126 | |
| 127 | // Let's do threadsafe output to get predictable test results. |
| 128 | deletionCounter := make(chan string, 1000) |
| 129 | |
| 130 | // Make a controller that immediately deletes anything added to it, and |
| 131 | // logs anything deleted. |
| 132 | _, controller := NewInformer( |
| 133 | source, |
| 134 | &v1.Pod{}, |
| 135 | time.Millisecond*100, |
| 136 | ResourceEventHandlerFuncs{ |
| 137 | AddFunc: func(obj interface{}) { |
| 138 | source.Delete(obj.(runtime.Object)) |
| 139 | }, |
| 140 | DeleteFunc: func(obj interface{}) { |
| 141 | key, err := DeletionHandlingMetaNamespaceKeyFunc(obj) |
| 142 | if err != nil { |
| 143 | key = "oops something went wrong with the key" |
| 144 | } |
| 145 | |
| 146 | // Report this deletion. |
| 147 | deletionCounter <- key |
| 148 | }, |
| 149 | }, |
| 150 | ) |
| 151 | |
| 152 | // Run the controller and run it until we close stop. |
| 153 | stop := make(chan struct{}) |
| 154 | defer close(stop) |
| 155 | go controller.Run(stop) |
| 156 | |
| 157 | // Let's add a few objects to the source. |
| 158 | testIDs := []string{"a-hello", "b-controller", "c-framework"} |
| 159 | for _, name := range testIDs { |
| 160 | // Note that these pods are not valid-- the fake source doesn't |
| 161 | // call validation or anything. |
| 162 | source.Add(&v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: name}}) |
| 163 | } |
| 164 | |
| 165 | // Let's wait for the controller to process the things we just added. |
| 166 | outputSet := sets.String{} |
| 167 | for i := 0; i < len(testIDs); i++ { |
| 168 | outputSet.Insert(<-deletionCounter) |
| 169 | } |
| 170 | |
| 171 | for _, key := range outputSet.List() { |
| 172 | fmt.Println(key) |
| 173 | } |
| 174 | // Output: |
| 175 | // a-hello |
| 176 | // b-controller |
| 177 | // c-framework |
| 178 | } |
| 179 | |
| 180 | func TestHammerController(t *testing.T) { |
nothing calls this directly
no test coverage detected