forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream_test.go
214 lines (186 loc) · 5.34 KB
/
stream_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
package dmsg
import (
"context"
"fmt"
"io"
"net"
"sync"
"testing"
"time"
"github.com/skycoin/skycoin/src/util/logging"
"github.com/stretchr/testify/require"
"golang.org/x/net/nettest"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/skycoin/dmsg/noise"
)
func TestStream(t *testing.T) {
// Prepare mock discovery.
dc := disc.NewMock(0)
const maxSessions = 10
// Prepare dmsg server.
pkSrv, skSrv := GenKeyPair(t, "server")
srvConf := &ServerConfig{
MaxSessions: maxSessions,
UpdateInterval: 0,
}
srv := NewServer(pkSrv, skSrv, dc, srvConf, nil)
srv.SetLogger(logging.MustGetLogger("server"))
lisSrv, err := net.Listen("tcp", "")
require.NoError(t, err)
// Serve dmsg server.
chSrv := make(chan error, 1)
go func() { chSrv <- srv.Serve(lisSrv, "") }() //nolint:errcheck
// Prepare and serve dmsg client A.
pkA, skA := GenKeyPair(t, "client A")
clientA := NewClient(pkA, skA, dc, DefaultConfig())
clientA.SetLogger(logging.MustGetLogger("client_A"))
go clientA.Serve(context.Background())
// Prepare and serve dmsg client B.
pkB, skB := GenKeyPair(t, "client B")
clientB := NewClient(pkB, skB, dc, DefaultConfig())
clientB.SetLogger(logging.MustGetLogger("client_B"))
go clientB.Serve(context.Background())
// Ensure all entities are registered in discovery before continuing.
time.Sleep(time.Second * 2)
// Helper functions.
makePiper := func(dialer, listener *Client, port uint16) (net.Listener, nettest.MakePipe) {
lis, err := listener.Listen(port)
require.NoError(t, err)
return lis, func() (c1, c2 net.Conn, stop func(), err error) {
if c1, err = dialer.DialStream(context.TODO(), Addr{PK: listener.LocalPK(), Port: port}); err != nil {
return
}
if c2, err = lis.Accept(); err != nil {
return
}
stop = func() {
_ = c1.Close() //nolint:errcheck
_ = c2.Close() //nolint:errcheck
}
return
}
}
t.Run("test_large_data_io", func(t *testing.T) {
const port = 8080
lis, makePipe := makePiper(clientA, clientB, port)
connA, connB, stop, errA := makePipe()
require.NoError(t, errA)
fmt.Println(connA.LocalAddr(), connA.RemoteAddr())
fmt.Println(connB.LocalAddr(), connB.RemoteAddr())
largeData := cipher.RandByte(noise.MaxWriteSize)
nA, errA := connA.Write(largeData)
require.NoError(t, errA)
require.Equal(t, len(largeData), nA)
readB := make([]byte, len(largeData))
nB, errB := io.ReadFull(connB, readB)
require.NoError(t, errB)
require.Equal(t, len(largeData), nB)
require.Equal(t, largeData, readB)
// Closing logic.
stop()
require.NoError(t, lis.Close())
})
// TODO: The Timeout portions of these tests sometimes fail for currently unknown reasons.
// TODO: We need to look into whether nettest.TestConn is even suitable for dmsg.Stream.
// TODO: If so, we need to see how to fix the behavior.
//t.Run("TestConn", func(t *testing.T) {
// const rounds = 3
// listeners := make([]net.Listener, 0, rounds*2)
//
// for port := uint16(1); port <= rounds; port++ {
// lis1, makePipe1 := makePiper(clientA, clientB, port)
// listeners = append(listeners, lis1)
// nettest.TestConn(t, makePipe1)
//
// lis2, makePipe2 := makePiper(clientB, clientA, port)
// listeners = append(listeners, lis2)
// nettest.TestConn(t, makePipe2)
// }
//
// // Closing logic.
// for _, lis := range listeners {
// require.NoError(t, lis.Close())
// }
//})
//
//t.Run("TestConn concurrent", func(t *testing.T) {
// const rounds = 10
// listeners := make([]net.Listener, 0, rounds*2)
//
// wg := new(sync.WaitGroup)
// wg.Add(rounds * 2)
//
// for port := uint16(1); port <= rounds; port++ {
// lis1, makePipe1 := makePiper(clientA, clientB, port)
// listeners = append(listeners, lis1)
// go func(makePipe1 nettest.MakePipe) {
// nettest.TestConn(t, makePipe1)
// wg.Done()
// }(makePipe1)
//
// lis2, makePipe2 := makePiper(clientB, clientA, port)
// listeners = append(listeners, lis2)
// go func(makePipe2 nettest.MakePipe) {
// nettest.TestConn(t, makePipe2)
// wg.Done()
// }(makePipe2)
// }
//
// wg.Wait()
//
// // Closing logic.
// for _, lis := range listeners {
// require.NoError(t, lis.Close())
// }
//})
t.Run("test_concurrent_dialing", func(t *testing.T) {
const port = 8080
const rounds = 10
aErrs := make([]error, rounds)
bErrs := make([]error, rounds)
wg := new(sync.WaitGroup)
wg.Add(rounds * 2)
lis, err := clientA.Listen(port)
require.NoError(t, err)
for i := 0; i < rounds; i++ {
go func(i int) {
connB, err := clientB.Dial(context.TODO(), lis.DmsgAddr())
if err != nil {
bErrs[i] = err
} else {
_ = connB.Close() //nolint:errcheck
}
wg.Done()
}(i)
go func(i int) {
connA, err := lis.Accept()
if err != nil {
aErrs[i] = err
} else {
_ = connA.Close() //nolint:errcheck
}
wg.Done()
}(i)
}
wg.Wait()
for _, err := range aErrs {
require.NoError(t, err)
}
for _, err := range bErrs {
require.NoError(t, err)
}
// Closing logic.
require.NoError(t, lis.Close())
})
// Closing logic.
require.NoError(t, clientB.Close())
require.NoError(t, clientA.Close())
require.NoError(t, srv.Close())
require.NoError(t, <-chSrv)
}
func GenKeyPair(t *testing.T, seed string) (cipher.PubKey, cipher.SecKey) {
pk, sk, err := cipher.GenerateDeterministicKeyPair([]byte(seed))
require.NoError(t, err)
return pk, sk
}