mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-20 17:16:18 +00:00
Merge pull request #34139 from liggitt/fix-jitter-until
Automatic merge from submit-queue Fix wait.JitterUntil https://github.com/kubernetes/kubernetes/pull/29743 changed a util method to cause process exits if a handler function panics. Utility methods should not make process exit decisions. If a process (like the controller manager) wants to exit on panic, appending a panic handler or setting `ReallyCrash = true` is the right way to do that (discussed [here](https://github.com/kubernetes/kubernetes/pull/29743#r75509074)). This restores the documented behavior of wait.JitterUntil
This commit is contained in:
commit
808ed6bfd0
@ -20,6 +20,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/util/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// For any test of the style:
|
// For any test of the style:
|
||||||
@ -81,6 +83,7 @@ func JitterUntil(f func(), period time.Duration, jitterFactor float64, sliding b
|
|||||||
}
|
}
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
|
defer runtime.HandleCrash()
|
||||||
f()
|
f()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
@ -23,6 +23,8 @@ import (
|
|||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"k8s.io/kubernetes/pkg/util/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUntil(t *testing.T) {
|
func TestUntil(t *testing.T) {
|
||||||
@ -109,6 +111,41 @@ func TestJitterUntilReturnsImmediately(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestJitterUntilRecoversPanic(t *testing.T) {
|
||||||
|
// Save and restore crash handlers
|
||||||
|
originalReallyCrash := runtime.ReallyCrash
|
||||||
|
originalHandlers := runtime.PanicHandlers
|
||||||
|
defer func() {
|
||||||
|
runtime.ReallyCrash = originalReallyCrash
|
||||||
|
runtime.PanicHandlers = originalHandlers
|
||||||
|
}()
|
||||||
|
|
||||||
|
called := 0
|
||||||
|
handled := 0
|
||||||
|
|
||||||
|
// Hook up a custom crash handler to ensure it is called when a jitter function panics
|
||||||
|
runtime.ReallyCrash = false
|
||||||
|
runtime.PanicHandlers = []func(interface{}){
|
||||||
|
func(p interface{}) {
|
||||||
|
handled++
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
ch := make(chan struct{})
|
||||||
|
JitterUntil(func() {
|
||||||
|
called++
|
||||||
|
if called > 2 {
|
||||||
|
close(ch)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
panic("TestJitterUntilRecoversPanic")
|
||||||
|
}, time.Millisecond, 1.0, true, ch)
|
||||||
|
|
||||||
|
if called != 3 {
|
||||||
|
t.Errorf("Expected panic recovers")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestJitterUntilNegativeFactor(t *testing.T) {
|
func TestJitterUntilNegativeFactor(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
ch := make(chan struct{})
|
ch := make(chan struct{})
|
||||||
|
Loading…
Reference in New Issue
Block a user