2016-07-02 15:52:10 +03:00
|
|
|
package proto_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-18 17:42:34 +03:00
|
|
|
"github.com/go-redis/redis/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"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("WriteBuffer", func() {
|
|
|
|
var buf *proto.WriteBuffer
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2018-08-06 13:59:15 +03:00
|
|
|
buf = proto.NewWriteBuffer()
|
2016-07-02 15:52:10 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
It("should reset", func() {
|
|
|
|
buf.AppendString("string")
|
|
|
|
Expect(buf.Len()).To(Equal(12))
|
|
|
|
buf.Reset()
|
|
|
|
Expect(buf.Len()).To(Equal(0))
|
|
|
|
})
|
|
|
|
|
|
|
|
It("should append args", func() {
|
|
|
|
err := buf.Append([]interface{}{
|
|
|
|
"string",
|
|
|
|
12,
|
|
|
|
34.56,
|
|
|
|
[]byte{'b', 'y', 't', 'e', 's'},
|
|
|
|
true,
|
|
|
|
nil,
|
|
|
|
})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
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 marshalable args", func() {
|
|
|
|
err := buf.Append([]interface{}{time.Unix(1414141414, 0)})
|
|
|
|
Expect(err).NotTo(HaveOccurred())
|
|
|
|
Expect(buf.Len()).To(Equal(26))
|
|
|
|
})
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
func BenchmarkWriteBuffer_Append(b *testing.B) {
|
2018-08-06 13:59:15 +03:00
|
|
|
buf := proto.NewWriteBuffer()
|
2016-07-02 15:52:10 +03:00
|
|
|
args := []interface{}{"hello", "world", "foo", "bar"}
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
buf.Append(args)
|
|
|
|
buf.Reset()
|
|
|
|
}
|
|
|
|
}
|