-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: propagate time through context values (#5858)
Signed-off-by: knqyf263 <knqyf263@gmail.com>
- Loading branch information
Showing
77 changed files
with
384 additions
and
546 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,33 @@ | ||
package clock | ||
|
||
import ( | ||
"testing" | ||
"context" | ||
"time" | ||
|
||
"k8s.io/utils/clock" | ||
clocktesting "k8s.io/utils/clock/testing" | ||
) | ||
|
||
var c clock.Clock = clock.RealClock{} | ||
// clockKey is the context key for clock. It is unexported to prevent collisions with context keys defined in | ||
// other packages. | ||
type clockKey struct{} | ||
|
||
// SetFakeTime sets a fake time for testing. | ||
func SetFakeTime(t *testing.T, fakeTime time.Time) { | ||
c = clocktesting.NewFakeClock(fakeTime) | ||
t.Cleanup(func() { | ||
c = clock.RealClock{} | ||
}) | ||
// With returns a new context with the given time. | ||
func With(ctx context.Context, t time.Time) context.Context { | ||
c := clocktesting.NewFakeClock(t) | ||
return context.WithValue(ctx, clockKey{}, c) | ||
} | ||
|
||
func Now() time.Time { | ||
return c.Now() | ||
// Now returns the current time. | ||
func Now(ctx context.Context) time.Time { | ||
return Clock(ctx).Now() | ||
} | ||
|
||
// Clock returns the clock from the context. | ||
func Clock(ctx context.Context) clock.Clock { | ||
t, ok := ctx.Value(clockKey{}).(clock.Clock) | ||
if !ok { | ||
return clock.RealClock{} | ||
} | ||
return t | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.