Debian Codegraph @

internal/proto/write_buffer_test.go

raw
1 package proto_test
2
3 import (
4 "testing"
5 "time"
6
7 "gopkg.in/redis.v5/internal/proto"
8
9 . "github.com/onsi/ginkgo"
10 . "github.com/onsi/gomega"
11 )
12
13 var _ = Describe("WriteBuffer", func() {
14 var buf *proto.WriteBuffer
15
16 BeforeEach(func() {
17 buf = proto.NewWriteBuffer()
18 })
19
20 It("should reset", func() {
21 buf.AppendString("string")
22 Expect(buf.Len()).To(Equal(12))
23 buf.Reset()
24 Expect(buf.Len()).To(Equal(0))
25 })
26
27 It("should append args", func() {
28 err := buf.Append([]interface{}{
29 "string",
30 12,
31 34.56,
32 []byte{'b', 'y', 't', 'e', 's'},
33 true,
34 nil,
35 })
36 Expect(err).NotTo(HaveOccurred())
37 Expect(buf.Bytes()).To(Equal([]byte("*6\r\n" +
38 "$6\r\nstring\r\n" +
39 "$2\r\n12\r\n" +
40 "$5\r\n34.56\r\n" +
41 "$5\r\nbytes\r\n" +
42 "$1\r\n1\r\n" +
43 "$0\r\n" +
44 "\r\n")))
45 })
46
47 It("should append marshalable args", func() {
48 err := buf.Append([]interface{}{time.Unix(1414141414, 0)})
49 Expect(err).NotTo(HaveOccurred())
50 Expect(buf.Len()).To(Equal(26))
51 })
52
53 })
54
55 func BenchmarkWriteBuffer_Append(b *testing.B) {
56 buf := proto.NewWriteBuffer()
57 args := []interface{}{"hello", "world", "foo", "bar"}
58
59 for i := 0; i < b.N; i++ {
60 buf.Append(args)
61 buf.Reset()
62 }
63 }