forked from skycoin/dmsg-deprecated
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_client_test.go
109 lines (83 loc) · 2.21 KB
/
new_client_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
// +build !no_ci
package dmsg
import (
"fmt"
"log"
"net"
"sync"
"testing"
"github.com/skycoin/dmsg/cipher"
"github.com/skycoin/dmsg/disc"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TODO: update comments mentioning a & b
// Given two client instances (a & b) and a server instance (s),
// Client b should be able to dial a transport with client b
// Data should be sent and delivered successfully via the transport.
// TODO: fix this.
func TestNewClient(t *testing.T) {
srvPK, srvSK := cipher.GenerateKeyPair()
sAddr := "127.0.0.1:8081"
const tpCount = 10
dc := disc.NewMock()
l, err := net.Listen("tcp", sAddr)
require.NoError(t, err)
srv, err := NewServer(srvPK, srvSK, "", l, dc)
require.NoError(t, err)
log.Println(srv.Addr())
var serveErr error
serveDone := make(chan struct{})
go func() {
serveErr = srv.Serve()
close(serveDone)
}()
responder := createClient(t, dc, responderName)
initiator := createClient(t, dc, initiatorName)
wg := new(sync.WaitGroup)
wg.Add(1)
errCh := make(chan error, 1)
go func() {
defer wg.Done()
for i := 0; i < tpCount; i++ {
initiatorTp, responderTp := dial(t, initiator, responder, noDelay)
for j := 0; j < msgCount; j++ {
pay := []byte(fmt.Sprintf("This is message %d!", j))
if _, err := responderTp.Write(pay); err != nil {
errCh <- err
return
}
if _, err := initiatorTp.Read(pay); err != nil {
errCh <- err
return
}
}
if err := closeClosers(responderTp, initiatorTp); err != nil {
errCh <- err
return
}
}
errCh <- nil
}()
for i := 0; i < tpCount; i++ {
initiatorTp, responderTp := dial(t, initiator, responder, noDelay)
for j := 0; j < msgCount; j++ {
pay := []byte(fmt.Sprintf("This is message %d!", j))
n, err := responderTp.Write(pay)
require.NoError(t, err)
require.Equal(t, len(pay), n)
got := make([]byte, len(pay))
n, err = initiatorTp.Read(got)
require.Equal(t, len(pay), n)
require.NoError(t, err)
require.Equal(t, pay, got)
}
// Close TPs
require.NoError(t, closeClosers(responderTp, initiatorTp))
}
wg.Wait()
assert.NoError(t, <-errCh)
assert.NoError(t, srv.Close())
<-serveDone
assert.NoError(t, serveErr)
}