add fakes for events package, add startEventWatcher to event interface

Signed-off-by: Yassine TIJANI <ytijani@vmware.com>

Kubernetes-commit: bfa7de0165c911178daaac1991c08f7424b0048f
This commit is contained in:
Yassine TIJANI 2019-07-05 14:18:02 +02:00 committed by Kubernetes Publisher
parent 07281898b0
commit 36ad1c08c8
4 changed files with 55 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2014 The Kubernetes Authors. Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.

View File

@ -271,9 +271,9 @@ func getKey(event *v1beta1.Event) eventKey {
return key return key
} }
// startEventWatcher starts sending events received from this EventBroadcaster to the given event handler function. // StartEventWatcher starts sending events received from this EventBroadcaster to the given event handler function.
// The return value is used to stop recording // The return value is used to stop recording
func (e *eventBroadcasterImpl) startEventWatcher(eventHandler func(event runtime.Object)) func() { func (e *eventBroadcasterImpl) StartEventWatcher(eventHandler func(event runtime.Object)) func() {
watcher := e.Watch() watcher := e.Watch()
go func() { go func() {
defer utilruntime.HandleCrash() defer utilruntime.HandleCrash()
@ -304,7 +304,7 @@ func (e *eventBroadcasterImpl) StartRecordingToSink(stopCh <-chan struct{}) {
} }
e.recordToSink(event, clock.RealClock{}) e.recordToSink(event, clock.RealClock{})
} }
stopWatcher := e.startEventWatcher(eventHandler) stopWatcher := e.StartEventWatcher(eventHandler)
go func() { go func() {
<-stopCh <-stopCh
stopWatcher() stopWatcher()

45
tools/events/fake.go Normal file
View File

@ -0,0 +1,45 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package events
import (
"fmt"
"k8s.io/apimachinery/pkg/runtime"
)
// FakeRecorder is used as a fake during tests. It is thread safe. It is usable
// when created manually and not by NewFakeRecorder, however all events may be
// thrown away in this case.
type FakeRecorder struct {
Events chan string
}
// Eventf emits an event
func (f *FakeRecorder) Eventf(regarding runtime.Object, related runtime.Object, eventtype, reason, action, note string, args ...interface{}) {
if f.Events != nil {
f.Events <- fmt.Sprintf(eventtype+" "+reason+" "+note, args...)
}
}
// NewFakeRecorder creates new fake event recorder with event channel with
// buffer of given size.
func NewFakeRecorder(bufferSize int) *FakeRecorder {
return &FakeRecorder{
Events: make(chan string, bufferSize),
}
}

View File

@ -45,6 +45,12 @@ type EventBroadcaster interface {
// NewRecorder returns an EventRecorder that can be used to send events to this EventBroadcaster // NewRecorder returns an EventRecorder that can be used to send events to this EventBroadcaster
// with the event source set to the given event source. // with the event source set to the given event source.
NewRecorder(scheme *runtime.Scheme, reportingController string) EventRecorder NewRecorder(scheme *runtime.Scheme, reportingController string) EventRecorder
// StartEventWatcher enables you to watch for emitted events without usage
// of StartRecordingToSink. This lets you also process events in a custom way (e.g. in tests).
// NOTE: events received on your eventHandler should be copied before being used.
// TODO: figure out if this can be removed.
StartEventWatcher(eventHandler func(event runtime.Object)) func()
} }
// EventSink knows how to store events (client-go implements it.) // EventSink knows how to store events (client-go implements it.)