Skip to content

Commit

Permalink
Delint and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rk1274 committed Nov 1, 2024
1 parent 42edaec commit 30cdfab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
32 changes: 23 additions & 9 deletions stat/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 10 additions & 3 deletions stat/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 30cdfab

Please sign in to comment.