diff options
author | Martin Czygan <martin.czygan@gmail.com> | 2021-11-23 22:53:55 +0100 |
---|---|---|
committer | Martin Czygan <martin.czygan@gmail.com> | 2021-11-23 22:53:55 +0100 |
commit | 14e63308eee071fd67de31638aaa2aa7f05e4a6f (patch) | |
tree | a3749342ea29568a52c72a9cda2954f828e87905 /skate | |
parent | ea01471c88e941d3686d07b81a4ff6a9c09862f1 (diff) | |
download | refcat-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')
-rw-r--r-- | skate/go.mod | 1 | ||||
-rw-r--r-- | skate/go.sum | 2 | ||||
-rw-r--r-- | skate/reduce.go | 13 | ||||
-rw-r--r-- | skate/reduce_test.go | 10 |
4 files changed, 13 insertions, 13 deletions
diff --git a/skate/go.mod b/skate/go.mod index 69383ef..5e2f5ac 100644 --- a/skate/go.mod +++ b/skate/go.mod @@ -4,6 +4,7 @@ go 1.15 require ( github.com/adrg/xdg v0.3.3 + github.com/benbjohnson/clock v1.3.0 github.com/klauspost/compress v1.13.1 github.com/klauspost/cpuid/v2 v2.0.6 // indirect github.com/kr/pretty v0.1.0 diff --git a/skate/go.sum b/skate/go.sum index 0e176b0..668126f 100644 --- a/skate/go.sum +++ b/skate/go.sum @@ -1,5 +1,7 @@ github.com/adrg/xdg v0.3.3 h1:s/tV7MdqQnzB1nKY8aqHvAMD+uCiuEDzVB5HLRY849U= github.com/adrg/xdg v0.3.3/go.mod h1:61xAR2VZcggl2St4O9ohF5qCKe08+JDmE4VNzPFQvOQ= +github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= +github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= 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 diff --git a/skate/reduce_test.go b/skate/reduce_test.go index 276dcc7..2da5218 100644 --- a/skate/reduce_test.go +++ b/skate/reduce_test.go @@ -4,8 +4,8 @@ import ( "bytes" "reflect" "testing" - "time" + "github.com/benbjohnson/clock" "github.com/kr/pretty" "gitlab.com/internetarchive/refcat/skate/testutil" "gitlab.com/internetarchive/refcat/skate/xio" @@ -404,9 +404,7 @@ func TestZippyExact(t *testing.T) { err: nil, }, } - timeNow = func() time.Time { - return time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) - } + T = clock.NewMock() for i, c := range cases { a, b, err := xio.OpenTwo(c.a, c.b) if err != nil { @@ -453,9 +451,7 @@ func TestZippyBrefAugment(t *testing.T) { err: nil, }, } - timeNow = func() time.Time { - return time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC) - } + T = clock.NewMock() for i, c := range cases { a, b, err := xio.OpenTwo(c.a, c.b) if err != nil { |