Debian Codegraph @

dependencies/pkg/mod/github.com/redis/go-redis/[email protected]/internal/proto/reader_test.go

raw
1 package proto_test
2
3 import (
4 "bytes"
5 "fmt"
6 "io"
7 "testing"
8
9 "github.com/redis/go-redis/v9/internal/proto"
10 )
11
12 func BenchmarkReader_ParseReply_Status(b *testing.B) {
13 benchmarkParseReply(b, "+OK\r\n", false)
14 }
15
16 func BenchmarkReader_ParseReply_Int(b *testing.B) {
17 benchmarkParseReply(b, ":1\r\n", false)
18 }
19
20 func BenchmarkReader_ParseReply_Float(b *testing.B) {
21 benchmarkParseReply(b, ",123.456\r\n", false)
22 }
23
24 func BenchmarkReader_ParseReply_Bool(b *testing.B) {
25 benchmarkParseReply(b, "#t\r\n", false)
26 }
27
28 func BenchmarkReader_ParseReply_BigInt(b *testing.B) {
29 benchmarkParseReply(b, "(3492890328409238509324850943850943825024385\r\n", false)
30 }
31
32 func BenchmarkReader_ParseReply_Error(b *testing.B) {
33 benchmarkParseReply(b, "-Error message\r\n", true)
34 }
35
36 func BenchmarkReader_ParseReply_Nil(b *testing.B) {
37 benchmarkParseReply(b, "_\r\n", true)
38 }
39
40 func BenchmarkReader_ParseReply_BlobError(b *testing.B) {
41 benchmarkParseReply(b, "!21\r\nSYNTAX invalid syntax", true)
42 }
43
44 func BenchmarkReader_ParseReply_String(b *testing.B) {
45 benchmarkParseReply(b, "$5\r\nhello\r\n", false)
46 }
47
48 func BenchmarkReader_ParseReply_Verb(b *testing.B) {
49 benchmarkParseReply(b, "$9\r\ntxt:hello\r\n", false)
50 }
51
52 func BenchmarkReader_ParseReply_Slice(b *testing.B) {
53 benchmarkParseReply(b, "*2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
54 }
55
56 func BenchmarkReader_ParseReply_Set(b *testing.B) {
57 benchmarkParseReply(b, "~2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
58 }
59
60 func BenchmarkReader_ParseReply_Push(b *testing.B) {
61 benchmarkParseReply(b, ">2\r\n$5\r\nhello\r\n$5\r\nworld\r\n", false)
62 }
63
64 func BenchmarkReader_ParseReply_Map(b *testing.B) {
65 benchmarkParseReply(b, "%2\r\n$5\r\nhello\r\n$5\r\nworld\r\n+key\r\n+value\r\n", false)
66 }
67
68 func BenchmarkReader_ParseReply_Attr(b *testing.B) {
69 benchmarkParseReply(b, "%1\r\n+key\r\n+value\r\n+hello\r\n", false)
70 }
71
72 func TestReader_ReadLine(t *testing.T) {
73 original := bytes.Repeat([]byte("a"), 8192)
74 original[len(original)-2] = '\r'
75 original[len(original)-1] = '\n'
76 r := proto.NewReader(bytes.NewReader(original))
77 read, err := r.ReadLine()
78 if err != nil && err != io.EOF {
79 t.Errorf("Should be able to read the full buffer: %v", err)
80 }
81
82 if !bytes.Equal(read, original[:len(original)-2]) {
83 t.Errorf("Values must be equal: %d expected %d", len(read), len(original[:len(original)-2]))
84 }
85 }
86
87 func benchmarkParseReply(b *testing.B, reply string, wanterr bool) {
88 buf := new(bytes.Buffer)
89 for i := 0; i < b.N; i++ {
90 fmt.Fprint(buf, reply)
91 }
92 p := proto.NewReader(buf)
93 b.ResetTimer()
94
95 for i := 0; i < b.N; i++ {
96 _, err := p.ReadReply()
97 if !wanterr && err != nil {
98 b.Fatal(err)
99 }
100 }
101 }