Make fake event recorder thread safe.

The test recorder should be thread safe in case it's used in multiple
goroutines. This does not serve to ensure any order of recorded events,
only to prevent golang race detector to crash when two events are emitted by
concurrent goroutines.
This commit is contained in:
Jan Safranek 2016-05-04 14:20:13 +02:00
parent 963aebd3e5
commit f4abfb2a0c

View File

@ -23,18 +23,32 @@ import (
"k8s.io/kubernetes/pkg/runtime"
)
// FakeRecorder is used as a fake during tests.
// 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 []string
Events chan string
}
func (f *FakeRecorder) Event(object runtime.Object, eventtype, reason, message string) {
f.Events = append(f.Events, fmt.Sprintf("%s %s %s", eventtype, reason, message))
if f.Events != nil {
f.Events <- fmt.Sprintf("%s %s %s", eventtype, reason, message)
}
}
func (f *FakeRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
f.Events = append(f.Events, fmt.Sprintf(eventtype+" "+reason+" "+messageFmt, args...))
if f.Events != nil {
f.Events <- fmt.Sprintf(eventtype+" "+reason+" "+messageFmt, args...)
}
}
func (f *FakeRecorder) PastEventf(object runtime.Object, timestamp unversioned.Time, eventtype, reason, messageFmt string, args ...interface{}) {
}
// 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),
}
}