mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 09:22:44 +00:00
Try to fix flaky test in record/event
The error messages were inconsistent with what was actually being tested in regards to timestamps being equal or not.
This commit is contained in:
parent
ad243edaa3
commit
71ca503d30
@ -21,6 +21,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api"
|
"k8s.io/kubernetes/pkg/api"
|
||||||
@ -271,39 +272,41 @@ func TestEventf(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range table {
|
for _, item := range table {
|
||||||
called := make(chan struct{})
|
var wg sync.WaitGroup
|
||||||
|
// We expect only one callback
|
||||||
|
wg.Add(1)
|
||||||
testEvents := testEventSink{
|
testEvents := testEventSink{
|
||||||
OnCreate: func(event *api.Event) (*api.Event, error) {
|
OnCreate: func(event *api.Event) (*api.Event, error) {
|
||||||
|
defer wg.Done()
|
||||||
returnEvent, _ := validateEvent(event, item.expect, t)
|
returnEvent, _ := validateEvent(event, item.expect, t)
|
||||||
if item.expectUpdate {
|
if item.expectUpdate {
|
||||||
t.Errorf("Expected event update(), got event create()")
|
t.Errorf("Expected event update(), got event create()")
|
||||||
}
|
}
|
||||||
called <- struct{}{}
|
|
||||||
return returnEvent, nil
|
return returnEvent, nil
|
||||||
},
|
},
|
||||||
OnUpdate: func(event *api.Event) (*api.Event, error) {
|
OnUpdate: func(event *api.Event) (*api.Event, error) {
|
||||||
|
defer wg.Done()
|
||||||
returnEvent, _ := validateEvent(event, item.expect, t)
|
returnEvent, _ := validateEvent(event, item.expect, t)
|
||||||
if !item.expectUpdate {
|
if !item.expectUpdate {
|
||||||
t.Errorf("Expected event create(), got event update()")
|
t.Errorf("Expected event create(), got event update()")
|
||||||
}
|
}
|
||||||
called <- struct{}{}
|
|
||||||
return returnEvent, nil
|
return returnEvent, nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
eventBroadcaster := NewBroadcaster()
|
eventBroadcaster := NewBroadcaster()
|
||||||
sinkWatcher := eventBroadcaster.StartRecordingToSink(&testEvents)
|
sinkWatcher := eventBroadcaster.StartRecordingToSink(&testEvents)
|
||||||
logWatcher1 := eventBroadcaster.StartLogging(t.Logf) // Prove that it is useful
|
logWatcher1 := eventBroadcaster.StartLogging(t.Logf) // Prove that it is useful
|
||||||
|
wg.Add(1)
|
||||||
logWatcher2 := eventBroadcaster.StartLogging(func(formatter string, args ...interface{}) {
|
logWatcher2 := eventBroadcaster.StartLogging(func(formatter string, args ...interface{}) {
|
||||||
|
defer wg.Done()
|
||||||
if e, a := item.expectLog, fmt.Sprintf(formatter, args...); e != a {
|
if e, a := item.expectLog, fmt.Sprintf(formatter, args...); e != a {
|
||||||
t.Errorf("Expected '%v', got '%v'", e, a)
|
t.Errorf("Expected '%v', got '%v'", e, a)
|
||||||
}
|
}
|
||||||
called <- struct{}{}
|
|
||||||
})
|
})
|
||||||
recorder := eventBroadcaster.NewRecorder(api.EventSource{Component: "eventTest"})
|
recorder := eventBroadcaster.NewRecorder(api.EventSource{Component: "eventTest"})
|
||||||
recorder.Eventf(item.obj, item.reason, item.messageFmt, item.elements...)
|
recorder.Eventf(item.obj, item.reason, item.messageFmt, item.elements...)
|
||||||
|
|
||||||
<-called
|
wg.Wait()
|
||||||
<-called
|
|
||||||
sinkWatcher.Stop()
|
sinkWatcher.Stop()
|
||||||
logWatcher1.Stop()
|
logWatcher1.Stop()
|
||||||
logWatcher2.Stop()
|
logWatcher2.Stop()
|
||||||
@ -316,17 +319,17 @@ func validateEvent(actualEvent *api.Event, expectedEvent *api.Event, t *testing.
|
|||||||
if actualEvent.FirstTimestamp.IsZero() || actualEvent.LastTimestamp.IsZero() {
|
if actualEvent.FirstTimestamp.IsZero() || actualEvent.LastTimestamp.IsZero() {
|
||||||
t.Errorf("timestamp wasn't set: %#v", *actualEvent)
|
t.Errorf("timestamp wasn't set: %#v", *actualEvent)
|
||||||
}
|
}
|
||||||
if actualEvent.FirstTimestamp.Equal(actualEvent.LastTimestamp) {
|
|
||||||
if expectCompression {
|
|
||||||
t.Errorf("FirstTimestamp (%q) and LastTimestamp (%q) must be equal to indicate only one occurrence of the event, but were different. Actual Event: %#v", actualEvent.FirstTimestamp, actualEvent.LastTimestamp, *actualEvent)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if !expectCompression {
|
|
||||||
t.Errorf("FirstTimestamp (%q) and LastTimestamp (%q) must be different to indicate event compression happened, but were the same. Actual Event: %#v", actualEvent.FirstTimestamp, actualEvent.LastTimestamp, *actualEvent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
actualFirstTimestamp := actualEvent.FirstTimestamp
|
actualFirstTimestamp := actualEvent.FirstTimestamp
|
||||||
actualLastTimestamp := actualEvent.LastTimestamp
|
actualLastTimestamp := actualEvent.LastTimestamp
|
||||||
|
if actualFirstTimestamp.Equal(actualLastTimestamp) {
|
||||||
|
if expectCompression {
|
||||||
|
t.Errorf("FirstTimestamp (%q) and LastTimestamp (%q) must be different to indicate event compression happened, but were the same. Actual Event: %#v", actualFirstTimestamp, actualLastTimestamp, *actualEvent)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if expectedEvent.Count == 1 {
|
||||||
|
t.Errorf("FirstTimestamp (%q) and LastTimestamp (%q) must be equal to indicate only one occurrence of the event, but were different. Actual Event: %#v", actualFirstTimestamp, actualLastTimestamp, *actualEvent)
|
||||||
|
}
|
||||||
|
}
|
||||||
// Temp clear time stamps for comparison because actual values don't matter for comparison
|
// Temp clear time stamps for comparison because actual values don't matter for comparison
|
||||||
actualEvent.FirstTimestamp = expectedEvent.FirstTimestamp
|
actualEvent.FirstTimestamp = expectedEvent.FirstTimestamp
|
||||||
actualEvent.LastTimestamp = expectedEvent.LastTimestamp
|
actualEvent.LastTimestamp = expectedEvent.LastTimestamp
|
||||||
|
Loading…
Reference in New Issue
Block a user