This repository has been archived by the owner on Dec 7, 2019. It is now read-only.
forked from jbenet/go-peerstream
-
Notifications
You must be signed in to change notification settings - Fork 4
/
conn_test.go
229 lines (192 loc) · 4.38 KB
/
conn_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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package peerstream
import (
"errors"
"net"
"sync"
"testing"
tpt "github.com/libp2p/go-libp2p-transport"
smux "github.com/libp2p/go-stream-muxer"
)
type fakeconn struct {
tpt.Conn
}
func (f *fakeconn) Close() error {
return nil
}
var _ net.Conn = new(fakeconn)
func newFakeSmuxConn() *fakeSmuxConn {
return &fakeSmuxConn{
closed: make(chan struct{}),
}
}
type fakeSmuxConn struct {
closeLock sync.Mutex
closed chan struct{}
}
func (fsc *fakeSmuxConn) IsClosed() bool {
select {
case <-fsc.closed:
return true
default:
return false
}
}
// AcceptStream accepts a stream opened by the other side.
func (fsc *fakeSmuxConn) AcceptStream() (smux.Stream, error) {
<-fsc.closed
return nil, errors.New("connection closed")
}
func (fsc *fakeSmuxConn) OpenStream() (smux.Stream, error) {
if fsc.IsClosed() {
return nil, errors.New("connection closed")
}
return &fakeSmuxStream{conn: fsc, closed: make(chan struct{})}, nil
}
func (fsc *fakeSmuxConn) Close() error {
fsc.closeLock.Lock()
defer fsc.closeLock.Unlock()
if fsc.IsClosed() {
return errors.New("already closed")
}
close(fsc.closed)
return nil
}
var _ smux.Conn = (*fakeSmuxConn)(nil)
func TestConnBasic(t *testing.T) {
s := NewSwarm(fakeTransport{func(c net.Conn, isServer bool) (smux.Conn, error) {
return newFakeSmuxConn(), nil
}})
nc := new(fakeconn)
c, err := s.AddConn(nc)
if err != nil {
t.Fatal(err)
}
if c.Swarm() != s {
t.Fatalf("incorrect swarm returned from connection")
}
if sc, ok := c.Conn().(*fakeSmuxConn); !ok {
t.Fatalf("expected a fakeSmuxConn, got %v", sc)
}
if c.NetConn() != nc {
t.Fatalf("expected %v, got %v", nc, c.NetConn())
}
}
func TestConnsWithGroup(t *testing.T) {
s := NewSwarm(nil)
a := newConn(nil, newFakeSmuxConn(), s)
b := newConn(nil, newFakeSmuxConn(), s)
c := newConn(nil, newFakeSmuxConn(), s)
g := "foo"
b.Conn().Close()
c.Conn().Close()
s.conns[a] = struct{}{}
s.conns[b] = struct{}{}
s.conns[c] = struct{}{}
a.AddGroup(g)
b.AddGroup(g)
c.AddGroup(g)
conns := s.ConnsWithGroup(g)
if len(conns) != 1 {
t.Fatal("should have only gotten one")
}
groups := a.Groups()
if len(groups) != 1 {
t.Fatal("should have only gotten one")
}
if groups[0] != g {
t.Fatalf("expected group '%v', got group '%v'", g, groups[0])
}
b.closingLock.Lock()
defer b.closingLock.Unlock()
c.closingLock.Lock()
defer c.closingLock.Unlock()
if !b.closing {
t.Fatal("b should at least be closing")
}
if !c.closing {
t.Fatal("c should at least be closing")
}
}
func TestConnIdx(t *testing.T) {
s := NewSwarm(nil)
c, err := s.AddConn(new(fakeconn))
if err != nil {
t.Fatal(err)
}
g := "foo"
g2 := "bar"
if len(s.ConnsWithGroup(g)) != 0 {
t.Fatal("should have gotten none")
}
c.AddGroup(g)
if !c.InGroup(g) {
t.Fatal("should be in the appropriate group")
}
if len(s.ConnsWithGroup(g)) != 1 {
t.Fatal("should have only gotten one")
}
c.Close()
if !c.InGroup(g) {
t.Fatal("should still be in the appropriate group")
}
if len(s.ConnsWithGroup(g)) != 0 {
t.Fatal("should have gotten none")
}
c.AddGroup(g2)
if !c.InGroup(g2) {
t.Fatal("should now be in group 2")
}
if c.InGroup("bla") {
t.Fatal("should not be in arbitrary groups")
}
if len(s.ConnsWithGroup(g)) != 0 {
t.Fatal("should still have gotten none")
}
if len(s.ConnsWithGroup(g2)) != 0 {
t.Fatal("should still have gotten none")
}
if len(s.connIdx) != 0 {
t.Fatal("should have an empty index")
}
if len(s.conns) != 0 {
t.Fatal("should not be holding any connections")
}
}
func TestAddConnWithGroups(t *testing.T) {
s := NewSwarm(nil)
g := "foo"
g2 := "bar"
g3 := "baz"
c, err := s.AddConn(new(fakeconn), g, g2)
if !c.InGroup(g) || !c.InGroup(g2) || c.InGroup(g3) {
t.Fatal("should be in the appropriate groups")
}
if err != nil {
t.Fatal(err)
}
if len(s.ConnsWithGroup(g)) != 1 {
t.Fatal("should have gotten one")
}
if len(s.ConnsWithGroup(g2)) != 1 {
t.Fatal("should have gotten one")
}
if len(s.ConnsWithGroup(g3)) != 0 {
t.Fatal("should have gotten none")
}
c.Close()
if len(s.ConnsWithGroup(g)) != 0 {
t.Fatal("should have gotten none")
}
if len(s.ConnsWithGroup(g2)) != 0 {
t.Fatal("should have gotten none")
}
if len(s.ConnsWithGroup(g3)) != 0 {
t.Fatal("should still have gotten none")
}
if len(s.connIdx) != 0 {
t.Fatal("should have an empty index")
}
if len(s.conns) != 0 {
t.Fatal("should not be holding any connections")
}
}