diff --git a/component/http/cache/cache.go b/component/http/cache/cache.go index 70f9bb523..48c22331e 100644 --- a/component/http/cache/cache.go +++ b/component/http/cache/cache.go @@ -44,13 +44,7 @@ func init() { metrics = newPrometheusMetrics() } -// TimeInstant is a timing function -// returns the current time instant of the system's clock -// by default it can be `time.Now().Unix()` , -// but for testing purposes we want to control the time -type TimeInstant func() int64 - -var Now TimeInstant = func() int64 { +var NowSeconds = func() int64 { return time.Now().Unix() } @@ -81,7 +75,7 @@ func cacheHandler(exec executor, rc *RouteCache) func(request *cacheHandlerReque return func(request *cacheHandlerRequest) (response *CacheHandlerResponse, e error) { - now := Now() + now := NowSeconds() key := request.getKey() diff --git a/component/http/cache/cache_test.go b/component/http/cache/cache_test.go index 1289e6662..323b42cb7 100644 --- a/component/http/cache/cache_test.go +++ b/component/http/cache/cache_test.go @@ -1080,7 +1080,7 @@ func TestCache_WithCacheGetErr(t *testing.T) { cacheImpl := &testingCache{ cache: make(map[string]testingCacheEntity), getErr: errors.New("get error"), - instant: Now, + instant: NowSeconds, } args := [][]testArgs{ @@ -1132,7 +1132,7 @@ func TestCache_WithCacheSetErr(t *testing.T) { cacheImpl := &testingCache{ cache: make(map[string]testingCacheEntity), setErr: errors.New("set error"), - instant: Now, + instant: NowSeconds, } args := [][]testArgs{ @@ -1682,7 +1682,7 @@ func assertCache(t *testing.T, args [][]testArgs) { } } - Now = func() int64 { + NowSeconds = func() int64 { return arg.requestParams.timeInstance } @@ -1750,7 +1750,7 @@ type testingCache struct { setCount int getErr error setErr error - instant TimeInstant + instant func() int64 } func newTestingCache() *testingCache { diff --git a/component/http/route_cache_test.go b/component/http/route_cache_test.go index 54e71eb3a..c15426adc 100644 --- a/component/http/route_cache_test.go +++ b/component/http/route_cache_test.go @@ -45,7 +45,7 @@ func TestCachingMiddleware(t *testing.T) { } testingCache := newTestingCache() - testingCache.instant = httpcache.Now + testingCache.instant = httpcache.NowSeconds cache, errs := httpcache.NewRouteCache(testingCache, httpcache.Age{Max: 1 * time.Second}) assert.Empty(t, errs) @@ -157,7 +157,7 @@ func assertRouteBuilder(t *testing.T, arg arg, routeBuilder *RouteBuilder, cache func TestRouteCacheImplementation_WithSingleRequest(t *testing.T) { cc := newTestingCache() - cc.instant = httpcache.Now + cc.instant = httpcache.NowSeconds var executions uint32 @@ -226,7 +226,7 @@ func TestRouteCacheImplementation_WithSingleRequest(t *testing.T) { func TestRouteCacheAsMiddleware_WithSingleRequest(t *testing.T) { cc := newTestingCache() - cc.instant = httpcache.Now + cc.instant = httpcache.NowSeconds var executions uint32 @@ -318,7 +318,7 @@ func newMiddlewareWrapper(middlewareFunc func(w http.ResponseWriter, r *http.Req func TestRawRouteCacheImplementation_WithSingleRequest(t *testing.T) { cc := newTestingCache() - cc.instant = httpcache.Now + cc.instant = httpcache.NowSeconds var executions uint32 @@ -485,7 +485,7 @@ type testingCache struct { setCount int getErr error setErr error - instant httpcache.TimeInstant + instant func() int64 } func newTestingCache() *testingCache {