44 "context"
55 "fmt"
66 "net/http"
7+ "sync"
78 "testing"
89 "time"
910
@@ -32,7 +33,7 @@ func ExampleNewOwnedResourceController() {
3233 CtxQueue := queue .NewQueueOperationsCtx ()
3334 registry := typed .NewRegistry ()
3435 broadcaster := record .NewBroadcaster ()
35- eventSink := & typedcorev1.EventSinkImpl {Interface : fake .NewSimpleClientset ().CoreV1 ().Events ("" )}
36+ eventSink := & typedcorev1.EventSinkImpl {Interface : fake .NewClientset ().CoreV1 ().Events ("" )}
3637
3738 // the controller processes objects on the queue, but doesn't set up any
3839 // informers by default.
@@ -120,11 +121,12 @@ func TestControllerEventsBroadcast(t *testing.T) {
120121 recorder .Event (& v1.ObjectReference {Namespace : "test" , Name : "a" }, v1 .EventTypeNormal , "test" , "test" )
121122
122123 require .Eventually (t , func () bool {
123- return len ( eventSink .Events ) > 0
124+ return eventSink .Len ( ) > 0
124125 }, 5 * time .Second , 1 * time .Millisecond )
125126}
126127
127128type fakeEventSink struct {
129+ mu sync.Mutex
128130 Events map [types.UID ]* v1.Event
129131}
130132
@@ -134,18 +136,30 @@ func newFakeEventSink() *fakeEventSink {
134136 }
135137}
136138
139+ func (f * fakeEventSink ) Len () int {
140+ f .mu .Lock ()
141+ defer f .mu .Unlock ()
142+ return len (f .Events )
143+ }
144+
137145func (f * fakeEventSink ) Create (event * v1.Event ) (* v1.Event , error ) {
146+ f .mu .Lock ()
138147 f .Events [event .UID ] = event
148+ f .mu .Unlock ()
139149 return event , nil
140150}
141151
142152func (f * fakeEventSink ) Update (event * v1.Event ) (* v1.Event , error ) {
153+ f .mu .Lock ()
143154 f .Events [event .UID ] = event
155+ f .mu .Unlock ()
144156 return event , nil
145157}
146158
147159func (f * fakeEventSink ) Patch (oldEvent * v1.Event , _ []byte ) (* v1.Event , error ) {
160+ f .mu .Lock ()
148161 f .Events [oldEvent .UID ] = oldEvent
162+ f .mu .Unlock ()
149163 return oldEvent , nil
150164}
151165
0 commit comments