-
Notifications
You must be signed in to change notification settings - Fork 4
/
net_test.go
64 lines (51 loc) · 954 Bytes
/
net_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
package main
import (
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestServer(t *testing.T) {
ast := assert.New(t)
testCount := 100
t.Run("echo-server", func(t *testing.T) {
fd, err := TcpServer(20083)
ast.Nil(err)
var wg sync.WaitGroup
// start listener
go func() {
for {
cfd, err := Accept(fd)
ast.Nil(err)
wg.Add(1)
// read
var buf [32]byte
n, _ := Read(cfd, buf[:])
// write
Write(cfd, buf[:n])
wg.Done()
}
}()
// start clients
go func() {
for i := 0; i < testCount; i++ {
tcpConn, err := net.Dial("tcp", "127.0.0.1:20083")
ast.Nil(err)
wg.Add(1)
// write
msg := fmt.Sprintf("%d", time.Now().UnixNano())
tcpConn.Write([]byte(msg))
// read
var res [32]byte
n, err := tcpConn.Read(res[:])
ast.Nil(err)
// equal
ast.Equal(msg, string(res[:n]))
wg.Done()
}
}()
wg.Wait()
})
}