2016-07-02 15:52:10 +03:00
|
|
|
package proto_test
|
|
|
|
|
|
|
|
import (
|
2018-08-17 13:56:37 +03:00
|
|
|
"bytes"
|
2019-07-25 12:21:12 +03:00
|
|
|
"encoding"
|
2016-07-02 15:52:10 +03:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-03-11 17:29:16 +03:00
|
|
|
"github.com/go-redis/redis/v8/internal/proto"
|
2017-02-08 12:24:09 +03:00
|
|
|
|
2016-07-02 15:52:10 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
2019-07-25 12:21:12 +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")))
|
|
|
|
})
|
|
|
|
|
2019-07-25 12:21:12 +03:00
|
|
|
It("should append time", func() {
|
2020-03-12 18:33:50 +03:00
|
|
|
tm := time.Date(2019, 01, 01, 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
|
|
|
|
2020-03-12 18:33:50 +03:00
|
|
|
Expect(buf.Len()).To(Equal(41))
|
2016-07-02 15:52:10 +03:00
|
|
|
})
|
|
|
|
|
2019-07-25 12:21:12 +03:00
|
|
|
It("should append marshalable args", func() {
|
|
|
|
err := wr.WriteArgs([]interface{}{&MyType{}})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
|
|
|
|
Expect(buf.Len()).To(Equal(15))
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
}
|