mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 02:33:34 +00:00
vendor update for CSI 0.3.0
This commit is contained in:
56
vendor/k8s.io/utils/clock/testing/fake_clock.go
generated
vendored
56
vendor/k8s.io/utils/clock/testing/fake_clock.go
generated
vendored
@ -34,7 +34,7 @@ type FakeClock struct {
|
||||
time time.Time
|
||||
|
||||
// waiters are waiting for the fake time to pass their specified time
|
||||
waiters []fakeClockWaiter
|
||||
waiters []*fakeClockWaiter
|
||||
}
|
||||
|
||||
type fakeClockWaiter struct {
|
||||
@ -45,6 +45,7 @@ type fakeClockWaiter struct {
|
||||
fired bool
|
||||
}
|
||||
|
||||
// NewFakeClock constructs a fake clock set to the provided time.
|
||||
func NewFakeClock(t time.Time) *FakeClock {
|
||||
return &FakeClock{
|
||||
time: t,
|
||||
@ -65,20 +66,20 @@ func (f *FakeClock) Since(ts time.Time) time.Duration {
|
||||
return f.time.Sub(ts)
|
||||
}
|
||||
|
||||
// Fake version of time.After(d).
|
||||
// After is the fake version of time.After(d).
|
||||
func (f *FakeClock) After(d time.Duration) <-chan time.Time {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
stopTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // Don't block!
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
f.waiters = append(f.waiters, &fakeClockWaiter{
|
||||
targetTime: stopTime,
|
||||
destChan: ch,
|
||||
})
|
||||
return ch
|
||||
}
|
||||
|
||||
// Fake version of time.NewTimer(d).
|
||||
// NewTimer constructs a fake timer, akin to time.NewTimer(d).
|
||||
func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
@ -91,16 +92,20 @@ func (f *FakeClock) NewTimer(d time.Duration) clock.Timer {
|
||||
destChan: ch,
|
||||
},
|
||||
}
|
||||
f.waiters = append(f.waiters, timer.waiter)
|
||||
f.waiters = append(f.waiters, &timer.waiter)
|
||||
return timer
|
||||
}
|
||||
|
||||
// Tick constructs a fake ticker, akin to time.Tick
|
||||
func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
|
||||
if d <= 0 {
|
||||
return nil
|
||||
}
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
tickTime := f.time.Add(d)
|
||||
ch := make(chan time.Time, 1) // hold one tick
|
||||
f.waiters = append(f.waiters, fakeClockWaiter{
|
||||
f.waiters = append(f.waiters, &fakeClockWaiter{
|
||||
targetTime: tickTime,
|
||||
stepInterval: d,
|
||||
skipIfBlocked: true,
|
||||
@ -110,14 +115,15 @@ func (f *FakeClock) Tick(d time.Duration) <-chan time.Time {
|
||||
return ch
|
||||
}
|
||||
|
||||
// Move clock by Duration, notify anyone that's called After, Tick, or NewTimer
|
||||
// Step moves the clock by Duration and notifies anyone that's called After,
|
||||
// Tick, or NewTimer.
|
||||
func (f *FakeClock) Step(d time.Duration) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
f.setTimeLocked(f.time.Add(d))
|
||||
}
|
||||
|
||||
// Sets the time.
|
||||
// SetTime sets the time.
|
||||
func (f *FakeClock) SetTime(t time.Time) {
|
||||
f.lock.Lock()
|
||||
defer f.lock.Unlock()
|
||||
@ -127,9 +133,9 @@ func (f *FakeClock) SetTime(t time.Time) {
|
||||
// Actually changes the time and checks any waiters. f must be write-locked.
|
||||
func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
f.time = t
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(f.waiters))
|
||||
newWaiters := make([]*fakeClockWaiter, 0, len(f.waiters))
|
||||
for i := range f.waiters {
|
||||
w := &f.waiters[i]
|
||||
w := f.waiters[i]
|
||||
if !w.targetTime.After(t) {
|
||||
|
||||
if w.skipIfBlocked {
|
||||
@ -147,7 +153,7 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
for !w.targetTime.After(t) {
|
||||
w.targetTime = w.targetTime.Add(w.stepInterval)
|
||||
}
|
||||
newWaiters = append(newWaiters, *w)
|
||||
newWaiters = append(newWaiters, w)
|
||||
}
|
||||
|
||||
} else {
|
||||
@ -157,7 +163,7 @@ func (f *FakeClock) setTimeLocked(t time.Time) {
|
||||
f.waiters = newWaiters
|
||||
}
|
||||
|
||||
// Returns true if After has been called on f but not yet satisfied (so you can
|
||||
// HasWaiters returns true if After has been called on f but not yet satisfied (so you can
|
||||
// write race-free tests).
|
||||
func (f *FakeClock) HasWaiters() bool {
|
||||
f.lock.RLock()
|
||||
@ -165,6 +171,7 @@ func (f *FakeClock) HasWaiters() bool {
|
||||
return len(f.waiters) > 0
|
||||
}
|
||||
|
||||
// Sleep is akin to time.Sleep
|
||||
func (f *FakeClock) Sleep(d time.Duration) {
|
||||
f.Step(d)
|
||||
}
|
||||
@ -186,24 +193,25 @@ func (i *IntervalClock) Since(ts time.Time) time.Duration {
|
||||
return i.Time.Sub(ts)
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// After is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) After(d time.Duration) <-chan time.Time {
|
||||
panic("IntervalClock doesn't implement After")
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// NewTimer is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) NewTimer(d time.Duration) clock.Timer {
|
||||
panic("IntervalClock doesn't implement NewTimer")
|
||||
}
|
||||
|
||||
// Unimplemented, will panic.
|
||||
// Tick is unimplemented, will panic.
|
||||
// TODO: make interval clock use FakeClock so this can be implemented.
|
||||
func (*IntervalClock) Tick(d time.Duration) <-chan time.Time {
|
||||
panic("IntervalClock doesn't implement Tick")
|
||||
}
|
||||
|
||||
// Sleep is unimplemented, will panic.
|
||||
func (*IntervalClock) Sleep(d time.Duration) {
|
||||
panic("IntervalClock doesn't implement Sleep")
|
||||
}
|
||||
@ -226,11 +234,11 @@ func (f *fakeTimer) Stop() bool {
|
||||
f.fakeClock.lock.Lock()
|
||||
defer f.fakeClock.lock.Unlock()
|
||||
|
||||
newWaiters := make([]fakeClockWaiter, 0, len(f.fakeClock.waiters))
|
||||
newWaiters := make([]*fakeClockWaiter, 0, len(f.fakeClock.waiters))
|
||||
for i := range f.fakeClock.waiters {
|
||||
w := &f.fakeClock.waiters[i]
|
||||
w := f.fakeClock.waiters[i]
|
||||
if w != &f.waiter {
|
||||
newWaiters = append(newWaiters, *w)
|
||||
newWaiters = append(newWaiters, w)
|
||||
}
|
||||
}
|
||||
|
||||
@ -250,5 +258,17 @@ func (f *fakeTimer) Reset(d time.Duration) bool {
|
||||
f.waiter.fired = false
|
||||
f.waiter.targetTime = f.fakeClock.time.Add(d)
|
||||
|
||||
var isWaiting bool
|
||||
for i := range f.fakeClock.waiters {
|
||||
w := f.fakeClock.waiters[i]
|
||||
if w == &f.waiter {
|
||||
isWaiting = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isWaiting {
|
||||
f.fakeClock.waiters = append(f.fakeClock.waiters, &f.waiter)
|
||||
}
|
||||
|
||||
return active
|
||||
}
|
||||
|
90
vendor/k8s.io/utils/clock/testing/fake_clock_test.go
generated
vendored
90
vendor/k8s.io/utils/clock/testing/fake_clock_test.go
generated
vendored
@ -182,3 +182,93 @@ func TestFakeTick(t *testing.T) {
|
||||
t.Errorf("unexpected number of accumulated ticks: %d", accumulatedTicks)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFakeStop(t *testing.T) {
|
||||
tc := NewFakeClock(time.Now())
|
||||
timer := tc.NewTimer(time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
timer.Stop()
|
||||
if tc.HasWaiters() {
|
||||
t.Errorf("expected existing waiter to be cleaned up, but it is still present")
|
||||
}
|
||||
}
|
||||
|
||||
// This tests the pattern documented in the go docs here: https://golang.org/pkg/time/#Timer.Stop
|
||||
// This pattern is required to safely reset a timer, so should be common.
|
||||
// This also tests resetting the timer
|
||||
func TestFakeStopDrain(t *testing.T) {
|
||||
start := time.Time{}
|
||||
tc := NewFakeClock(start)
|
||||
timer := tc.NewTimer(time.Second)
|
||||
tc.Step(1 * time.Second)
|
||||
// Effectively `if !timer.Stop { <-t.C }` but with more asserts
|
||||
if timer.Stop() {
|
||||
t.Errorf("stop should report the timer had triggered")
|
||||
}
|
||||
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(1 * time.Second)) {
|
||||
t.Errorf("timer should have ticked after 1 second, got %v", readTime)
|
||||
}
|
||||
|
||||
timer.Reset(time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
select {
|
||||
case <-timer.C():
|
||||
t.Fatal("got time early on clock; haven't stepped yet")
|
||||
default:
|
||||
}
|
||||
tc.Step(1 * time.Second)
|
||||
if readTime := assertReadTime(t, timer.C()); !readTime.Equal(start.Add(2 * time.Second)) {
|
||||
t.Errorf("timer should have ticked again after reset + 1 more second, got %v", readTime)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimerNegative(t *testing.T) {
|
||||
tc := NewFakeClock(time.Now())
|
||||
timer := tc.NewTimer(-1 * time.Second)
|
||||
if !tc.HasWaiters() {
|
||||
t.Errorf("expected a waiter to be present, but it is not")
|
||||
}
|
||||
// force waiters to be called
|
||||
tc.Step(0)
|
||||
tick := assertReadTime(t, timer.C())
|
||||
if tick != tc.Now() {
|
||||
t.Errorf("expected -1s to turn into now: %v != %v", tick, tc.Now())
|
||||
}
|
||||
}
|
||||
|
||||
func TestTickNegative(t *testing.T) {
|
||||
// The stdlib 'Tick' returns nil for negative and zero values, so our fake
|
||||
// should too.
|
||||
tc := NewFakeClock(time.Now())
|
||||
if tick := tc.Tick(-1 * time.Second); tick != nil {
|
||||
t.Errorf("expected negative tick to be nil: %v", tick)
|
||||
}
|
||||
if tick := tc.Tick(0); tick != nil {
|
||||
t.Errorf("expected negative tick to be nil: %v", tick)
|
||||
}
|
||||
}
|
||||
|
||||
// assertReadTime asserts that the channel can be read and returns the time it
|
||||
// reads from the channel.
|
||||
func assertReadTime(t testing.TB, c <-chan time.Time) time.Time {
|
||||
type helper interface {
|
||||
Helper()
|
||||
}
|
||||
if h, ok := t.(helper); ok {
|
||||
h.Helper()
|
||||
}
|
||||
select {
|
||||
case ti, ok := <-c:
|
||||
if !ok {
|
||||
t.Fatalf("expected to read time from channel, but it was closed")
|
||||
}
|
||||
return ti
|
||||
default:
|
||||
t.Fatalf("expected to read time from channel, but couldn't")
|
||||
}
|
||||
panic("unreachable")
|
||||
}
|
||||
|
Reference in New Issue
Block a user