mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-12-01 17:44:30 +00:00
fix image gc bug
This commit is contained in:
42
Godeps/_workspace/src/github.com/ssoroka/ttime/README.md
generated
vendored
Normal file
42
Godeps/_workspace/src/github.com/ssoroka/ttime/README.md
generated
vendored
Normal file
@@ -0,0 +1,42 @@
|
||||
This is an experiment in making time easier to mock in Go tests.
|
||||
|
||||
You should be able to alias the ttime library to time to avoid having to change all your time.Now() methods to ttime.Now() throughout your code.
|
||||
|
||||
All methods return actual time.Time structs (if they were supposed to).
|
||||
|
||||
example code:
|
||||
|
||||
import (
|
||||
time "github.com/ssoroka/ttime"
|
||||
)
|
||||
|
||||
fmt.Printf("The starting time is %v", time.Now().UTC())
|
||||
|
||||
// in test this will not sleep at all, but it will advance the clock 5 seconds.
|
||||
// in production, it's identical to time.Sleep
|
||||
time.Sleep(5 * time.Second)
|
||||
fmt.Printf("The time after sleeping for 5 seconds is %v", time.Now().UTC())
|
||||
|
||||
time.After(10 * time.Second, func() {
|
||||
// This will execute after 10 seconds in production and immediately in tests.
|
||||
fmt.Printf("It is now %v", time.Now().UTC())
|
||||
})
|
||||
|
||||
example test:
|
||||
|
||||
func TestFreezingTime(t *testing.T) {
|
||||
time.Freeze(time.Now()) // freeze the system clock, at least as far as ttime is concerned.
|
||||
|
||||
// or freeze time at a specific date/time (eg, test leap-year support!):
|
||||
now, err := time.Parse(time.RFC3339, "2012-02-29T00:00:00Z")
|
||||
if err != nil { panic("date time parse failed") }
|
||||
time.Freeze(now)
|
||||
defer time.Unfreeze()
|
||||
|
||||
// test leap-year-specific code
|
||||
if !isLeapYear(time.Now()) {
|
||||
t.Error("Oh no! isLeapYear is broken!")
|
||||
}
|
||||
|
||||
t.Logf("It is now %v", time.Now().UTC())
|
||||
}
|
||||
108
Godeps/_workspace/src/github.com/ssoroka/ttime/ttime.go
generated
vendored
Normal file
108
Godeps/_workspace/src/github.com/ssoroka/ttime/ttime.go
generated
vendored
Normal file
@@ -0,0 +1,108 @@
|
||||
package ttime
|
||||
|
||||
import "time"
|
||||
|
||||
var (
|
||||
currentTime time.Time
|
||||
timeFrozen bool
|
||||
)
|
||||
|
||||
type Duration time.Duration
|
||||
type Location time.Location
|
||||
type Month time.Month
|
||||
type ParseError time.ParseError
|
||||
type Ticker time.Ticker
|
||||
type Time time.Time
|
||||
type Timer time.Timer
|
||||
type Weekday time.Weekday
|
||||
|
||||
var (
|
||||
// import a ton of constants so we can act like the time library.
|
||||
Parse = time.Parse
|
||||
ParseDuration = time.ParseDuration
|
||||
Date = time.Date
|
||||
ParseInLocation = time.ParseInLocation
|
||||
FixedZone = time.FixedZone
|
||||
LoadLocation = time.LoadLocation
|
||||
Sunday = time.Sunday
|
||||
Monday = time.Monday
|
||||
Tuesday = time.Tuesday
|
||||
Wednesday = time.Wednesday
|
||||
Thursday = time.Thursday
|
||||
Friday = time.Friday
|
||||
Saturday = time.Saturday
|
||||
ANSIC = time.ANSIC
|
||||
UnixDate = time.UnixDate
|
||||
RubyDate = time.RubyDate
|
||||
RFC822 = time.RFC822
|
||||
RFC822Z = time.RFC822Z
|
||||
RFC850 = time.RFC850
|
||||
RFC1123 = time.RFC1123
|
||||
RFC1123Z = time.RFC1123Z
|
||||
RFC3339 = time.RFC3339
|
||||
RFC3339Nano = time.RFC3339Nano
|
||||
Kitchen = time.Kitchen
|
||||
Stamp = time.Stamp
|
||||
StampMilli = time.StampMilli
|
||||
StampMicro = time.StampMicro
|
||||
StampNano = time.StampNano
|
||||
// constants that I really should redefine:
|
||||
NewTimer = time.NewTimer
|
||||
NewTicker = time.NewTicker
|
||||
Unix = time.Unix
|
||||
)
|
||||
|
||||
func Freeze(t time.Time) {
|
||||
currentTime = t
|
||||
timeFrozen = true
|
||||
}
|
||||
|
||||
func Unfreeze() {
|
||||
timeFrozen = false
|
||||
}
|
||||
|
||||
func IsFrozen() bool {
|
||||
return timeFrozen
|
||||
}
|
||||
|
||||
func Now() time.Time {
|
||||
if timeFrozen {
|
||||
return currentTime
|
||||
} else {
|
||||
return time.Now()
|
||||
}
|
||||
}
|
||||
|
||||
func After(d time.Duration) <-chan time.Time {
|
||||
if timeFrozen {
|
||||
currentTime = currentTime.Add(d)
|
||||
c := make(chan time.Time, 1)
|
||||
c <- currentTime
|
||||
return c
|
||||
} else {
|
||||
return time.After(d)
|
||||
}
|
||||
}
|
||||
|
||||
func Tick(d time.Duration) <-chan time.Time {
|
||||
if timeFrozen {
|
||||
c := make(chan time.Time, 1)
|
||||
go func() {
|
||||
for {
|
||||
currentTime = currentTime.Add(d)
|
||||
c <- currentTime
|
||||
}
|
||||
}()
|
||||
return c
|
||||
} else {
|
||||
return time.Tick(d)
|
||||
}
|
||||
}
|
||||
|
||||
func Sleep(d time.Duration) {
|
||||
if timeFrozen && d > 0 {
|
||||
currentTime = currentTime.Add(d)
|
||||
} else {
|
||||
time.Sleep(d)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user