...

Source file src/github.com/go-redis/redis/internal/proto/write_buffer_test.go

Documentation: github.com/go-redis/redis/internal/proto

     1  package proto_test
     2  
     3  import (
     4  	"bytes"
     5  	"io/ioutil"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/go-redis/redis/internal/proto"
    10  
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("WriteBuffer", func() {
    16  	var buf *bytes.Buffer
    17  	var wr *proto.Writer
    18  
    19  	BeforeEach(func() {
    20  		buf = new(bytes.Buffer)
    21  		wr = proto.NewWriter(buf)
    22  	})
    23  
    24  	It("should write args", func() {
    25  		err := wr.WriteArgs([]interface{}{
    26  			"string",
    27  			12,
    28  			34.56,
    29  			[]byte{'b', 'y', 't', 'e', 's'},
    30  			true,
    31  			nil,
    32  		})
    33  		Expect(err).NotTo(HaveOccurred())
    34  
    35  		err = wr.Flush()
    36  		Expect(err).NotTo(HaveOccurred())
    37  
    38  		Expect(buf.Bytes()).To(Equal([]byte("*6\r\n" +
    39  			"$6\r\nstring\r\n" +
    40  			"$2\r\n12\r\n" +
    41  			"$5\r\n34.56\r\n" +
    42  			"$5\r\nbytes\r\n" +
    43  			"$1\r\n1\r\n" +
    44  			"$0\r\n" +
    45  			"\r\n")))
    46  	})
    47  
    48  	It("should append marshalable args", func() {
    49  		err := wr.WriteArgs([]interface{}{time.Unix(1414141414, 0)})
    50  		Expect(err).NotTo(HaveOccurred())
    51  
    52  		err = wr.Flush()
    53  		Expect(err).NotTo(HaveOccurred())
    54  
    55  		Expect(buf.Len()).To(Equal(26))
    56  	})
    57  
    58  })
    59  
    60  func BenchmarkWriteBuffer_Append(b *testing.B) {
    61  	buf := proto.NewWriter(ioutil.Discard)
    62  	args := []interface{}{"hello", "world", "foo", "bar"}
    63  
    64  	for i := 0; i < b.N; i++ {
    65  		err := buf.WriteArgs(args)
    66  		if err != nil {
    67  			panic(err)
    68  		}
    69  
    70  		err = buf.Flush()
    71  		if err != nil {
    72  			panic(err)
    73  		}
    74  	}
    75  }
    76  

View as plain text