-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtunnel_test.go
169 lines (134 loc) · 2.82 KB
/
tunnel_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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package tunnel
import (
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func TestExample(t *testing.T) {
tn := NewUnbuffered()
for i := 0; i < 10; i++ {
go func() {
tn.Send("something")
}()
}
go func() {
for data := range tn.Out() {
assert.Equal(t, "something", data.(string))
}
}()
tn.Close()
tn.Wait()
return
}
func TestTunnelBasicCase(t *testing.T) {
th := NewBuffered(4)
th.Send("yo1")
th.Send("yo2")
th.Send("yo3")
th.Send("yo4")
var data interface{}
var ok bool
data, ok = <-th.Out()
assert.Equal(t, true, ok)
assert.Equal(t, "yo1", data.(string))
data, ok = <-th.Out()
assert.Equal(t, true, ok)
assert.Equal(t, "yo2", data.(string))
data, ok = <-th.Out()
assert.Equal(t, true, ok)
assert.Equal(t, "yo3", data.(string))
data, ok = <-th.Out()
assert.Equal(t, true, ok)
assert.Equal(t, "yo4", data.(string))
// Hammering Close() shouldn't affect anything
th.Close()
th.Close()
th.Close()
th.Wait()
th.Wait()
th.Wait()
assert.Equal(t, true, th.IsClosed())
assert.Equal(t, 0, th.Len())
}
func TestTunnelBufferOverflow(t *testing.T) {
th := NewBuffered(100)
// Attempt to write more than the tunnel can handle.
for i := 0; i < 2000; i++ {
go th.Send(1)
}
// Let the loop above run for some time
time.Sleep(100 * time.Millisecond)
// Close this tunnel, any further attempt to push should fail.
th.Close()
// Flush them out
readerDone := make(chan bool)
go func() {
for c := range th.Out() {
assert.Equal(t, 1, c.(int))
}
close(readerDone)
}()
th.Wait()
assert.Equal(t, true, th.IsClosed())
<-readerDone
// Further attempt at reading will fail
_, ok := <-th.Out()
assert.Equal(t, false, ok)
assert.Equal(t, 0, th.Len())
}
func TestTunnelGoCrazyUnbuffered(t *testing.T) {
th := NewUnbuffered()
// reader
readerDone := make(chan bool)
go func() {
for c := range th.Out() {
assert.Equal(t, "data", c.(string))
}
close(readerDone)
}()
// writer
writerDone := make(chan bool)
go func() {
var err error
for err == nil {
err = th.Send("data")
}
close(writerDone)
}()
// Wait to make reader and writer go crazy
time.Sleep(1000 * time.Millisecond)
th.Close()
th.Wait()
assert.Equal(t, true, th.IsClosed())
<-readerDone
<-writerDone
assert.Equal(t, 0, th.Len())
}
func TestTunnelGoCrazyBuffered(t *testing.T) {
th := NewBuffered(100)
// reader
readerDone := make(chan bool)
go func() {
for c := range th.Out() {
assert.Equal(t, "data", c.(string))
}
close(readerDone)
}()
// writer
writerDone := make(chan bool)
go func() {
var err error
for err == nil {
err = th.Send("data")
}
close(writerDone)
}()
// Wait to make reader and writer go crazy
time.Sleep(1000 * time.Millisecond)
th.Close()
th.Wait()
assert.Equal(t, true, th.IsClosed())
<-readerDone
<-writerDone
assert.Equal(t, 0, th.Len())
}