-
Notifications
You must be signed in to change notification settings - Fork 2
/
session_test.go
100 lines (95 loc) · 1.85 KB
/
session_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
package emux
import (
"context"
"errors"
"fmt"
"io"
"math"
"net"
"testing"
)
func TestSession(t *testing.T) {
listener, err := net.Listen("tcp", ":0")
if err != nil {
t.Fatal(err)
}
go func() {
defer listener.Close()
for {
conn, err := listener.Accept()
if err != nil {
t.Fatal(err)
}
sess := NewServer(context.Background(), conn, &DefaultInstruction)
go func() {
defer sess.Close()
for {
stm, err := sess.Accept()
if err != nil {
if errors.Is(err, ErrClosed) {
return
}
t.Fatal(err)
}
buf := make([]byte, math.MaxUint16)
go func() {
defer stm.Close()
for {
n, err := stm.Read(buf)
if err != nil {
if err == io.EOF {
return
}
t.Fatal(err)
}
_, err = stm.Write([]byte("echo " + string(buf[:n])))
if err != nil {
if errors.Is(err, ErrClosed) {
return
}
t.Fatal(err)
}
}
}()
}
}()
}
}()
buf := make([]byte, math.MaxUint16)
for i := 0; i != 5; i++ {
conn, err := net.Dial("tcp", listener.Addr().String())
if err != nil {
t.Fatal(err)
}
sess := NewClient(context.Background(), conn, &DefaultInstruction)
_, err = sess.Dial(context.Background())
if err != nil {
t.Fatal(err)
}
c, err := sess.Dial(context.Background())
if err != nil {
t.Fatal(err)
}
c.Close()
stm, err := sess.Dial(context.Background())
if err != nil {
t.Fatal(err)
}
for j := 0; j != 5; j++ {
msg := fmt.Sprintf("hello %d %d !!!!!!", i, j)
_, err := stm.Write([]byte(msg))
if err != nil {
t.Fatal(err)
}
n, err := stm.Read(buf)
if err != nil {
t.Fatal(err)
}
if string(buf[:n]) != "echo "+msg {
t.Fatalf("fail trans: want %q, got %q", "echo "+msg, buf[:n])
}
t.Log(string(buf[:n]))
}
sess.Close()
}
}