Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed May 31, 2024
1 parent ae05019 commit 0fa48f0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 58 deletions.
8 changes: 4 additions & 4 deletions src/libraries/System.Net.ServerSentEvents/src/PACKAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ Asynchronously parsing event contents as strings

```csharp
using HttpClient client = new();
using Stream response = await client.GetStreamAsync("https://localhost:12345/sse");
await foreach (SseItem<string> item in SseParser.Create(response).EnumerateAsync())
using Stream stream = await client.GetStreamAsync("https://localhost:12345/sse");
await foreach (SseItem<string> item in SseParser.Create(stream).EnumerateAsync())
{
Console.WriteLine(item.Data);
}
Expand All @@ -29,9 +29,9 @@ Synchronously parsing event contents as JSON

```csharp
MemoryStream stream = new(data);
foreach (SseItem<Book> item in SseParser.Create(response, (eventType, bytes) => JsonSerializer.Deserialize<Book>(bytes)).Enumerate())
foreach (SseItem<Book> item in SseParser.Create(stream, (eventType, bytes) => JsonSerializer.Deserialize<Book>(bytes)).Enumerate())
{
Console.WriteLine(item.Author);
Console.WriteLine(item.Data.Author);
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,4 @@
<data name="InvalidOperation_EnumerateOnlyOnce" xml:space="preserve">
<value>The enumerable may be enumerated only once.</value>
</data>
<data name="InvalidOperation_InvalidStreamNegativeBytes" xml:space="preserve">
<value>The Stream implementation is invalid, returning a negative number from a read operation.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ private bool ProcessLine(out SseItem<T> sseItem, out int advance)
if (fieldName.SequenceEqual("data"u8))
{
// Spec: "Append the field value to the data buffer, then append a single U+000A LINE FEED (LF) character to the data buffer."
// Spec: "If the data buffer's last character is a U+000A LINE FEED (LF) character, then remove the last character from the data buffer."

// If there's nothing currently in the data buffer and we can easily detect that this line is immediately followed by
// an empty line, we can optimize it to just handle the data directly from the line buffer, rather than first copying
Expand Down Expand Up @@ -462,16 +463,16 @@ private int FillLineBuffer()
_lineBuffer, offset, _lineBuffer.Length - offset);
#endif

if (bytesRead <= 0)
if (bytesRead > 0)
{
_lineLength += bytesRead;
}
else
{
_eof = true;
if (bytesRead < 0)
{
throw new InvalidOperationException(SR.InvalidOperation_InvalidStreamNegativeBytes);
}
bytesRead = 0;
}

_lineLength += bytesRead;
return bytesRead;
}

Expand All @@ -489,16 +490,16 @@ private async ValueTask<int> FillLineBufferAsync(CancellationToken cancellationT
#endif
.ConfigureAwait(false);

if (bytesRead <= 0)
if (bytesRead > 0)
{
_lineLength += bytesRead;
}
else
{
_eof = true;
if (bytesRead < 0)
{
throw new InvalidOperationException(SR.InvalidOperation_InvalidStreamNegativeBytes);
}
bytesRead = 0;
}

_lineLength += bytesRead;
return bytesRead;
}

Expand Down
38 changes: 0 additions & 38 deletions src/libraries/System.Net.ServerSentEvents/tests/SseParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,27 +726,6 @@ public void NonGenericEnumerator_ProducesExpectedItems()
Assert.False(e.MoveNext());
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task InvalidStream_ReturnsNegativeValueFromRead_Throws(bool useAsync)
{
using Stream stream = new InvalidReadStream();

SseParser<string> parser = SseParser.Create(stream);

if (useAsync)
{
await using IAsyncEnumerator<SseItem<string>> e = parser.EnumerateAsync().GetAsyncEnumerator();
await Assert.ThrowsAsync<InvalidOperationException>(async () => await e.MoveNextAsync());
}
else
{
using IEnumerator<SseItem<string>> e = parser.Enumerate().GetEnumerator();
Assert.Throws<InvalidOperationException>(() => e.MoveNext());
}
}

[Theory]
[MemberData(nameof(NewlineTrickleAsyncData))]
public async Task MultipleItemParsers_OpenAI_StreamingResponse(string newline, bool trickle, bool useAsync)
Expand Down Expand Up @@ -978,23 +957,6 @@ public override async ValueTask<int> ReadAsync(Memory<byte> buffer, Cancellation
await Task.Yield();
return await base.ReadAsync(buffer.Slice(0, Math.Min(buffer.Length, 1)), cancellationToken);
}
#endif
}

private sealed class InvalidReadStream : MemoryStream
{
public override int Read(byte[] buffer, int offset, int count) =>
-1;

public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) =>
Task.FromResult(-1);

#if NET
public override int Read(Span<byte> buffer) =>
-1;

public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) =>
ValueTask.FromResult(-1);
#endif
}
}
Expand Down

0 comments on commit 0fa48f0

Please sign in to comment.