redis/internal/proto/writer_test.go

155 lines
3.7 KiB
Go
Raw Permalink Normal View History

2016-07-02 15:52:10 +03:00
package proto_test
import (
2018-08-17 13:56:37 +03:00
"bytes"
"encoding"
"fmt"
"net"
2016-07-02 15:52:10 +03:00
"testing"
"time"
2023-01-27 18:00:49 +03:00
. "github.com/bsm/ginkgo/v2"
. "github.com/bsm/gomega"
2021-09-08 16:00:52 +03:00
2023-01-23 09:48:54 +03:00
"github.com/redis/go-redis/v9/internal/proto"
"github.com/redis/go-redis/v9/internal/util"
2016-07-02 15:52:10 +03:00
)
type MyType struct{}
var _ encoding.BinaryMarshaler = (*MyType)(nil)
func (t *MyType) MarshalBinary() ([]byte, error) {
return []byte("hello"), nil
}
2016-07-02 15:52:10 +03:00
var _ = Describe("WriteBuffer", func() {
2018-08-17 13:56:37 +03:00
var buf *bytes.Buffer
var wr *proto.Writer
2016-07-02 15:52:10 +03:00
BeforeEach(func() {
2018-08-17 13:56:37 +03:00
buf = new(bytes.Buffer)
wr = proto.NewWriter(buf)
2016-07-02 15:52:10 +03:00
})
2018-08-17 13:56:37 +03:00
It("should write args", func() {
err := wr.WriteArgs([]interface{}{
2016-07-02 15:52:10 +03:00
"string",
12,
34.56,
[]byte{'b', 'y', 't', 'e', 's'},
true,
nil,
})
Expect(err).NotTo(HaveOccurred())
2018-08-17 13:56:37 +03:00
2016-07-02 15:52:10 +03:00
Expect(buf.Bytes()).To(Equal([]byte("*6\r\n" +
"$6\r\nstring\r\n" +
"$2\r\n12\r\n" +
"$5\r\n34.56\r\n" +
"$5\r\nbytes\r\n" +
"$1\r\n1\r\n" +
"$0\r\n" +
"\r\n")))
})
It("should append time", func() {
2021-09-08 16:00:52 +03:00
tm := time.Date(2019, 1, 1, 9, 45, 10, 222125, time.UTC)
err := wr.WriteArgs([]interface{}{tm})
2016-07-02 15:52:10 +03:00
Expect(err).NotTo(HaveOccurred())
2018-08-17 13:56:37 +03:00
Expect(buf.Len()).To(Equal(41))
2016-07-02 15:52:10 +03:00
})
It("should append marshalable args", func() {
err := wr.WriteArgs([]interface{}{&MyType{}})
Expect(err).NotTo(HaveOccurred())
Expect(buf.Len()).To(Equal(15))
})
It("should append net.IP", func() {
ip := net.ParseIP("192.168.1.1")
err := wr.WriteArgs([]interface{}{ip})
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(Equal(fmt.Sprintf("*1\r\n$16\r\n%s\r\n", bytes.NewBuffer(ip))))
})
2016-07-02 15:52:10 +03:00
})
2020-06-09 16:29:53 +03:00
type discard struct{}
func (discard) Write(b []byte) (int, error) {
return len(b), nil
}
func (discard) WriteString(s string) (int, error) {
return len(s), nil
}
func (discard) WriteByte(c byte) error {
return nil
}
2016-07-02 15:52:10 +03:00
func BenchmarkWriteBuffer_Append(b *testing.B) {
2020-06-09 16:29:53 +03:00
buf := proto.NewWriter(discard{})
2016-07-02 15:52:10 +03:00
args := []interface{}{"hello", "world", "foo", "bar"}
for i := 0; i < b.N; i++ {
2018-08-17 13:56:37 +03:00
err := buf.WriteArgs(args)
if err != nil {
2020-06-09 16:29:53 +03:00
b.Fatal(err)
2018-08-17 13:56:37 +03:00
}
2016-07-02 15:52:10 +03:00
}
}
var _ = Describe("WriteArg", func() {
var buf *bytes.Buffer
var wr *proto.Writer
BeforeEach(func() {
buf = new(bytes.Buffer)
wr = proto.NewWriter(buf)
})
args := map[any]string{
2023-12-17 13:13:01 +03:00
"hello": "$5\r\nhello\r\n",
int(10): "$2\r\n10\r\n",
util.ToPtr(int(10)): "$2\r\n10\r\n",
int8(10): "$2\r\n10\r\n",
util.ToPtr(int8(10)): "$2\r\n10\r\n",
int16(10): "$2\r\n10\r\n",
util.ToPtr(int16(10)): "$2\r\n10\r\n",
int32(10): "$2\r\n10\r\n",
util.ToPtr(int32(10)): "$2\r\n10\r\n",
int64(10): "$2\r\n10\r\n",
util.ToPtr(int64(10)): "$2\r\n10\r\n",
uint(10): "$2\r\n10\r\n",
util.ToPtr(uint(10)): "$2\r\n10\r\n",
uint8(10): "$2\r\n10\r\n",
util.ToPtr(uint8(10)): "$2\r\n10\r\n",
uint16(10): "$2\r\n10\r\n",
util.ToPtr(uint16(10)): "$2\r\n10\r\n",
uint32(10): "$2\r\n10\r\n",
util.ToPtr(uint32(10)): "$2\r\n10\r\n",
uint64(10): "$2\r\n10\r\n",
util.ToPtr(uint64(10)): "$2\r\n10\r\n",
2023-12-17 13:13:01 +03:00
float32(10.3): "$18\r\n10.300000190734863\r\n",
util.ToPtr(float32(10.3)): "$18\r\n10.300000190734863\r\n",
float64(10.3): "$4\r\n10.3\r\n",
util.ToPtr(float64(10.3)): "$4\r\n10.3\r\n",
bool(true): "$1\r\n1\r\n",
bool(false): "$1\r\n0\r\n",
util.ToPtr(bool(true)): "$1\r\n1\r\n",
util.ToPtr(bool(false)): "$1\r\n0\r\n",
}
for arg, expect := range args {
2023-12-17 13:13:01 +03:00
arg, expect := arg, expect
It(fmt.Sprintf("should write arg of type %T", arg), func() {
err := wr.WriteArg(arg)
Expect(err).NotTo(HaveOccurred())
Expect(buf.String()).To(Equal(expect))
})
}
})