Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support option of error strategy on ForEach operator #322

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 4 additions & 1 deletion observable_operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,7 @@ func (o *ObservableImpl) FlatMap(apply ItemToObservable, opts ...Option) Observa
// ForEach subscribes to the Observable and receives notifications for each element.
func (o *ObservableImpl) ForEach(nextFunc NextFunc, errFunc ErrFunc, completedFunc CompletedFunc, opts ...Option) Disposed {
dispose := make(chan struct{})
option := parseOptions(opts...)
handler := func(ctx context.Context, src <-chan Item) {
defer close(dispose)
for {
Expand All @@ -1167,7 +1168,9 @@ func (o *ObservableImpl) ForEach(nextFunc NextFunc, errFunc ErrFunc, completedFu
}
if i.Error() {
errFunc(i.E)
break
if option.getErrorStrategy() == StopOnError {
return
}
}
nextFunc(i.V)
}
Expand Down
86 changes: 86 additions & 0 deletions observable_operator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -854,6 +854,92 @@ func Test_Observable_ForEach_Error(t *testing.T) {
assert.Equal(t, errFoo, gotErr)
}

func Test_Observable_ForEach_ContinueOnError(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
count := 0
var gotErr error
done := make(chan struct{})

obs := testObservable(ctx, 1, 2, 3, errFoo, 4)
var strategy OnErrorStrategy = ContinueOnError

obs.ForEach(func(i interface{}) {
switch v := i.(type) {
case int:
count += v
case error:
// do nothing
default:
}
}, func(err error) {
gotErr = err
select {
case <-ctx.Done():
return
default:
if strategy == StopOnError {
done <- struct{}{}
}
}
}, func() {
select {
case <-ctx.Done():
return
case done <- struct{}{}:
}
}, WithContext(ctx), WithErrorStrategy(strategy))

// We avoid using the assertion API on purpose
<-done
assert.Equal(t, 10, count)
assert.Equal(t, errFoo, gotErr)
}

func Test_Observable_ForEach_StopOnError(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
count := 0
var gotErr error
done := make(chan struct{})

obs := testObservable(ctx, 1, 2, 3, errFoo, 4)
var strategy OnErrorStrategy = StopOnError

obs.ForEach(func(i interface{}) {
switch v := i.(type) {
case int:
count += v
case error:
// do nothing
default:
}
}, func(err error) {
gotErr = err
select {
case <-ctx.Done():
return
default:
if strategy == StopOnError {
done <- struct{}{}
}
}
}, func() {
select {
case <-ctx.Done():
return
case done <- struct{}{}:
}
}, WithContext(ctx), WithErrorStrategy(strategy))

// We avoid using the assertion API on purpose
<-done
assert.Equal(t, 6, count)
assert.Equal(t, errFoo, gotErr)
}

func Test_Observable_ForEach_Done(t *testing.T) {
defer goleak.VerifyNone(t)
ctx, cancel := context.WithCancel(context.Background())
Expand Down