MCPcopy Create free account
hub / github.com/apache/fory / TestInputStreamShrink

Function TestInputStreamShrink

go/fory/stream_test.go:216–261  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

214}
215
216func TestInputStreamShrink(t *testing.T) {
217 // Create a large payload that easily escapes the bufferSize (4096)
218 data := make([]byte, 10000)
219 for i := range data {
220 data[i] = byte(i % 256)
221 }
222
223 // Create a stream reader with a tiny bufferSize so we can trigger Shrink reliably
224 buf := bytes.NewReader(data)
225 sr := NewInputStreamWithBufferSize(buf, 100)
226
227 // Force a read/fill to pull a chunk into memory
228 err := sr.buffer.fill(5000, nil)
229 if !err {
230 t.Fatalf("Failed to fill buffer")
231 }
232
233 // Fake an artificial read that consumed a massive portion of the buffer
234 originalCapacity := cap(sr.buffer.data)
235 sr.buffer.readerIndex = 4500
236
237 // Trigger Shrink
238 sr.Shrink()
239
240 // 1. Validate reader index was successfully reset
241 if sr.buffer.readerIndex != 0 {
242 t.Errorf("Expected readerIndex to reset to 0, got %d", sr.buffer.readerIndex)
243 }
244
245 // 2. Validate the capacity actually shrank (reclaimed memory)
246 newCapacity := cap(sr.buffer.data)
247 if newCapacity >= originalCapacity {
248 t.Errorf("Expected capacity to shrink (was %d, now %d)", originalCapacity, newCapacity)
249 }
250
251 // 3. Validate the remaining unread data remained intact
252 if sr.buffer.writerIndex != 500 {
253 t.Errorf("Expected writerIndex to be 500 remaining bytes, got %d", sr.buffer.writerIndex)
254 }
255 for i := 0; i < 500; i++ {
256 if sr.buffer.data[i] != byte((4500+i)%256) {
257 t.Errorf("Data corruption post-shrink at index %d", i)
258 break
259 }
260 }
261}

Callers

nothing calls this directly

Calls 3

ShrinkMethod · 0.80
fillMethod · 0.45

Tested by

no test coverage detected