From 30cdfab1faca93c3c14024a14c6f0efd7b7c064d Mon Sep 17 00:00:00 2001 From: Rosie Kern Date: Fri, 1 Nov 2024 15:55:19 +0000 Subject: [PATCH] Delint and fix tests --- stat/paths.go | 32 +++++++++++++++++++++++--------- stat/paths_test.go | 13 ++++++++++--- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/stat/paths.go b/stat/paths.go index 1d34a9d5..6e04dee9 100644 --- a/stat/paths.go +++ b/stat/paths.go @@ -123,35 +123,49 @@ func (p *Paths) Scan(paths io.Reader) error { endTime := time.Now().Add(p.ScanTimeout) + err := p.lstatEachPath(scanner, r, endTime) + if err != nil { + return err + } + + return scanner.Err() +} + +func (p *Paths) lstatEachPath(scanner *bufio.Scanner, r *reporter.Reporter, //nolint:funlen,gocognit + endTime time.Time) (err error) { var wg sync.WaitGroup defer func() { - wg.Wait() + errw := p.waitUntilWGOrMaxTime(&wg, endTime) + if errw != nil { + err = errw + } + p.stopReporting() }() for scanner.Scan() { - path, err := strconv.Unquote(scanner.Text()) - if err != nil { - return err + path, erru := strconv.Unquote(scanner.Text()) + if erru != nil { + return erru } - info, err := p.timeLstat(r, path) + info, errt := p.timeLstat(r, path) errWg := p.waitUntilWGOrMaxTime(&wg, endTime) if errWg != nil { return errWg } - if errors.Is(err, errLstatConsecFails) { - return err - } else if err != nil { + if errors.Is(errt, errLstatConsecFails) { + return errt + } else if errt != nil { continue } p.dispatch(path, info, &wg) } - return scanner.Err() + return err } func (p *Paths) waitUntilWGOrMaxTime(wg *sync.WaitGroup, endTime time.Time) error { diff --git a/stat/paths_test.go b/stat/paths_test.go index 809ba4de..f635b993 100644 --- a/stat/paths_test.go +++ b/stat/paths_test.go @@ -32,6 +32,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "sync/atomic" "testing" "time" @@ -102,14 +103,20 @@ func TestPaths(t *testing.T) { Convey("Given a small max failure count, scan succeeds with non-consecutive failures", func() { s = WithTimeout(100*time.Millisecond, 1, 2, l) + var mu sync.Mutex + count := 0 + mockLstat := func(path string) (fs.FileInfo, error) { - if count%2 != 0 { + mu.Lock() + c := count + count++ + mu.Unlock() + + if c%2 != 0 { time.Sleep(200 * time.Millisecond) } - count++ - return os.Lstat(path) }