-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathredhub_test.go
129 lines (106 loc) · 2.97 KB
/
redhub_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
122
123
124
125
126
127
128
129
package redhub
import (
"net"
"testing"
"github.com/IceFireDB/redhub/pkg/resp"
"github.com/panjf2000/gnet"
"github.com/stretchr/testify/assert"
)
type mockConn struct {
gnet.Conn
id string
closed bool
written []byte
}
func (m *mockConn) Write(buf []byte) error {
m.written = append(m.written, buf...)
return nil
}
func (m *mockConn) Close() error {
m.closed = true
return nil
}
func (m *mockConn) Context() interface{} { return nil }
func (m *mockConn) SetContext(interface{}) {}
func (m *mockConn) RemoteAddr() net.Addr {
return &net.TCPAddr{
IP: net.ParseIP("127.0.0.1"),
Port: 6379,
Zone: "",
}
}
func TestNewRedHub(t *testing.T) {
onOpened := func(c *Conn) ([]byte, Action) { return nil, None }
onClosed := func(c *Conn, err error) Action { return None }
handler := func(cmd resp.Command, out []byte) ([]byte, Action) { return out, None }
rh := NewRedHub(onOpened, onClosed, handler)
assert.NotNil(t, rh)
assert.NotNil(t, rh.redHubBufMap)
assert.NotNil(t, rh.connSync)
}
func TestOnOpened(t *testing.T) {
onOpened := func(c *Conn) ([]byte, Action) {
return []byte("WELCOME"), None
}
rh := NewRedHub(onOpened, nil, nil)
mock := &mockConn{id: "test1"}
out, action := rh.OnOpened(mock)
assert.Equal(t, "WELCOME", string(out))
assert.Equal(t, gnet.None, action)
rh.connSync.RLock()
_, ok := rh.redHubBufMap[mock]
rh.connSync.RUnlock()
assert.True(t, ok)
}
func TestOnClosed(t *testing.T) {
onClosed := func(c *Conn, err error) Action {
return Close
}
rh := NewRedHub(nil, onClosed, nil)
mock := &mockConn{id: "test1"}
rh.connSync.Lock()
rh.redHubBufMap[mock] = &connBuffer{}
rh.connSync.Unlock()
action := rh.OnClosed(mock, nil)
assert.Equal(t, gnet.Close, action)
rh.connSync.RLock()
_, ok := rh.redHubBufMap[mock]
rh.connSync.RUnlock()
assert.False(t, ok)
}
func TestReact_InvalidCommand(t *testing.T) {
handler := func(cmd resp.Command, out []byte) ([]byte, Action) {
return out, None
}
rh := NewRedHub(nil, nil, handler)
mock := &mockConn{id: "test1"}
out, action := rh.React([]byte("invalid command"), mock)
assert.Contains(t, string(out), "ERR")
assert.Equal(t, gnet.None, action)
}
func TestReact_ValidCommand(t *testing.T) {
handler := func(cmd resp.Command, out []byte) ([]byte, Action) {
return append(out, []byte("OK")...), None
}
rh := NewRedHub(nil, nil, handler)
mock := &mockConn{id: "test1"}
rh.connSync.Lock()
rh.redHubBufMap[mock] = &connBuffer{}
rh.connSync.Unlock()
// Test a simple PING command
out, action := rh.React([]byte("*1\r\n$4\r\nPING\r\n"), mock)
assert.Equal(t, "OK", string(out))
assert.Equal(t, gnet.None, action)
}
func TestReact_CloseAction(t *testing.T) {
handler := func(cmd resp.Command, out []byte) ([]byte, Action) {
return out, Close
}
rh := NewRedHub(nil, nil, handler)
mock := &mockConn{id: "test1"}
rh.connSync.Lock()
rh.redHubBufMap[mock] = &connBuffer{}
rh.connSync.Unlock()
_, action := rh.React([]byte("*1\r\n$4\r\nQUIT\r\n"), mock)
assert.Equal(t, gnet.Close, action)
}