Skip to content

Commit

Permalink
Merge pull request #371 from tylerferrara/wrapper
Browse files Browse the repository at this point in the history
Created a timewrapper package to mock time functions
  • Loading branch information
k8s-ci-robot committed Jul 28, 2021
2 parents e283fb6 + d95b7b0 commit 1cb2002
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
17 changes: 9 additions & 8 deletions legacy/reqcounter/reqcounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/sirupsen/logrus"
tw "sigs.k8s.io/k8s-container-image-promoter/legacy/timewrapper"
)

// RequestCounter records the number of HTTP requests to GCR.
Expand All @@ -48,7 +49,7 @@ func (rc *RequestCounter) Flush() {
defer rc.Mutex.Unlock()

// Log the number of requests within this measurement window.
msg := fmt.Sprintf("From %s to %s [%d min] there have been %d requests to GCR.", rc.Since.Format(timestampFormat), time.Now().Format(timestampFormat), rc.Interval/time.Minute, rc.Requests)
msg := fmt.Sprintf("From %s to %s [%d min] there have been %d requests to GCR.", rc.Since.Format(TimestampFormat), Clock.Now().Format(TimestampFormat), rc.Interval/time.Minute, rc.Requests)
Debug(msg)

// Reset the request counter.
Expand All @@ -60,14 +61,14 @@ func (rc *RequestCounter) Flush() {
// if it must be reset.
func (rc *RequestCounter) reset() {
rc.Requests = 0
rc.Since = time.Now()
rc.Since = Clock.Now()
}

// watch continuously logs the request counter at the specified intervals.
func (rc *RequestCounter) watch() {
go func() {
for {
Sleep(rc.Interval)
Clock.Sleep(rc.Interval)
rc.Flush()
}
}()
Expand Down Expand Up @@ -104,8 +105,8 @@ const (
// requests counters to perfectly line up with the actual GCR quota.
// Source: https://cloud.google.com/container-registry/quotas
MeasurementWindow time.Duration = time.Minute * 10
// timestampFormat specifies the syntax for logging time stamps of request counters.
timestampFormat string = "2006-01-02 15:04:05"
// TimestampFormat specifies the syntax for logging time stamps of request counters.
TimestampFormat string = "2006-01-02 15:04:05"
)

var (
Expand All @@ -116,8 +117,8 @@ var (
NetMonitor *NetworkMonitor
// Debug is defined to simplify testing of logrus.Debug calls.
Debug func(args ...interface{}) = logrus.Debug
// Sleep is defined to allow mocking of the time.Sleep function in tests.
Sleep func(d time.Duration) = time.Sleep
// Clock is defined to allow mocking of time functions.
Clock tw.Time = tw.RealTime{}
)

// Init allows request counting to begin.
Expand All @@ -129,7 +130,7 @@ func Init() {
requestCounter := &RequestCounter{
Mutex: sync.Mutex{},
Requests: 0,
Since: time.Now(),
Since: Clock.Now(),
Interval: MeasurementWindow,
}

Expand Down
47 changes: 47 additions & 0 deletions legacy/timewrapper/timewrapper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package timewrapper

import (
"time"
)

// Time groups the functions mocked by FakeTime.
type Time interface {
Now() time.Time
Sleep(d time.Duration)
}

// RealTime is a wrapper for actual time functions.
type RealTime struct{}

// Now simply calls time.Now()
func (RealTime) Now() time.Time { return time.Now() }

// Sleep simply calls time.Sleep(d), using the given duration.
func (RealTime) Sleep(d time.Duration) { time.Sleep(d) }

// FakeTime holds the global fake time.
type FakeTime struct {
Time time.Time
}

// Now returns the global fake time.
func (ft FakeTime) Now() time.Time { return ft.Time }

// Sleep does not block the current goroutine.
func (ft FakeTime) Sleep(d time.Duration) {}

0 comments on commit 1cb2002

Please sign in to comment.