Skip to content

Commit

Permalink
Added a missing test case for 100% reduce coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
destel committed Jul 21, 2024
1 parent 80bd081 commit 5d76613
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions reduce_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ func TestReduce(t *testing.T) {
}
})

// This one is needed to cover the case when the first argument
// of user function is an error.
t.Run(th.Name("error in first input item", n), func(t *testing.T) {
in := FromChan(th.FromRange(0, 1000), nil)
in = replaceWithError(in, 0, fmt.Errorf("err0"))

var cnt atomic.Int64
_, _, err := Reduce(in, n, func(x, y int) (int, error) {
cnt.Add(1)
return x + y, nil
})

th.ExpectError(t, err, "err0")
if cnt.Load() > 100 {
t.Errorf("early exit did not happen")
}

time.Sleep(1 * time.Second)

th.ExpectDrainedChan(t, in)
if cnt.Load() > 100 {
t.Errorf("extra calls to f were made")
}
})

t.Run(th.Name("error in func", n), func(t *testing.T) {
in := FromChan(th.FromRange(0, 1000), nil)

Expand Down

0 comments on commit 5d76613

Please sign in to comment.