aboutsummaryrefslogtreecommitdiffstats
path: root/skate/reduce.go
diff options
context:
space:
mode:
authorMartin Czygan <martin.czygan@gmail.com>2021-11-23 22:53:55 +0100
committerMartin Czygan <martin.czygan@gmail.com>2021-11-23 22:53:55 +0100
commit14e63308eee071fd67de31638aaa2aa7f05e4a6f (patch)
treea3749342ea29568a52c72a9cda2954f828e87905 /skate/reduce.go
parentea01471c88e941d3686d07b81a4ff6a9c09862f1 (diff)
downloadrefcat-14e63308eee071fd67de31638aaa2aa7f05e4a6f.tar.gz
refcat-14e63308eee071fd67de31638aaa2aa7f05e4a6f.zip
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.
Diffstat (limited to 'skate/reduce.go')
-rw-r--r--skate/reduce.go13
1 files changed, 7 insertions, 6 deletions
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