-
Notifications
You must be signed in to change notification settings - Fork 5
/
unbound_test.go
101 lines (91 loc) · 2.05 KB
/
unbound_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// Copyright (C) 2022 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/golem
//
package pipe_test
import (
"context"
"math/rand"
"testing"
"time"
"github.com/fogfish/golem/pipe"
"github.com/fogfish/it/v2"
)
func TestPipeNew(t *testing.T) {
t.Run("Empty.Recv", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
in, _ := pipe.New[int](ctx, 0)
select {
case <-in:
panic("Must Not Recv Anything")
case <-time.After(10 * time.Millisecond):
}
close()
})
t.Run("Empty.Send", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
_, eg := pipe.New[int](ctx, 0)
select {
case eg <- 0:
break
case <-time.After(10 * time.Millisecond):
panic("Must Not Blocked")
}
close()
})
t.Run("Send.Recv", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
in, eg := pipe.New[int](ctx, 0)
eg <- 100
it.Then(t).Should(
it.Equal(<-in, 100),
)
close()
})
t.Run("Send.Batch.Recv", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
in, eg := pipe.New[int](ctx, 0)
for i := 0; i < 1000; i++ {
eg <- i
}
for i := 0; i < 1000; i++ {
it.Then(t).Should(
it.Equal(<-in, i),
)
}
close()
})
t.Run("Recv.Async.Send", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
in, eg := pipe.New[int](ctx, 0)
go func() {
for i := 0; i < 1000; i++ {
time.Sleep(time.Duration(rand.Intn(4)) * time.Millisecond)
it.Then(t).Should(
it.Equal(<-in, i),
)
}
}()
for i := 0; i < 1000; i++ {
eg <- i
time.Sleep(time.Duration(rand.Intn(9)) * time.Millisecond)
}
close()
})
t.Run("Recv.After.Close", func(t *testing.T) {
ctx, close := context.WithCancel(context.Background())
in, eg := pipe.New[int](ctx, 0)
for i := 0; i < 1000; i++ {
eg <- i
}
close()
for i := 0; i < 1000; i++ {
it.Then(t).Should(
it.Equal(<-in, i),
)
}
})
}