|
| 1 | +package redhub |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/IceFireDB/redhub/pkg/resp" |
| 8 | + "github.com/panjf2000/gnet" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | +) |
| 11 | + |
| 12 | +type mockConn struct { |
| 13 | + gnet.Conn |
| 14 | + id string |
| 15 | + closed bool |
| 16 | + written []byte |
| 17 | +} |
| 18 | + |
| 19 | +func (m *mockConn) Write(buf []byte) error { |
| 20 | + m.written = append(m.written, buf...) |
| 21 | + return nil |
| 22 | +} |
| 23 | + |
| 24 | +func (m *mockConn) Close() error { |
| 25 | + m.closed = true |
| 26 | + return nil |
| 27 | +} |
| 28 | + |
| 29 | +func (m *mockConn) Context() interface{} { return nil } |
| 30 | +func (m *mockConn) SetContext(interface{}) {} |
| 31 | +func (m *mockConn) RemoteAddr() net.Addr { |
| 32 | + return &net.TCPAddr{ |
| 33 | + IP: net.ParseIP("127.0.0.1"), |
| 34 | + Port: 6379, |
| 35 | + Zone: "", |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestNewRedHub(t *testing.T) { |
| 40 | + onOpened := func(c *Conn) ([]byte, Action) { return nil, None } |
| 41 | + onClosed := func(c *Conn, err error) Action { return None } |
| 42 | + handler := func(cmd resp.Command, out []byte) ([]byte, Action) { return out, None } |
| 43 | + |
| 44 | + rh := NewRedHub(onOpened, onClosed, handler) |
| 45 | + assert.NotNil(t, rh) |
| 46 | + assert.NotNil(t, rh.redHubBufMap) |
| 47 | + assert.NotNil(t, rh.connSync) |
| 48 | +} |
| 49 | + |
| 50 | +func TestOnOpened(t *testing.T) { |
| 51 | + onOpened := func(c *Conn) ([]byte, Action) { |
| 52 | + return []byte("WELCOME"), None |
| 53 | + } |
| 54 | + rh := NewRedHub(onOpened, nil, nil) |
| 55 | + |
| 56 | + mock := &mockConn{id: "test1"} |
| 57 | + out, action := rh.OnOpened(mock) |
| 58 | + assert.Equal(t, "WELCOME", string(out)) |
| 59 | + assert.Equal(t, gnet.None, action) |
| 60 | + |
| 61 | + rh.connSync.RLock() |
| 62 | + _, ok := rh.redHubBufMap[mock] |
| 63 | + rh.connSync.RUnlock() |
| 64 | + assert.True(t, ok) |
| 65 | +} |
| 66 | + |
| 67 | +func TestOnClosed(t *testing.T) { |
| 68 | + onClosed := func(c *Conn, err error) Action { |
| 69 | + return Close |
| 70 | + } |
| 71 | + rh := NewRedHub(nil, onClosed, nil) |
| 72 | + |
| 73 | + mock := &mockConn{id: "test1"} |
| 74 | + rh.connSync.Lock() |
| 75 | + rh.redHubBufMap[mock] = &connBuffer{} |
| 76 | + rh.connSync.Unlock() |
| 77 | + |
| 78 | + action := rh.OnClosed(mock, nil) |
| 79 | + assert.Equal(t, gnet.Close, action) |
| 80 | + |
| 81 | + rh.connSync.RLock() |
| 82 | + _, ok := rh.redHubBufMap[mock] |
| 83 | + rh.connSync.RUnlock() |
| 84 | + assert.False(t, ok) |
| 85 | +} |
| 86 | + |
| 87 | +func TestReact_InvalidCommand(t *testing.T) { |
| 88 | + handler := func(cmd resp.Command, out []byte) ([]byte, Action) { |
| 89 | + return out, None |
| 90 | + } |
| 91 | + rh := NewRedHub(nil, nil, handler) |
| 92 | + |
| 93 | + mock := &mockConn{id: "test1"} |
| 94 | + out, action := rh.React([]byte("invalid command"), mock) |
| 95 | + assert.Contains(t, string(out), "ERR") |
| 96 | + assert.Equal(t, gnet.None, action) |
| 97 | +} |
| 98 | + |
| 99 | +func TestReact_ValidCommand(t *testing.T) { |
| 100 | + handler := func(cmd resp.Command, out []byte) ([]byte, Action) { |
| 101 | + return append(out, []byte("OK")...), None |
| 102 | + } |
| 103 | + rh := NewRedHub(nil, nil, handler) |
| 104 | + |
| 105 | + mock := &mockConn{id: "test1"} |
| 106 | + rh.connSync.Lock() |
| 107 | + rh.redHubBufMap[mock] = &connBuffer{} |
| 108 | + rh.connSync.Unlock() |
| 109 | + |
| 110 | + // Test a simple PING command |
| 111 | + out, action := rh.React([]byte("*1\r\n$4\r\nPING\r\n"), mock) |
| 112 | + assert.Equal(t, "OK", string(out)) |
| 113 | + assert.Equal(t, gnet.None, action) |
| 114 | +} |
| 115 | + |
| 116 | +func TestReact_CloseAction(t *testing.T) { |
| 117 | + handler := func(cmd resp.Command, out []byte) ([]byte, Action) { |
| 118 | + return out, Close |
| 119 | + } |
| 120 | + rh := NewRedHub(nil, nil, handler) |
| 121 | + |
| 122 | + mock := &mockConn{id: "test1"} |
| 123 | + rh.connSync.Lock() |
| 124 | + rh.redHubBufMap[mock] = &connBuffer{} |
| 125 | + rh.connSync.Unlock() |
| 126 | + |
| 127 | + _, action := rh.React([]byte("*1\r\n$4\r\nQUIT\r\n"), mock) |
| 128 | + assert.Equal(t, gnet.Close, action) |
| 129 | +} |
0 commit comments