Skip to content

Commit

Permalink
fix: fx/fn.Head func will forever block when n is less than 1 (zeromi…
Browse files Browse the repository at this point in the history
…cro#128)

* fix fx/Stream Head func will forever block when n is less than 1

* update test case

* update test case
  • Loading branch information
fanlongteng authored Oct 15, 2020
1 parent c824e9e commit 901fadb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/fx/fn.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ func (p Stream) Group(fn KeyFunc) Stream {
}

func (p Stream) Head(n int64) Stream {
if n < 1 {
panic("n must be greater than 0")
}
source := make(chan interface{})

go func() {
Expand Down
8 changes: 8 additions & 0 deletions core/fx/fn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ func TestHead(t *testing.T) {
assert.Equal(t, 3, result)
}

func TestHeadZero(t *testing.T) {
assert.Panics(t, func() {
Just(1, 2, 3, 4).Head(0).Reduce(func(pipe <-chan interface{}) (interface{}, error) {
return nil, nil
})
})
}

func TestHeadMore(t *testing.T) {
var result int
Just(1, 2, 3, 4).Head(6).Reduce(func(pipe <-chan interface{}) (interface{}, error) {
Expand Down

0 comments on commit 901fadb

Please sign in to comment.