-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener_test.go
97 lines (86 loc) · 1.54 KB
/
listener_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
package authConn
import (
"errors"
"fmt"
"github.com/eleztian/authConn/auth"
"sync"
"testing"
)
func TestListener(t *testing.T) {
authFunc := func(authPacket *AuthPacket) (rc byte, credential auth.Credential) {
cre := &auth.BasicCredential{}
err := authPacket.ToCredential(cre)
if err != nil {
return 1, nil
}
if cre.Username != "john" || cre.Password != "123" {
return 1, nil
}
return 0, cre
}
ln, err := Listen("tcp", ":8082", authFunc)
if err != nil {
panic(err)
}
defer ln.Close()
i := 0
for {
conn, err := ln.Accept()
if err != nil {
fmt.Println(err)
break
}
creConn := conn.(*ConnWithCredential)
fmt.Println("accept", creConn.Credential.Detail())
_ = conn.Close()
i++
if i == 10 {
_ = ln.Close()
}
}
}
func TestDialer(t *testing.T) {
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
go func() {
defer wg.Done()
conn, err := Dial("tcp", "127.0.0.1:8082", &auth.BasicCredential{
Username: "john",
Password: "123",
})
if err != nil {
t.Error(err)
return
}
_ = conn.Close()
}()
}
wg.Wait()
}
type AuthToken struct {
tk string
}
func (a AuthToken) Detail() map[string]string {
return map[string]string{
"tk": a.tk,
}
}
func (a *AuthToken) Reset(m map[string]string) error {
tk, ok := m["tk"]
if !ok {
return errors.New("not found tk")
}
a.tk = tk
return nil
}
func TestCluster(t *testing.T) {
conn, err := Dial("tcp", "127.0.0.1:8082", &AuthToken{
tk: "token",
})
if err != nil {
fmt.Println(err)
return
}
conn.Close()
}