-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler_test.go
90 lines (86 loc) · 2.21 KB
/
handler_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
package gtcp_test
import (
"context"
"io"
"sync/atomic"
"testing"
"time"
"github.com/cat2neat/gtcp"
)
func TestSetKeepAliveHandler(t *testing.T) {
t.Parallel()
dc := &debugNetConn{}
dc.ReadFunc = func(buf []byte) (int, error) {
copy(buf, smashingStr[:len(buf)])
return len(buf), nil
}
c := gtcp.NewBaseConn(dc)
srv := gtcp.Server{Logger: gtcp.DefaultLogger}
first := true
srv.SetKeepAliveHandler(time.Millisecond, func(conn gtcp.Conn) error {
if first {
first = false
return nil
}
// consume buffer in BaseConn that filled by keepalive handler
var bb [1]byte
c.Read(bb[:])
dc.ReadFunc = func(buf []byte) (int, error) {
return 0, errTest
}
return nil
})
srv.ConnHandler(context.Background(), c)
srv.SetKeepAliveHandler(time.Millisecond, func(conn gtcp.Conn) error {
return errTest
})
srv.ConnHandler(context.Background(), c)
ctx, cancel := context.WithCancel(context.Background())
cancel()
srv.SetKeepAliveHandler(time.Millisecond, func(conn gtcp.Conn) error {
return nil
})
srv.ConnHandler(ctx, c)
}
func TestSetPipelineHandler(t *testing.T) {
dc := &debugNetConn{}
dc.Remote = "127.0.0.1"
dc.ReadFunc = func(buf []byte) (int, error) {
copy(buf, smashingStr)
return len(smashingStr), nil
}
c := gtcp.NewBufferedConn(gtcp.NewBaseConn(dc))
srv := gtcp.Server{Logger: nl}
var cnt uint32
srv.SetPipelineHandler(16, func(r io.Reader) ([]byte, error) {
if atomic.LoadUint32(&cnt) < 16 {
buf := make([]byte, len(smashingStr))
r.Read(buf)
return buf, nil
}
return nil, errTest
}, func(buf []byte, wf gtcp.WriteFlusher) error {
new := atomic.AddUint32(&cnt, 1)
if new%2 == 0 {
return nil
}
return errTest
})
srv.ConnHandler(context.Background(), c)
ctx, cancel := context.WithCancel(context.Background())
cancel()
srv.SetPipelineHandler(1, func(r io.Reader) ([]byte, error) {
buf := make([]byte, len(smashingStr))
r.Read(buf)
return buf, nil
}, func(buf []byte, wf gtcp.WriteFlusher) error {
return nil
})
srv.ConnHandler(ctx, c)
srv.SetPipelineHandler(1, func(r io.Reader) ([]byte, error) {
panic("gtcp_test: panic for PipelineReader test")
}, func(buf []byte, wf gtcp.WriteFlusher) error {
return nil
})
srv.ConnHandler(context.Background(), c)
}