mirror of
https://github.com/HDT3213/godis.git
synced 2025-10-05 00:42:43 +08:00
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
package parser
|
|
|
|
import (
|
|
"bytes"
|
|
"github.com/hdt3213/godis/interface/redis"
|
|
"github.com/hdt3213/godis/lib/utils"
|
|
"github.com/hdt3213/godis/redis/protocol"
|
|
"io"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseStream(t *testing.T) {
|
|
replies := []redis.Reply{
|
|
protocol.MakeIntReply(1),
|
|
protocol.MakeStatusReply("OK"),
|
|
protocol.MakeErrReply("ERR unknown"),
|
|
protocol.MakeBulkReply([]byte("a\r\nb")), // test binary safe
|
|
protocol.MakeNullBulkReply(),
|
|
protocol.MakeMultiBulkReply([][]byte{
|
|
[]byte("a"),
|
|
[]byte("\r\n"),
|
|
}),
|
|
protocol.MakeEmptyMultiBulkReply(),
|
|
}
|
|
reqs := bytes.Buffer{}
|
|
for _, re := range replies {
|
|
reqs.Write(re.ToBytes())
|
|
}
|
|
reqs.Write([]byte("set a a" + protocol.CRLF)) // test text protocol
|
|
expected := make([]redis.Reply, len(replies))
|
|
copy(expected, replies)
|
|
expected = append(expected, protocol.MakeMultiBulkReply([][]byte{
|
|
[]byte("set"), []byte("a"), []byte("a"),
|
|
}))
|
|
|
|
ch := ParseStream(bytes.NewReader(reqs.Bytes()))
|
|
i := 0
|
|
for payload := range ch {
|
|
if payload.Err != nil {
|
|
if payload.Err == io.EOF {
|
|
return
|
|
}
|
|
t.Error(payload.Err)
|
|
return
|
|
}
|
|
if payload.Data == nil {
|
|
t.Error("empty data")
|
|
return
|
|
}
|
|
exp := expected[i]
|
|
i++
|
|
if !utils.BytesEquals(exp.ToBytes(), payload.Data.ToBytes()) {
|
|
t.Error("parse failed: " + string(exp.ToBytes()))
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestParseOne(t *testing.T) {
|
|
replies := []redis.Reply{
|
|
protocol.MakeIntReply(1),
|
|
protocol.MakeStatusReply("OK"),
|
|
protocol.MakeErrReply("ERR unknown"),
|
|
protocol.MakeBulkReply([]byte("a\r\nb")), // test binary safe
|
|
protocol.MakeNullBulkReply(),
|
|
protocol.MakeMultiBulkReply([][]byte{
|
|
[]byte("a"),
|
|
[]byte("\r\n"),
|
|
}),
|
|
protocol.MakeEmptyMultiBulkReply(),
|
|
}
|
|
for _, re := range replies {
|
|
result, err := ParseOne(re.ToBytes())
|
|
if err != nil {
|
|
t.Error(err)
|
|
continue
|
|
}
|
|
if !utils.BytesEquals(result.ToBytes(), re.ToBytes()) {
|
|
t.Error("parse failed: " + string(re.ToBytes()))
|
|
}
|
|
}
|
|
}
|