forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
189 lines (151 loc) · 5.55 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
189
package quickfix
import (
"strings"
"testing"
)
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()
}
}
func TestParser_JumpLength(t *testing.T) {
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"
reader := strings.NewReader(stream)
parser := newParser(reader)
index, err := parser.jumpLength()
if err != nil {
t.Error("Unexpected error: ", err)
}
expectedIndex := 111 + 17 - 1
if index != expectedIndex {
t.Error("expected index ", expectedIndex, " got ", index)
}
}
func TestParser_BadLength(t *testing.T) {
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"
reader := strings.NewReader(stream)
parser := newParser(reader)
bytes, _ := parser.ReadMessage()
if stream != string(bytes) {
t.Errorf("Expected %v, got %v", stream, string(bytes))
}
}
func TestParser_findStart(t *testing.T) {
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 {
reader := strings.NewReader(tc.stream)
parser := newParser(reader)
start, err := parser.findStart()
if err != nil {
if !tc.expectError {
t.Error("unxpected error", err)
}
continue
}
if err == nil && tc.expectError {
t.Error("expected error")
}
if start != tc.expectedStart {
t.Errorf("Expected start to be %v, but was %v", tc.expectedStart, start)
}
}
}
func TestParser_ReadEOF(t *testing.T) {
var testCases = []struct {
stream string
}{
{""},
{"hello8=FIX.4.0"},
}
for _, tc := range testCases {
reader := strings.NewReader(tc.stream)
parser := newParser(reader)
bytes, err := parser.ReadMessage()
if len(bytes) != 0 {
t.Error("Length of bytes should be 0")
}
if err == nil || err.Error() != "EOF" {
t.Error("Expected EOF")
}
}
}
func TestParser_ReadMessage(t *testing.T) {
stream := "hello8=FIX.4.09=5blah10=1038=FIX.4.09=4foo10=103"
reader := strings.NewReader(stream)
parser := newParser(reader)
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 := parser.ReadMessage()
if err != nil {
t.Error("Unexpected error", err)
}
if tc.expectedBytes != string(msg) {
t.Errorf("Expected %v got %v", tc.expectedBytes, string(msg))
}
if len(tc.expectedBytes) != cap(msg) {
t.Errorf("Expected capacity %v got %v", len(tc.expectedBytes), cap(msg))
}
if cap(parser.buffer) != tc.expectedBufferCap {
t.Errorf("Expected capacity %v got %v", tc.expectedBufferCap, cap(parser.buffer))
}
if len(parser.buffer) != tc.expectedBufferLen {
t.Errorf("Expected len %v got %v", tc.expectedBufferLen, len(parser.buffer))
}
}
}
func TestParser_ReadMessageGrowBuffer(t *testing.T) {
stream := "hello8=FIX.4.09=5blah10=1038=FIX.4.09=4foo10=103"
var testCases = []struct {
initialBufCap int
expectedBytes string
expectedBufferLen int
expectedBufferCap int
}{
{initialBufCap: 0, expectedBufferCap: defaultBufSize - 31, expectedBufferLen: len(stream) - 31},
{initialBufCap: 4, expectedBufferCap: defaultBufSize - 27, expectedBufferLen: len(stream) - 31},
{initialBufCap: 8, expectedBufferCap: defaultBufSize - 23, expectedBufferLen: len(stream) - 31},
{initialBufCap: 14, expectedBufferCap: defaultBufSize - 17, expectedBufferLen: len(stream) - 31},
{initialBufCap: 16, expectedBufferCap: defaultBufSize - 15, expectedBufferLen: len(stream) - 31},
{initialBufCap: 23, expectedBufferCap: defaultBufSize - 8, expectedBufferLen: len(stream) - 31},
{initialBufCap: 30, expectedBufferCap: defaultBufSize - 1, expectedBufferLen: len(stream) - 31},
{initialBufCap: 31, expectedBufferCap: 0, expectedBufferLen: 0},
{initialBufCap: 40, expectedBufferCap: 9, expectedBufferLen: 9},
{initialBufCap: 60, expectedBufferCap: 29, expectedBufferLen: 25},
{initialBufCap: 80, expectedBufferCap: 49, expectedBufferLen: 25},
}
for _, tc := range testCases {
parser := newParser(strings.NewReader(stream))
parser.buffer = make([]byte, 0, tc.initialBufCap)
msg, err := parser.ReadMessage()
if err != nil {
t.Fatal("unexpected err: ", err)
}
if string(msg) != "8=FIX.4.09=5blah10=103" {
t.Error("Didn't get expected message, got ", string(msg))
}
if cap(parser.buffer) != tc.expectedBufferCap {
t.Errorf("expected cap %v, got %v", tc.expectedBufferCap, cap(parser.buffer))
}
if len(parser.buffer) != tc.expectedBufferLen {
t.Errorf("expected len %v, got %v", tc.expectedBufferLen, len(parser.buffer))
}
}
}