-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpipeline_test.go
54 lines (47 loc) · 1.44 KB
/
pipeline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package jpipe_test
import (
"context"
"testing"
"time"
"github.com/junitechnology/jpipe"
"github.com/stretchr/testify/assert"
)
func TestPipelineDoneWhenContextDone(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
pipeline := jpipe.NewPipeline(jpipe.Config{Context: ctx})
jpipe.
FromSlice(pipeline, []int{1, 2, 3}).
Interval(func(value int) time.Duration { return 100 * time.Millisecond }).
ToSlice()
select {
case <-pipeline.Done():
assert.Fail(t, "Pipeline must not be done initially")
default:
}
assert.False(t, pipeline.IsDone())
cancel()
select {
case <-pipeline.Done():
case <-time.After(time.Millisecond):
assert.Fail(t, "Pipeline must be done if context is canceled")
}
assert.True(t, pipeline.IsDone())
assert.ErrorIs(t, pipeline.Error(), context.Canceled)
}
func TestPipelineRecoversFromPanicAndIncludesStacktrace(t *testing.T) {
pipeline := jpipe.NewPipeline(jpipe.Config{})
jpipe.
FromSlice(pipeline, []int{1, 2, 3}).
ForEach(func(value int) {
panic("panic")
})
select {
case <-pipeline.Done():
case <-time.After(time.Millisecond):
assert.Fail(t, "Pipeline must be done if context is canceled")
}
assert.True(t, pipeline.IsDone())
t.Logf("panic error:\n %v", pipeline.Error().Error())
assert.Contains(t, pipeline.Error().Error(), "panic")
assert.Contains(t, pipeline.Error().Error(), "TestPipelineRecoversFromPanicAndIncludesStacktrace") // this shows that the stacktrace is included
}