From 5d76613f86842b084464630a733bd737faec3fe6 Mon Sep 17 00:00:00 2001 From: destel Date: Sun, 21 Jul 2024 16:16:12 +0300 Subject: [PATCH] Added a missing test case for 100% reduce coverage --- reduce_test.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/reduce_test.go b/reduce_test.go index b0e55f0..8e83aae 100644 --- a/reduce_test.go +++ b/reduce_test.go @@ -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)