From 901fadb5d338eb3d12b5f927c8350170e6b77839 Mon Sep 17 00:00:00 2001 From: codingfanlt Date: Thu, 15 Oct 2020 14:10:37 +0800 Subject: [PATCH] fix: fx/fn.Head func will forever block when n is less than 1 (#128) * fix fx/Stream Head func will forever block when n is less than 1 * update test case * update test case --- core/fx/fn.go | 3 +++ core/fx/fn_test.go | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/core/fx/fn.go b/core/fx/fn.go index 7c802048..a38e3c59 100644 --- a/core/fx/fn.go +++ b/core/fx/fn.go @@ -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() { diff --git a/core/fx/fn_test.go b/core/fx/fn_test.go index a1a2ac6c..f3240b5e 100644 --- a/core/fx/fn_test.go +++ b/core/fx/fn_test.go @@ -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) {