-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtermination_test.go
88 lines (72 loc) · 1.93 KB
/
termination_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package pgproto_test
import (
"bytes"
"testing"
"github.com/c653labs/pgproto"
"github.com/stretchr/testify/suite"
)
// 'X' [int32 - length]
var rawTerminationMessage = [5]byte{
// Tag
'X',
// Length
'\x00', '\x00', '\x00', '\x04',
}
type TerminationTestSuite struct {
suite.Suite
}
func TestTerminationTestSuite(t *testing.T) {
suite.Run(t, new(TerminationTestSuite))
}
func (s *TerminationTestSuite) Test_ParseTermination() {
term, err := pgproto.ParseTermination(bytes.NewReader(rawTerminationMessage[:]))
s.Nil(err)
s.NotNil(term)
s.Equal(rawTerminationMessage[:], term.Encode())
}
func BenchmarkParseTermination(b *testing.B) {
b.RunParallel(func(p *testing.PB) {
for p.Next() {
_, err := pgproto.ParseTermination(bytes.NewReader(rawTerminationMessage[:]))
if err != nil {
b.Error(err)
}
}
})
}
func (s *TerminationTestSuite) Test_ParseTermination_Empty() {
term, err := pgproto.ParseTermination(bytes.NewReader([]byte{}))
s.NotNil(err)
s.Nil(term)
}
func (s *TerminationTestSuite) Test_ParseTermination_InvalidTag() {
// lowercase 'x' instead of uppercase 'X'
raw := []byte{'x', '\x00', '\x00', '\x00', '\x04'}
term, err := pgproto.ParseTermination(bytes.NewReader(raw))
s.NotNil(err)
s.Nil(term)
}
func (s *TerminationTestSuite) Test_ParseTermination_InvalidLength() {
// length of 3 instead of 4
raw := []byte{'X', '\x00', '\x00', '\x00', '\x03'}
term, err := pgproto.ParseTermination(bytes.NewReader(raw))
s.NotNil(err)
s.Nil(term)
// length of 5 instead of 4
raw = []byte{'X', '\x00', '\x00', '\x00', '\x05'}
term, err = pgproto.ParseTermination(bytes.NewReader(raw))
s.NotNil(err)
s.Nil(term)
}
func (s *TerminationTestSuite) Test_Termination_Encode() {
term := &pgproto.Termination{}
s.Equal(rawTerminationMessage[:], term.Encode())
}
func BenchmarkTermination_Encode(b *testing.B) {
term := &pgproto.Termination{}
b.RunParallel(func(p *testing.PB) {
for p.Next() {
term.Encode()
}
})
}