Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions parallel.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,21 @@ func ParFilter[I any](pipe Stream[I], num int, callback func(context.Context, I)
ops...,
)
}

// ParForEach is like ForEach, but calls the callback concurrently with num goroutines.
// It's better to use it with a buffered stream.
func ParForEach[I any](pipe Stream[I], num int, callback func(context.Context, I) error) error {
for i := 0; i < num; i++ {
pipe.eg.Go(func() error {
for elem := range pipe.in {
if err := callback(pipe.ctx, elem); err != nil {
return err
}
}

return nil
})
}

return pipe.eg.Wait()
}
100 changes: 99 additions & 1 deletion parallel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/rand"
"sort"
"strconv"
"sync"
"sync/atomic"
"testing"
"time"
Expand Down Expand Up @@ -39,7 +40,11 @@ func TestParallel(t *testing.T) {
}

func TestParallelPipeline(t *testing.T) {
testFn := func(producer rheos.Stream[int], mapFn func(context.Context, int) (int, error), filterMapFn func(context.Context, int) (int, bool, error)) ([]int, error) {
testFn := func(
producer rheos.Stream[int],
mapFn func(context.Context, int) (int, error),
filterMapFn func(context.Context, int) (int, bool, error),
) ([]int, error) {
size := rand.Intn(10) + 1
p2 := rheos.ParMap(producer, size, mapFn)
p3 := rheos.ParFilterMap(p2, size, filterMapFn)
Expand Down Expand Up @@ -141,3 +146,96 @@ func TestParallelPipeline(t *testing.T) {
}
})
}

func TestUnitParForEach(t *testing.T) {
t.Run("collect items", func(t *testing.T) {
num := int(rand.Int31n(100) + 10)
producer := newProducer(context.Background(), num)
want := intRange(num)

concurrency := rand.Intn(10) + 1

var mux sync.Mutex // guard writing to result slice
var result []int
err := rheos.ParForEach(
producer,
concurrency,
func(_ context.Context, v int) error {
mux.Lock()
defer mux.Unlock()

result = append(result, v)

return nil
},
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

sort.Slice(result, func(i, j int) bool {
return result[i] < result[j]
})
assertSlicesEqual(t, want, result)
})

t.Run("returns error", func(t *testing.T) {
num := int(rand.Int31n(100) + 10)
producer := newProducer(context.Background(), num)

concurrency := rand.Intn(10) + 1

var mux sync.Mutex // guard writing to result slice
var result []int
err := rheos.ParForEach(
producer,
concurrency,
func(_ context.Context, v int) error {
mux.Lock()
defer mux.Unlock()

result = append(result, v)
if len(result) >= num/2 {
return errTest
}

return nil
},
)

if !errors.Is(err, errTest) {
t.Errorf("unexpected error: %v, want: %v", err, errTest)
}
})

t.Run("context is cancelled", func(t *testing.T) {
num := int(rand.Int31n(100) + 10)

ctx, cancel := context.WithCancel(context.Background())
producer := newProducer(ctx, num)

concurrency := rand.Intn(10) + 1

var mux sync.Mutex // guard writing to result slice
var result []int
err := rheos.ParForEach(
producer,
concurrency,
func(_ context.Context, v int) error {
mux.Lock()
defer mux.Unlock()

result = append(result, v)
if len(result) >= num/2 {
cancel()
}

return nil
},
)

if !errors.Is(err, context.Canceled) {
t.Errorf("unexpected error: %v, want: %v", err, context.Canceled)
}
})
}
4 changes: 2 additions & 2 deletions rheos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func TestUnitForEach(t *testing.T) {
t.Run("collect items", func(t *testing.T) {
num := int(rand.Int31n(100) + 10)
p := newProducer(context.Background(), num)
want := intRange(num)

var result []int
err := rheos.ForEach(
Expand All @@ -123,11 +124,10 @@ func TestUnitForEach(t *testing.T) {
return nil
},
)

want := intRange(num)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

assertSlicesEqual(t, want, result)
})
t.Run("returns error", func(t *testing.T) {
Expand Down