TestAsyncBufferReadAsync tests reading from AsyncBuffer using readAt method with full = false
(t *testing.T)
| 340 | // TestAsyncBufferReadAsync tests reading from AsyncBuffer using readAt method |
| 341 | // with full = false |
| 342 | func TestAsyncBufferReadAsync(t *testing.T) { |
| 343 | // Let's use source buffer which is 4.5 chunks long |
| 344 | source, bytesReader := generateSourceData(t, asyncbuffer.ChunkSize*3) |
| 345 | br := newBlockingReader(bytesReader) |
| 346 | ab := asyncbuffer.New(br, -1) |
| 347 | defer ab.Close() |
| 348 | |
| 349 | // flush the first chunk to allow reading |
| 350 | br.flushNextChunk() |
| 351 | |
| 352 | // Let's try to read first two chunks, however, |
| 353 | // we know that only the first chunk is available |
| 354 | target := make([]byte, asyncbuffer.ChunkSize*2) |
| 355 | n, err := ab.ReadAt(target, 0) |
| 356 | require.NoError(t, err) |
| 357 | assert.Equal(t, asyncbuffer.ChunkSize, n) |
| 358 | assert.Equal(t, target[:asyncbuffer.ChunkSize], source[:asyncbuffer.ChunkSize]) |
| 359 | |
| 360 | br.flushNextChunk() // unlock reader to allow read second chunk |
| 361 | ab.WaitFor(asyncbuffer.ChunkSize + 1) // wait for the second chunk to be available |
| 362 | |
| 363 | target = make([]byte, asyncbuffer.ChunkSize*2) |
| 364 | n, err = ab.ReadAt(target, 0) |
| 365 | require.NoError(t, err) |
| 366 | assert.Equal(t, asyncbuffer.ChunkSize*2, n) |
| 367 | assert.Equal(t, target, source[:asyncbuffer.ChunkSize*2]) |
| 368 | |
| 369 | br.flush() // Flush the rest of the data |
| 370 | ab.Wait() |
| 371 | |
| 372 | // Try to read near end of the stream, EOF |
| 373 | target = make([]byte, asyncbuffer.ChunkSize) |
| 374 | n, err = ab.ReadAt(target, asyncbuffer.ChunkSize*3-1) |
| 375 | require.NoError(t, err) |
| 376 | assert.Equal(t, 1, n) |
| 377 | assert.Equal(t, target[0], source[asyncbuffer.ChunkSize*3-1]) |
| 378 | |
| 379 | // Try to read beyond the end of the stream == eof |
| 380 | target = make([]byte, asyncbuffer.ChunkSize) |
| 381 | n, err = ab.ReadAt(target, asyncbuffer.ChunkSize*3) |
| 382 | require.ErrorIs(t, io.EOF, err) |
| 383 | assert.Equal(t, 0, n) |
| 384 | } |
| 385 | |
| 386 | // TestAsyncBufferWithDataLenAndExactReaderSize tests that AsyncBuffer doesn't |
| 387 | // return an error when the expected data length is set and matches the reader size |
nothing calls this directly
no test coverage detected