Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions Runtime/Scripts/DataStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ internal StreamError(Proto.StreamError proto) : base(proto.Description) { }
public abstract class ReadIncrementalInstructionBase<TContent> : StreamYieldInstruction
{
private readonly ulong _handleValue;
private readonly Queue<TContent> _pendingChunks = new();
private TContent _latestChunk;

/// <summary>
Expand Down Expand Up @@ -107,8 +108,26 @@ protected ReadIncrementalInstructionBase(FfiHandle readerHandle)

protected void OnChunk(TContent content)
{
_latestChunk = content;
IsCurrentReadDone = true;
if (IsCurrentReadDone)
{
// Consumer hasn't yielded since the last chunk; buffer until Reset().
_pendingChunks.Enqueue(content);
}
else
{
_latestChunk = content;
IsCurrentReadDone = true;
}
}

public override void Reset()
{
base.Reset();
if (_pendingChunks.Count > 0)
{
_latestChunk = _pendingChunks.Dequeue();
IsCurrentReadDone = true;
}
}

protected void OnEos(Proto.StreamError protoError)
Expand Down
44 changes: 44 additions & 0 deletions Tests/EditMode/DataStreamIncrementalReadTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using LiveKit.Internal;
using NUnit.Framework;

namespace LiveKit.EditModeTests
{
public class DataStreamTests
{
private sealed class TestIncrementalReader : ReadIncrementalInstructionBase<string>
{
public TestIncrementalReader(FfiHandle h) : base(h) { }
public void PushChunk(string content) => OnChunk(content);
public string Value => LatestChunk;
}

[Test]
public void OnChunk_MultipleChunksBeforeConsumerDrains_AllChunksAreObserved()
{
using var handle = new FfiHandle(IntPtr.Zero);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: I'm probably missing something obvious, but how does creating a zero FfiHandle work here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a null pointer and we just need it to satisfy the constructor. In the TestIncrementalReader we can push chunks to mock callbacks without needing any native stuff, so the null handle is never used. Does that answer your question?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And on cleanup we check if the handle is valid, so it would also not be released.

var reader = new TestIncrementalReader(handle);

// Three ChunkReceived events arrive back-to-back on the sync context,
// before the consumer coroutine resumes from its yield.
reader.PushChunk("A");
reader.PushChunk("B");
reader.PushChunk("C");

// Drain like a consumer would: read the current chunk, Reset, loop
// while more is readable.
var observed = new List<string>();
while (!reader.keepWaiting)
{
observed.Add(reader.Value);
if (reader.IsEos) break;
reader.Reset();
}

CollectionAssert.AreEqual(new[] { "A", "B", "C" }, observed,
"ReadIncrementalInstructionBase dropped chunks when multiple " +
"ChunkReceived events arrived before the consumer could yield.");
}
}
}
11 changes: 11 additions & 0 deletions Tests/EditMode/DataStreamIncrementalReadTests.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading