From 14e63308eee071fd67de31638aaa2aa7f05e4a6f Mon Sep 17 00:00:00 2001 From: Martin Czygan Date: Tue, 23 Nov 2021 22:53:55 +0100 Subject: reduce: use mockable time While basically the same, we save a bit with a default mock and prepare a bit better for some future encapsulation. --- skate/reduce.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'skate/reduce.go') diff --git a/skate/reduce.go b/skate/reduce.go index 031e9bc..4394f14 100644 --- a/skate/reduce.go +++ b/skate/reduce.go @@ -44,6 +44,7 @@ import ( "strings" "time" + "github.com/benbjohnson/clock" "github.com/segmentio/encoding/json" "gitlab.com/internetarchive/refcat/skate/set" "gitlab.com/internetarchive/refcat/skate/xio" @@ -51,8 +52,8 @@ import ( ) var ( - // timeNow is used for mocking time in tests. - timeNow = time.Now + // a mockable time, here on package level (todo: more encapsulation) + T clock.Clock = clock.New() // a few sane dates to accept minDate = 1500 maxDate = time.Now().AddDate(5, 0, 0).Year() @@ -69,7 +70,7 @@ func groupLogf(g *zipkey.Group, s string, vs ...interface{}) { // match result, e.g. for doi matches. func ZippyExact(releases, refs io.Reader, matchResult MatchResult, w io.Writer) error { var ( - ts = timeNow().UTC().Format(time.RFC3339) + ts = T.Now().UTC().Format(time.RFC3339) enc = json.NewEncoder(xio.NewSyncWriter(w)) keyer = makeKeyFunc("\t", 1) grouper = func(g *zipkey.Group) error { @@ -385,7 +386,7 @@ func ZippyVerifyRefsOpenLibrary(olr, refs io.Reader, w io.Writer) error { // write a bref document for each match. func ZippyWayback(refs, cdx io.Reader, w io.Writer) error { var ( - ts = timeNow().UTC().Format(time.RFC3339) + ts = T.Now().UTC().Format(time.RFC3339) enc = json.NewEncoder(xio.NewSyncWriter(w)) keyer = makeKeyFunc("\t", 1) grouper = func(g *zipkey.Group) error { @@ -568,7 +569,7 @@ func deduplicateBrefs(brefs []*BiblioRef) []*BiblioRef { func matchedRefsExtend(matched []*BiblioRef, refs []*Ref, stats *statsAugment) []*BiblioRef { var ( seen = set.New() // store "key + index" of matched items - ts = timeNow().UTC().Format(time.RFC3339) + ts = T.Now().UTC().Format(time.RFC3339) ) for _, m := range matched { s := m.RefKey + fmt.Sprintf("%d", m.RefIndex) @@ -646,7 +647,7 @@ func matchedRefsExtend(matched []*BiblioRef, refs []*Ref, stats *statsAugment) [ func uniqueMatches(docs []string, stats *statsAugment) (result []*BiblioRef, err error) { var ( brefs []*BiblioRef - ts = timeNow().UTC().Format(time.RFC3339) + ts = T.Now().UTC().Format(time.RFC3339) ) for _, doc := range docs { var bref BiblioRef -- cgit v1.2.3