-
Notifications
You must be signed in to change notification settings - Fork 264
/
test.go
121 lines (97 loc) · 1.89 KB
/
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package server
import (
"io/ioutil"
"net"
"time"
"github.com/WuKongIM/WuKongIM/pkg/wklog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// NewServerOptions NewServerOptions
func NewServerOptions() *Options {
opts := NewOptions()
opts.Mode = TestMode
opts.UnitTest = true
dir, err := ioutil.TempDir("", "wukongim-test")
if err != nil {
panic(err)
}
opts.DataDir = dir
return opts
}
// NewTestServer NewTestServer
func NewTestServer(ots ...*Options) *Server {
var opts *Options
if len(ots) > 0 {
opts = ots[0]
} else {
opts = NewTestOptions()
}
l := New(opts)
return l
}
func NewTestOptions(logLevel ...zapcore.Level) *Options {
opt := NewOptions()
opt.UnitTest = true
opts := wklog.NewOptions()
if len(logLevel) > 0 {
opts.Level = logLevel[0]
} else {
opts.Level = zap.DebugLevel
}
opts.LogDir, _ = ioutil.TempDir("", "limlog")
wklog.Configure(opts)
return opt
}
type TestConn struct {
writeChan chan []byte
authed bool
connID uint32
version uint8
}
func NewTestConn() *TestConn {
return &TestConn{
writeChan: make(chan []byte),
}
}
func (t *TestConn) GetID() uint32 {
return t.connID
}
func (t *TestConn) SetID(id uint32) {
t.connID = id
}
func (t *TestConn) Write(buf []byte) (n int, err error) {
t.writeChan <- buf
n = len(buf)
return
}
func (t *TestConn) WriteChan() chan []byte {
return t.writeChan
}
func (t *TestConn) Close() error {
return nil
}
func (t *TestConn) Authed() bool {
return t.authed
}
func (t *TestConn) SetAuthed(v bool) {
t.authed = v
}
func (t *TestConn) Version() uint8 {
return t.version
}
func (t *TestConn) SetVersion(version uint8) {
t.version = version
}
func (t *TestConn) SetWriteDeadline(tm time.Time) error {
return nil
}
func (t *TestConn) SetReadDeadline(tm time.Time) error {
return nil
}
func (t *TestConn) RemoteAddr() net.Addr {
return nil
}
func (t *TestConn) OutboundBuffered() int {
return 0
}