forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
188 lines (155 loc) · 5.74 KB
/
parser_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) quickfixengine.org All rights reserved.
//
// This file may be distributed under the terms of the quickfixengine.org
// license as defined by quickfixengine.org and appearing in the file
// LICENSE included in the packaging of this file.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE.
//
// See http://www.quickfixengine.org/LICENSE for licensing information.
//
// Contact [email protected] if any conditions of this licensing
// are not clear to you.
package quickfix
import (
"strings"
"testing"
"github.com/stretchr/testify/suite"
)
func BenchmarkParser_ReadMessage(b *testing.B) {
stream := "8=FIXT.1.19=11135=D34=449=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=2348=FIXT.1.19=9535=D34=549=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=198"
for i := 0; i < b.N; i++ {
reader := strings.NewReader(stream)
parser := newParser(reader)
_, _ = parser.ReadMessage()
}
}
type ParserSuite struct {
suite.Suite
*parser
}
func TestParserSuite(t *testing.T) {
suite.Run(t, new(ParserSuite))
}
func (s *ParserSuite) SetupTest() {
s.parser = new(parser)
}
func (s *ParserSuite) TestInvalidNilLength() {
stream := "8=\x019=\x01"
s.reader = strings.NewReader(stream)
_, err := s.ReadMessage()
s.NotNil(err)
}
func (s *ParserSuite) TestOverflowLength() {
stream := "8=\x019=9300000000000000000\x01"
s.reader = strings.NewReader(stream)
_, err := s.ReadMessage()
s.NotNil(err)
}
func (s *ParserSuite) TestJumpLength() {
stream := "8=FIXT.1.19=11135=D34=449=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=2348=FIXT.1.19=9535=D34=549=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=198"
s.reader = strings.NewReader(stream)
index, err := s.parser.jumpLength()
s.Nil(err)
expectedIndex := 111 + 17 - 1
s.Equal(expectedIndex, index)
}
func (s *ParserSuite) TestBadLength() {
stream := "8=FIXT.1.19=11135=D34=449=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=2348=FIXT.1.19=9535=D34=549=TW52=20140511-23:10:3456=ISLD11=ID21=340=154=155=INTC60=20140511-23:10:3410=198"
s.reader = strings.NewReader(stream)
bytes, _ := s.parser.ReadMessage()
s.Equal(stream, bytes.String())
}
func (s *ParserSuite) TestFindStart() {
var testCases = []struct {
stream string
expectError bool
expectedStart int
}{
{stream: "", expectError: true},
{stream: "nostarthere", expectError: true},
{stream: "hello8=FIX.4.0", expectedStart: 5},
}
for _, tc := range testCases {
s.SetupTest()
s.reader = strings.NewReader(tc.stream)
start, err := s.parser.findStart()
if tc.expectError {
s.NotNil(err)
continue
}
s.Nil(err)
s.Equal(tc.expectedStart, start)
}
}
func (s *ParserSuite) TestReadEOF() {
var testCases = []struct {
stream string
}{
{""},
{"hello8=FIX.4.0"},
}
for _, tc := range testCases {
s.SetupTest()
s.reader = strings.NewReader(tc.stream)
bytes, err := s.parser.ReadMessage()
s.Nil(bytes)
s.NotNil(err)
s.Equal("EOF", err.Error())
}
}
func (s *ParserSuite) TestReadMessage() {
stream := "hello8=FIX.4.09=5blah10=1038=FIX.4.09=4foo10=103"
s.reader = strings.NewReader(stream)
var testCases = []struct {
expectedBytes string
expectedBufferLen int
expectedBufferCap int
}{
{expectedBytes: "8=FIX.4.09=5blah10=103", expectedBufferCap: defaultBufSize - 31, expectedBufferLen: len(stream) - 31},
{expectedBytes: "8=FIX.4.09=4foo10=103", expectedBufferCap: defaultBufSize - 31 - 25, expectedBufferLen: 0},
}
for _, tc := range testCases {
msg, err := s.parser.ReadMessage()
s.Nil(err)
s.Equal(tc.expectedBytes, msg.String())
s.Equal(tc.expectedBufferCap, cap(s.parser.buffer))
s.Equal(tc.expectedBufferLen, len(s.parser.buffer))
}
}
func (s *ParserSuite) TestReadMessageGrowBuffer() {
stream := "hello8=FIX.4.09=5blah10=1038=FIX.4.09=4foo10=103"
var testCases = []struct {
initialBufCap int
expectedBytes string
expectedBufferLen int
expectedBufferCap int
expectedBigBufferLen int
}{
{initialBufCap: 0, expectedBufferCap: defaultBufSize - 31, expectedBufferLen: len(stream) - 31, expectedBigBufferLen: defaultBufSize},
{initialBufCap: 4, expectedBufferCap: 6, expectedBufferLen: 6, expectedBigBufferLen: 32},
{initialBufCap: 8, expectedBufferCap: 6, expectedBufferLen: 6, expectedBigBufferLen: 32},
{initialBufCap: 14, expectedBufferCap: 10, expectedBufferLen: 10, expectedBigBufferLen: 36},
{initialBufCap: 16, expectedBufferCap: 18, expectedBufferLen: 18, expectedBigBufferLen: 44},
{initialBufCap: 23, expectedBufferCap: 10, expectedBufferLen: 10, expectedBigBufferLen: 36},
{initialBufCap: 30, expectedBufferCap: 24, expectedBufferLen: 24, expectedBigBufferLen: 50},
{initialBufCap: 31, expectedBufferCap: 0, expectedBufferLen: 0, expectedBigBufferLen: 31},
{initialBufCap: 40, expectedBufferCap: 9, expectedBufferLen: 9, expectedBigBufferLen: 40},
{initialBufCap: 60, expectedBufferCap: 29, expectedBufferLen: 25, expectedBigBufferLen: 60},
{initialBufCap: 80, expectedBufferCap: 49, expectedBufferLen: 25, expectedBigBufferLen: 80},
}
for _, tc := range testCases {
s.SetupTest()
s.reader = strings.NewReader(stream)
s.parser.bigBuffer = make([]byte, tc.initialBufCap)
s.parser.buffer = s.parser.bigBuffer[0:0]
msg, err := s.parser.ReadMessage()
s.Nil(err)
s.Equal("8=FIX.4.09=5blah10=103", msg.String())
s.Equal(tc.expectedBigBufferLen, len(s.parser.bigBuffer))
s.Equal(tc.expectedBufferCap, cap(s.parser.buffer))
s.Equal(tc.expectedBufferLen, len(s.parser.buffer))
}
}