Repository navigation
Expand file tree
/
Copy pathnet_test.go
More file actions
69 lines (64 loc) · 1.45 KB
/
Copy pathnet_test.go
File metadata and controls
69 lines (64 loc) · 1.45 KB
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
// Run the TCP networking test with:
//
// go test -run TestNet -v
package main
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func EchoServer(s, c, e chan struct{}) {
sfd, err := TcpServer(6666)
if err != nil {
fmt.Printf("tcp server error: %v\n", err)
}
fmt.Println("server started")
s <- struct{}{}
<-c
cfd, err := Accept(sfd)
fmt.Printf("accepted cfd: %v\n", cfd)
if err != nil {
fmt.Printf("server accpet error: %v\n", err)
}
buf := make([]byte, 10)
n, err := Read(cfd, buf)
if err != nil {
fmt.Printf("server read error: %v\n", err)
}
fmt.Printf("read %v bytes\n", n)
n, err = Write(cfd, buf)
if err != nil {
fmt.Printf("server write error: %v\n", err)
}
fmt.Printf("write %v bytes\n", n)
e <- struct{}{}
}
// func TestNet(t *testing.T) {
func useTestAeInstead(t *testing.T) {
fmt.Println("test net lib, rerun the test if the program is blocked")
s := make(chan struct{})
c := make(chan struct{})
e := make(chan struct{})
go EchoServer(s, c, e)
<-s
host := [4]byte{127, 0, 0, 1}
cfd, err := Connect(host, 6666)
fmt.Printf("connected cfd: %v\n", cfd)
time.Sleep(100 * time.Millisecond)
c <- struct{}{}
assert.Nil(t, err)
msg := "helloworld"
n, err := Write(cfd, []byte(msg))
assert.Nil(t, err)
assert.Equal(t, 10, n)
<-e
buf := make([]byte, 10)
n, err = Read(cfd, buf)
assert.Nil(t, err)
assert.Equal(t, 10, n)
assert.Equal(t, msg, string(buf))
}
func TestNet(t *testing.T) {
useTestAeInstead(t)
}