commit 55210b4eb4e7e983f6be80056f9889654d4126da
parent 53f587de6e763ea4f336ed2c8fabb09a02ca0eef
Author: francoispqt <francois@parquet.ninja>
Date: Thu, 3 May 2018 00:48:31 +0800
add better cancellation
Diffstat:
3 files changed, 23 insertions(+), 2 deletions(-)
diff --git a/decode.go b/decode.go
@@ -309,10 +309,9 @@ func (dec *Decoder) read() bool {
}
func (dec *Decoder) nextChar() byte {
- for dec.cursor < dec.length || dec.read() {
+ for ; dec.cursor < dec.length || dec.read(); dec.cursor++ {
switch dec.data[dec.cursor] {
case ' ', '\n', '\t', '\r', ',':
- dec.cursor = dec.cursor + 1
continue
}
d := dec.data[dec.cursor]
diff --git a/decode_stream.go b/decode_stream.go
@@ -63,6 +63,7 @@ func (dec *StreamDecoder) DecodeStream(c UnmarshalerStream) error {
return nil
}
}
+ close(dec.done)
return InvalidJSONError("Invalid JSON while parsing line delimited JSON")
}
diff --git a/decode_stream_test.go b/decode_stream_test.go
@@ -2,6 +2,7 @@ package gojay
import (
"context"
+ "errors"
"io"
"testing"
"time"
@@ -311,6 +312,20 @@ loop:
testCase.expectations(dec.Err(), result, t)
}
+func TestStreamDecodingErr(t *testing.T) {
+ testChan := ChannelStreamStrings(make(chan *string))
+ dec := Stream.NewDecoder(&StreamReaderErr{})
+ // start decoding (will block the goroutine until something is written to the ReadWriter)
+ go dec.DecodeStream(testChan)
+ select {
+ case <-dec.Done():
+ assert.NotNil(t, dec.Err(), "dec.Err() should not be nil")
+ case <-testChan:
+ assert.True(t, false, "should not be called")
+ }
+
+}
+
type ChannelStreamStrings chan *string
func (c ChannelStreamStrings) UnmarshalStream(dec *StreamDecoder) error {
@@ -360,6 +375,12 @@ func (r *StreamReader) Read(b []byte) (int, error) {
}
}
+type StreamReaderErr struct{}
+
+func (r *StreamReaderErr) Read(b []byte) (int, error) {
+ return 0, errors.New("Test Error")
+}
+
// Deadline test
func TestStreamDecodingDeadline(t *testing.T) {
dec := Stream.NewDecoder(&StreamReader{})