-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsource_test.go
83 lines (65 loc) · 2.29 KB
/
source_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package jpipe_test
import (
"context"
"fmt"
"testing"
"time"
"github.com/junitechnology/jpipe"
"github.com/stretchr/testify/assert"
)
func TestFromSlice(t *testing.T) {
t.Run("Creates channel from slice", func(t *testing.T) {
slice := []int{1, 2, 3}
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, slice)
actual := drainChannel(channel)
assert.Equal(t, slice, actual)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if pipeline canceled", func(t *testing.T) {
slice := []int{1, 2, 3}
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromSlice(pipeline, slice)
goChannel := channel.ToGoChannel()
readGoChannel(goChannel, 2)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestFromRange(t *testing.T) {
t.Run("Creates channel from range", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromRange(pipeline, 7, 9)
actual := drainChannel(channel)
assert.Equal(t, []int{7, 8, 9}, actual)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
t.Run("Exits early if pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromRange(pipeline, 7, 9)
goChannel := channel.ToGoChannel()
readGoChannel(goChannel, 2)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}
func TestFromGenerator(t *testing.T) {
t.Run("Creates channel from generator", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromGenerator(pipeline, func(i uint64) string { return fmt.Sprintf("%dA", i) })
goChannel := channel.ToGoChannel()
actual := readGoChannel(goChannel, 3)
assert.Equal(t, []string{"0A", "1A", "2A"}, actual)
})
t.Run("Exits early if pipeline canceled", func(t *testing.T) {
pipeline := jpipe.New(context.TODO())
channel := jpipe.FromGenerator(pipeline, func(i uint64) string { return fmt.Sprintf("%dA", i) })
goChannel := channel.ToGoChannel()
readGoChannel(goChannel, 2)
cancelPipeline(pipeline)
assertChannelClosed(t, goChannel, 10*time.Millisecond)
assertPipelineDone(t, pipeline, 10*time.Millisecond)
})
}