-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetserver_test.go
72 lines (61 loc) · 1.29 KB
/
netserver_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
package netserver
import (
"context"
"fmt"
"io"
"net"
"reflect"
"testing"
"time"
)
func TestNew(t *testing.T) {
server := New(echo, WithLogFunc(func(s string, a ...any) {
fmt.Printf(s, a...)
fmt.Printf("\n")
}, true))
fmt.Println(server.Close())
go func() {
time.Sleep(time.Second * 3)
fmt.Println(server.Close())
}()
fmt.Println(server.ServeTCP("127.0.0.1:5555"))
}
func TestNewAcceptError(t *testing.T) {
server := New(echo, WithLogFunc(func(s string, a ...any) {
fmt.Printf(s, a...)
fmt.Printf("\n")
}, true))
listen, err := net.Listen("tcp", "127.0.0.1:5555")
if err != nil {
t.Fatalf(err.Error())
}
ll := &acceptNListener{0, listen}
err = server.Serve(ll)
fmt.Println(err)
}
type acceptNListener struct {
n int
net.Listener
}
func (al *acceptNListener) Accept() (net.Conn, error) {
al.n++
fmt.Println(al.n)
if al.n > 5 {
return nil, fmt.Errorf("模拟accept错误")
}
return al.Listener.Accept()
}
func echo(ctx context.Context, conn net.Conn) {
written, err := io.Copy(conn, conn)
fmt.Printf("copy %d err:%v \n", written, err)
}
func cc(f func(string2 string, vs ...any)) {
if reflect.ValueOf(f).Pointer() == reflect.ValueOf(noopLogFunc).Pointer() {
fmt.Println("eq")
} else {
fmt.Println("not eq")
}
}
func TestFF(t *testing.T) {
cc(noopLogFunc)
}