-
Notifications
You must be signed in to change notification settings - Fork 1
/
chan_test.go
75 lines (70 loc) · 1.69 KB
/
chan_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
package goneric
import (
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
func TestChanToSlice(t *testing.T) {
ch := make(chan int, 5)
var sl []int
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
sl = ChanToSlice(ch)
wg.Done()
}()
ch <- 1
ch <- 2
ch <- 3
ch <- 4
close(ch)
wg.Wait()
assert.Equal(t, []int{1, 2, 3, 4}, sl)
}
func TestChanToSliceN(t *testing.T) {
f := ctr{}
chGen := GenChan(f.Counter)
sl := ChanToSliceN(chGen, 10)
assert.Equal(t, 55, Sum(sl...))
assert.Len(t, sl, 10)
ch2 := make(chan int, 5)
var sl2 []int
wg2 := sync.WaitGroup{}
wg2.Add(1)
go func() {
sl2 = ChanToSliceN(ch2, 2)
wg2.Done()
}()
ch2 <- 1
close(ch2)
wg2.Wait()
assert.Equal(t, []int{1}, sl2)
}
func TestChanToSliceNTimeout(t *testing.T) {
f1 := ctr{}
chGen1 := GenChan(func() int { return f1.SleepyCounter(time.Millisecond * 100) })
out1 := ChanToSliceNTimeout(chGen1, 10, time.Millisecond*350)
assert.Equal(t, []int{1, 2, 3}, out1)
f2 := ctr{}
chGen2 := GenChan(func() int { return f2.SleepyCounter(time.Millisecond) })
out2 := ChanToSliceNTimeout(chGen2, 10, time.Millisecond*350)
assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, out2)
}
func TestSliceToChan(t *testing.T) {
data := []int{6, 5, 3, 8}
t.Run("chan open", func(t *testing.T) {
ch := make(chan int, 1)
SliceToChan(data, ch)
out := ChanToSliceN(ch, len(data))
assert.Equal(t, data, out)
assert.NotPanics(t, func() { close(ch) }, "make sure out channel is open")
})
t.Run("chan close", func(t *testing.T) {
ch := make(chan int, 1)
SliceToChan(data, ch, true)
out := ChanToSlice(ch)
assert.Equal(t, data, out)
assert.Panics(t, func() { close(ch) }, "make sure out channel is closed")
})
}