-
Notifications
You must be signed in to change notification settings - Fork 14
/
handler_test.go
97 lines (92 loc) · 2.03 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
91
92
93
94
95
96
97
// Copyright (c) 2020 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package netpoll
import (
"errors"
"net"
"testing"
"time"
)
func TestNewHandler(t *testing.T) {
var handler = NewHandler(func(conn net.Conn) (Context, error) {
if n, err := conn.Write([]byte("")); err != nil {
t.Error(err)
} else if n != 0 {
t.Error(n)
}
if n, err := conn.Write([]byte("12345678")); err == nil {
t.Error("Unexpected")
} else if n != 0 {
t.Error(n)
}
if n, err := conn.Read([]byte("")); err != nil {
t.Error(err)
} else if n != 0 {
t.Error(n)
}
conn.LocalAddr()
conn.RemoteAddr()
conn.SetDeadline(time.Now().Add(time.Second))
conn.SetReadDeadline(time.Now().Add(time.Second))
conn.SetWriteDeadline(time.Now().Add(time.Second))
conn.Close()
conn.Close()
return conn, nil
}, func(context Context) error {
return nil
})
ctx, err := handler.Upgrade(&conn{})
if err != nil {
t.Error(err)
}
err = handler.Serve(ctx)
if err != nil {
t.Error(err)
}
}
func TestConnHandler(t *testing.T) {
var handler = &ConnHandler{}
ctx, err := handler.Upgrade(&conn{})
if err != ErrUpgradeFunc {
t.Error(err)
}
err = handler.Serve(ctx)
if err != ErrServeFunc {
t.Error(err)
}
handler.SetUpgrade(func(conn net.Conn) (Context, error) {
return conn, nil
})
handler.SetServe(func(context Context) error {
return nil
})
}
func TestDataHandler(t *testing.T) {
var handler = &DataHandler{
NoShared: true,
NoCopy: false,
BufferSize: 0,
}
_, err := handler.Upgrade(&conn{})
if err != ErrHandlerFunc {
t.Error(err)
}
handler.HandlerFunc = func(req []byte) (res []byte) {
return
}
var fakeErr = errors.New("fake error")
handler.SetUpgrade(func(conn net.Conn) (net.Conn, error) {
return nil, fakeErr
})
_, err = handler.Upgrade(&conn{})
if err != fakeErr {
t.Error(err)
}
handler.SetUpgrade(func(conn net.Conn) (net.Conn, error) {
return conn, nil
})
_, err = handler.Upgrade(&conn{})
if err != nil {
t.Error(err)
}
}