MCPcopy Create free account
hub / github.com/Pagghiu/SaneCppLibraries / unshift

Method unshift

Tests/Libraries/AsyncStreams/AsyncStreamsTest.cpp:484–555  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

482}
483
484void SC::AsyncStreamsTest::unshift()
485{
486 AsyncBufferView::ID bufferID;
487 Span<char> data;
488 AsyncBufferView buffers[1];
489 Buffer buffer;
490 SC_TEST_EXPECT(buffer.resizeWithoutInitializing(123));
491 buffers[0] = buffer.toSpan();
492 buffers[0].setReusable(true);
493
494 AsyncBuffersPool pool;
495 pool.setBuffers(buffers);
496
497 struct TestReadableStream : public AsyncReadableStream
498 {
499 int step = 0;
500 bool success = true;
501
502 virtual Result asyncRead() override
503 {
504 step++;
505 // We do nothing here, just waiting for push
506 return Result(true);
507 }
508 };
509 TestReadableStream readable;
510 AsyncReadableStream::Request requests[3];
511 readable.setReadQueue(requests); // Capacity 2
512 SC_TEST_EXPECT(readable.init(pool));
513
514 SC_TEST_EXPECT(readable.getBuffersPool().requestNewBuffer(123, bufferID, data));
515
516 // 1. Manually unshift a buffer
517 char content[] = "123";
518 memcpy(data.data(), content, 3);
519 readable.getBuffersPool().setNewBufferSize(bufferID, 3);
520 SC_TRUST_RESULT(readable.unshift(bufferID));
521 // Release our reference so that the stream is the only owner and can recycle it after emission
522 pool.unrefBuffer(bufferID);
523
524 // 2. Start reading, it should immediately receive the unshifted buffer
525 SC_TEST_EXPECT(readable.eventData.addListener(
526 [&readable](AsyncBufferView::ID id)
527 {
528 Span<const char> readData;
529 SC_TRUST_RESULT(readable.getBuffersPool().getReadableData(id, readData));
530 StringView str = StringView(readData, false, StringEncoding::Ascii);
531 if (str != "123")
532 readable.success = false;
533 // Should be received before asyncRead is even called or right at start
534 if (readable.step != 0)
535 readable.success = false;
536 }));
537
538 SC_TRUST_RESULT(readable.start());
539 SC_TEST_EXPECT(readable.success);
540
541 // Cleanup to allow re-use of buffer for next check

Callers

nothing calls this directly

Calls 10

toSpanMethod · 0.80
requestNewBufferMethod · 0.80
setNewBufferSizeMethod · 0.80
unrefBufferMethod · 0.80
getReadableDataMethod · 0.80
initMethod · 0.45
dataMethod · 0.45
startMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected