Skip to content

Commit

Permalink
beatlabs#128 remove timeinstant abstraction
Browse files Browse the repository at this point in the history
Signed-off-by: Vangelis Katikaridis <[email protected]>
  • Loading branch information
drakos74 committed May 9, 2020
1 parent e68d2a3 commit 45c2316
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
10 changes: 2 additions & 8 deletions component/http/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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()

Expand Down
8 changes: 4 additions & 4 deletions component/http/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -1682,7 +1682,7 @@ func assertCache(t *testing.T, args [][]testArgs) {
}
}

Now = func() int64 {
NowSeconds = func() int64 {
return arg.requestParams.timeInstance
}

Expand Down Expand Up @@ -1750,7 +1750,7 @@ type testingCache struct {
setCount int
getErr error
setErr error
instant TimeInstant
instant func() int64
}

func newTestingCache() *testingCache {
Expand Down
10 changes: 5 additions & 5 deletions component/http/route_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -485,7 +485,7 @@ type testingCache struct {
setCount int
getErr error
setErr error
instant httpcache.TimeInstant
instant func() int64
}

func newTestingCache() *testingCache {
Expand Down

0 comments on commit 45c2316

Please sign in to comment.