mirror of
https://github.com/json-iterator/go.git
synced 2025-01-23 18:54:21 +02:00
#177 flush buffer should check available again
This commit is contained in:
parent
46b20bbbec
commit
dbb1ef3f63
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user