forked from francoispqt/gojay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode_stream_test.go
375 lines (335 loc) · 12 KB
/
decode_stream_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package gojay
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// Basic Behaviour Tests
//
func TestDecoderImplementsContext(t *testing.T) {
var dec interface{} = &StreamDecoder{}
_ = dec.(context.Context)
}
func TestDecodeStreamNoReader(t *testing.T) {
dec := Stream.NewDecoder(nil)
dec.done = make(chan struct{}, 1)
testChan := ChannelStreamObjects(make(chan *TestObj))
go dec.DecodeStream(&testChan)
select {
case <-dec.Done():
assert.NotNil(t, dec.Err(), "dec.Err() should not be nil")
assert.Equal(t, "No reader given to decode stream", dec.Err().Error(), "dec.Err().Error() should not be 'No reader given to decode stream'")
case <-testChan:
assert.True(t, false, "should not be called as decoder should not return error right away")
}
}
// Table Tests
// Objects
type StreamTestObject struct {
name string
streamReader *StreamReader
expectations func(error, []*TestObj, *testing.T)
}
func TestStreamDecodingObjectsParallel(t *testing.T) {
var tests = []StreamTestObject{
{
name: "Stream objects",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
{"test":246,"test2":-246,"test3":"string"}
{"test":247,"test2":248,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
`,
},
expectations: func(err error, result []*TestObj, t *testing.T) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 246, result[0].test, "v[0].test should be equal to 246")
assert.Equal(t, -246, result[0].test2, "v[0].test2 should be equal to -247")
assert.Equal(t, "string", result[0].test3, "v[0].test3 should be equal to \"string\"")
assert.Equal(t, 247, result[1].test, "result[1].test should be equal to 246")
assert.Equal(t, 248, result[1].test2, "result[1].test2 should be equal to 248")
assert.Equal(t, "string", result[1].test3, "result[1].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[2].test, "result[2].test should be equal to 777")
assert.Equal(t, 456, result[2].test2, "result[2].test2 should be equal to 456")
assert.Equal(t, "string", result[2].test3, "result[2].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[3].test, "result[3].test should be equal to 777")
assert.Equal(t, 456, result[3].test2, "result[3].test2 should be equal to 456")
assert.Equal(t, "string", result[3].test3, "result[3].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[4].test, "result[4].test should be equal to 777")
assert.Equal(t, 456, result[4].test2, "result[4].test2 should be equal to 456")
assert.Equal(t, "string", result[4].test3, "result[4].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[5].test, "result[5].test should be equal to 777")
assert.Equal(t, 456, result[5].test2, "result[5].test2 should be equal to 456")
assert.Equal(t, "string", result[5].test3, "result[5].test3 should be equal to \"string\"")
},
},
{
name: "Stream test objects with null values",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
{"test":246,"test2":-246,"test3":"string"}
{"test":247,"test2":248,"test3":"string"}
null
{"test":777,"test2":456,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
{"test":777,"test2":456,"test3":"string"}
`,
},
expectations: func(err error, result []*TestObj, t *testing.T) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 246, result[0].test, "v[0].test should be equal to 246")
assert.Equal(t, -246, result[0].test2, "v[0].test2 should be equal to -247")
assert.Equal(t, "string", result[0].test3, "v[0].test3 should be equal to \"string\"")
assert.Equal(t, 247, result[1].test, "result[1].test should be equal to 246")
assert.Equal(t, 248, result[1].test2, "result[1].test2 should be equal to 248")
assert.Equal(t, "string", result[1].test3, "result[1].test3 should be equal to \"string\"")
assert.Equal(t, 0, result[2].test, "result[2].test should be equal to 0 as input is null")
assert.Equal(t, 0, result[2].test2, "result[2].test2 should be equal to 0 as input is null")
assert.Equal(t, "", result[2].test3, "result[2].test3 should be equal to \"\" as input is null")
assert.Equal(t, 777, result[3].test, "result[3].test should be equal to 777")
assert.Equal(t, 456, result[3].test2, "result[3].test2 should be equal to 456")
assert.Equal(t, "string", result[3].test3, "result[3].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[4].test, "result[4].test should be equal to 777")
assert.Equal(t, 456, result[4].test2, "result[4].test2 should be equal to 456")
assert.Equal(t, "string", result[4].test3, "result[4].test3 should be equal to \"string\"")
assert.Equal(t, 777, result[5].test, "result[5].test should be equal to 777")
assert.Equal(t, 456, result[5].test2, "result[5].test2 should be equal to 456")
assert.Equal(t, "string", result[5].test3, "result[5].test3 should be equal to \"string\"")
},
},
{
name: "Stream test starting with null values",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
null
{"test":246,"test2":-246,"test3":"string"}
{"test":247,"test2":248,"test3":"string"}
`,
},
expectations: func(err error, result []*TestObj, t *testing.T) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, 0, result[0].test, "result[0].test should be equal to 0 as input is null")
assert.Equal(t, 0, result[0].test2, "result[0].test2 should be equal to 0 as input is null")
assert.Equal(t, "", result[0].test3, "result[0].test3 should be equal to \"\" as input is null")
assert.Equal(t, 246, result[1].test, "v[1].test should be equal to 246")
assert.Equal(t, -246, result[1].test2, "v[1].test2 should be equal to -247")
assert.Equal(t, "string", result[1].test3, "v[1].test3 should be equal to \"string\"")
assert.Equal(t, 247, result[2].test, "result[2].test should be equal to 246")
assert.Equal(t, 248, result[2].test2, "result[2].test2 should be equal to 248")
assert.Equal(t, "string", result[2].test3, "result[2].test3 should be equal to \"string\"")
},
},
{
name: "Stream test invalid JSON",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
invalid json
{"test":246,"test2":-246,"test3":"string"}
{"test":247,"test2":248,"test3":"string"}
`,
},
expectations: func(err error, result []*TestObj, t *testing.T) {
assert.NotNil(t, err, "err is not nil as JSON is invalid")
assert.IsType(t, InvalidJSONError(""), err, "err is of type InvalidJSONError")
assert.Equal(t, "Invalid JSON", err.Error(), "err message is Invalid JSON")
},
},
}
for _, testCase := range tests {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
runStreamTestCaseObjects(t, testCase)
})
}
}
func runStreamTestCaseObjects(t *testing.T, testCase StreamTestObject) {
// create our channel which will receive our objects
testChan := ChannelStreamObjects(make(chan *TestObj))
dec := Stream.NewDecoder(testCase.streamReader)
// start decoding (will block the goroutine until something is written to the ReadWriter)
go dec.DecodeStream(&testChan)
// start writing to the ReadWriter
go testCase.streamReader.Write()
// prepare our result
result := []*TestObj{}
loop:
for {
select {
case v := <-testChan:
result = append(result, v)
case <-dec.Done():
break loop
}
}
testCase.expectations(dec.Err(), result, t)
}
type ChannelStreamObjects chan *TestObj
func (c *ChannelStreamObjects) UnmarshalStream(dec *StreamDecoder) error {
obj := &TestObj{}
if err := dec.AddObject(obj); err != nil {
return err
}
*c <- obj
return nil
}
// Strings
type StreamTestString struct {
name string
streamReader *StreamReader
expectations func(error, []*string, *testing.T)
}
func TestStreamDecodingStringsParallel(t *testing.T) {
var tests = []StreamTestString{
{
name: "Stream strings basic",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
"hello"
"world"
"!"
`,
},
expectations: func(err error, result []*string, t *testing.T) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, "hello", *result[0], "v[0] should be equal to 'hello'")
assert.Equal(t, "world", *result[1], "v[1] should be equal to 'world'")
assert.Equal(t, "!", *result[2], "v[2] should be equal to '!'")
},
},
{
name: "Stream strings with null",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
"hello"
null
"!"
`,
},
expectations: func(err error, result []*string, t *testing.T) {
assert.Nil(t, err, "err should be nil")
assert.Equal(t, "hello", *result[0], "v[0] should be equal to 'hello'")
assert.Equal(t, "", *result[1], "v[1] should be equal to ''")
assert.Equal(t, "!", *result[2], "v[2] should be equal to '!'")
},
},
{
name: "Stream strings invalid JSON",
streamReader: &StreamReader{
readChan: make(chan string),
done: make(chan struct{}),
data: `
"hello"
world
"!"
`,
},
expectations: func(err error, result []*string, t *testing.T) {
assert.NotNil(t, err, "err should not be nil")
assert.IsType(t, InvalidJSONError(""), err, "err is of type InvalidJSONError")
assert.Equal(t, "Invalid JSON", err.Error(), "err message is Invalid JSON")
},
},
}
for _, testCase := range tests {
testCase := testCase
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()
runStreamTestCaseStrings(t, testCase)
})
}
}
func runStreamTestCaseStrings(t *testing.T, testCase StreamTestString) {
// create our channel which will receive our objects
testChan := ChannelStreamStrings(make(chan *string))
dec := Stream.NewDecoder(testCase.streamReader)
// start decoding (will block the goroutine until something is written to the ReadWriter)
go dec.DecodeStream(&testChan)
// start writing to the ReadWriter
go testCase.streamReader.Write()
// prepare our result
result := []*string{}
loop:
for {
select {
case v := <-testChan:
result = append(result, v)
case <-dec.Done():
break loop
}
}
testCase.expectations(dec.Err(), result, t)
}
type ChannelStreamStrings chan *string
func (c *ChannelStreamStrings) UnmarshalStream(dec *StreamDecoder) error {
str := ""
if err := dec.AddString(&str); err != nil {
return err
}
*c <- &str
return nil
}
// StreamReader mocks a stream reading chunks of data
type StreamReader struct {
writeCounter int
readChan chan string
done chan struct{}
data string
}
func (r *StreamReader) Write() {
l := len(r.data)
t := 4
chunkSize := l / t
carry := 0
lastWrite := 0
for r.writeCounter < t {
time.Sleep(time.Duration(r.writeCounter*100) * time.Millisecond)
currentChunkStart := (chunkSize) * r.writeCounter
lastWrite := currentChunkStart + chunkSize
r.readChan <- r.data[currentChunkStart:lastWrite]
carry = l - lastWrite
r.writeCounter++
}
if carry > 0 {
r.readChan <- r.data[lastWrite:]
}
r.done <- struct{}{}
}
func (r *StreamReader) Read(b []byte) (int, error) {
select {
case v := <-r.readChan:
n := copy(b, v)
return n, nil
case <-r.done:
return 0, nil
}
}
// Deadline test
func TestStreamDecodingDeadline(t *testing.T) {
dec := Stream.NewDecoder(&StreamReader{})
now := time.Now()
dec.SetDeadline(now)
deadline, _ := dec.Deadline()
assert.Equal(t, now.String(), deadline.String(), "dec.now and now should be equal")
assert.Equal(t, now.String(), dec.deadline.String(), "dec.now and now should be equal")
}
func TestStreamDecodingErrNotSet(t *testing.T) {
dec := Stream.NewDecoder(&StreamReader{})
assert.Nil(t, dec.Err(), "dec.Err should be nim")
}