Pārlūkot izejas kodu

#177 flush buffer should check available again

Tao Wen 8 gadi atpakaļ
vecāks
revīzija
dbb1ef3f63
2 mainītis faili ar 19 papildinājumiem un 0 dzēšanām
  1. 3 0
      feature_stream.go
  2. 16 0
      jsoniter_stream_test.go

+ 3 - 0
feature_stream.go

@@ -191,6 +191,9 @@ func (stream *Stream) ensure(minimal int) {
 func (stream *Stream) growAtLeast(minimal int) {
 	if stream.out != nil {
 		stream.Flush()
+		if stream.Available() >= minimal {
+			return
+		}
 	}
 	toGrow := len(stream.buf)
 	if toGrow < minimal {

+ 16 - 0
jsoniter_stream_test.go

@@ -51,3 +51,19 @@ func Test_writeString_should_grow_buffer(t *testing.T) {
 	should.Nil(stream.Error)
 	should.Equal(`"123"`, string(stream.Buffer()))
 }
+
+type NopWriter struct {
+	bufferSize int
+}
+
+func (w *NopWriter) Write(p []byte) (n int, err error) {
+	w.bufferSize = cap(p)
+	return len(p), nil
+}
+
+func Test_flush_buffer_should_stop_grow_buffer(t *testing.T) {
+	writer := new(NopWriter)
+	NewEncoder(writer).Encode(make([]int, 10000000))
+	should := require.New(t)
+	should.Equal(512, writer.bufferSize)
+}