Merge pull request #46648 from caesarxuchao/fix-46631

Automatic merge from submit-queue (batch tested with PRs 46648, 46500, 46238, 46668, 46557)

Fix initializer_manager_test.go flake

Fixes https://github.com/kubernetes/kubernetes/issues/46631.

I reproduced the flake after 98 runs.

With the fix, it's not flaky in 1000 runs.
This commit is contained in:
Kubernetes Submit Queue
2017-06-02 15:20:42 -07:00
committed by GitHub
2 changed files with 22 additions and 24 deletions

View File

@@ -46,6 +46,8 @@ type poller struct {
// if the number of consecutive read failure equals or exceeds the failureThreshold , the
// configuration is regarded as not ready.
failureThreshold int
// number of consecutive failures so far.
failures int
// if the configuration is regarded as ready.
ready bool
mergedConfiguration runtime.Object
@@ -84,17 +86,18 @@ func (a *poller) setConfigurationAndReady(value runtime.Object) {
}
func (a *poller) Run(stopCh <-chan struct{}) {
var failure int
go wait.Until(func() {
configuration, err := a.get()
if err != nil {
failure++
if failure >= a.failureThreshold {
a.notReady()
}
return
}
failure = 0
a.setConfigurationAndReady(configuration)
}, a.interval, stopCh)
go wait.Until(a.sync, a.interval, stopCh)
}
func (a *poller) sync() {
configuration, err := a.get()
if err != nil {
a.failures++
if a.failures >= a.failureThreshold {
a.notReady()
}
return
}
a.failures = 0
a.setConfigurationAndReady(configuration)
}

View File

@@ -30,7 +30,6 @@ type mockLister struct {
invoked int
successes int
failures int
stopCh chan struct{}
configurationList v1alpha1.InitializerConfigurationList
t *testing.T
}
@@ -40,20 +39,15 @@ func newMockLister(successes, failures int, configurationList v1alpha1.Initializ
failures: failures,
successes: successes,
configurationList: configurationList,
stopCh: make(chan struct{}),
t: t,
}
}
// The first List will be successful; the next m.failures List will
// fail; the next m.successes List will be successful; the stopCh is closed at
// the 1+m.failures+m.successes call.
// fail; the next m.successes List will be successful
// List should only be called 1+m.failures+m.successes times.
func (m *mockLister) List(options metav1.ListOptions) (*v1alpha1.InitializerConfigurationList, error) {
m.invoked++
// m.successes could be 0, so call this `if` first.
if m.invoked == 1+m.failures+m.successes {
close(m.stopCh)
}
if m.invoked == 1 {
return &m.configurationList, nil
}
@@ -63,7 +57,7 @@ func (m *mockLister) List(options metav1.ListOptions) (*v1alpha1.InitializerConf
if m.invoked <= 1+m.failures+m.successes {
return &m.configurationList, nil
}
m.t.Fatalf("unexpected call to List, stopCh has been closed at the %d time call", 1+m.successes+m.failures)
m.t.Fatalf("unexpected call to List, should only be called %d times", 1+m.successes+m.failures)
return nil, nil
}
@@ -103,8 +97,9 @@ func TestConfiguration(t *testing.T) {
mock := newMockLister(c.successes, c.failures, v1alpha1.InitializerConfigurationList{}, t)
manager := NewInitializerConfigurationManager(mock)
manager.interval = 1 * time.Millisecond
manager.Run(mock.stopCh)
<-mock.stopCh
for i := 0; i < 1+c.successes+c.failures; i++ {
manager.sync()
}
_, err := manager.Initializers()
if err != nil && c.expectReady {
t.Errorf("case %s, expect ready, got: %v", c.name, err)