Skip to content
This repository was archived by the owner on Nov 6, 2023. It is now read-only.

Commit a169dd6

Browse files
committed
#2 Add unit test and fix for potential bug dotnet/aspnetcore#6146
1 parent 1249885 commit a169dd6

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.IO;
2+
using System.Text;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.VisualStudio.TestTools.UnitTesting;
6+
7+
namespace VueCliMiddleware.Tests
8+
{
9+
[TestClass]
10+
public class EventedStreamReaderTests
11+
{
12+
[TestMethod]
13+
public async Task ReadChunks_MultipleNewLines_OnCompleteLineFiresEachNewline()
14+
{
15+
16+
string testMessage = "this \nis \na \ntest \nof \nmultiple \nnewlines\n trailing data";
17+
int numNewLines = testMessage.Split('\n').Length - 1;
18+
int chunksReceived = 0;
19+
int linesReceived = 0;
20+
21+
using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(testMessage)))
22+
{
23+
var streamReader = new StreamReader(ms);
24+
25+
var esr = new EventedStreamReader(streamReader);
26+
esr.OnReceivedChunk += (e) => Interlocked.Increment(ref chunksReceived);
27+
esr.OnReceivedLine += (e) => Interlocked.Increment(ref linesReceived);
28+
await Task.Delay(200);
29+
}
30+
31+
Assert.AreEqual(numNewLines, linesReceived);
32+
}
33+
}
34+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
8+
<RootNamespace>VueCliMiddleware</RootNamespace>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
13+
<PackageReference Include="MSTest.TestAdapter" Version="1.4.0" />
14+
<PackageReference Include="MSTest.TestFramework" Version="1.4.0" />
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\src\VueCliMiddleware.csproj" />
19+
</ItemGroup>
20+
21+
</Project>

src/Util/Internals.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
using System.Text;
1515
using System.Text.RegularExpressions;
1616

17+
18+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("VueCliMiddleware.Tests")]
1719
namespace VueCliMiddleware
1820
{
1921
internal static class LoggerFinder
@@ -154,17 +156,23 @@ private async Task Run()
154156

155157
OnChunk(new ArraySegment<char>(buf, 0, chunkLength));
156158

157-
var lineBreakPos = Array.IndexOf(buf, '\n', 0, chunkLength);
158-
if (lineBreakPos < 0)
159-
{
160-
_linesBuffer.Append(buf, 0, chunkLength);
161-
}
162-
else
159+
int lineBreakPos = -1;
160+
int startPos = 0;
161+
162+
// get all the newlines
163+
while ((lineBreakPos = Array.IndexOf(buf, '\n', startPos, chunkLength - startPos)) >= 0 && startPos < chunkLength)
163164
{
164-
_linesBuffer.Append(buf, 0, lineBreakPos + 1);
165+
var length = (lineBreakPos + 1) - startPos;
166+
_linesBuffer.Append(buf, startPos, length);
165167
OnCompleteLine(_linesBuffer.ToString());
166168
_linesBuffer.Clear();
167-
_linesBuffer.Append(buf, lineBreakPos + 1, chunkLength - (lineBreakPos + 1));
169+
startPos = lineBreakPos + 1;
170+
}
171+
172+
// get the rest
173+
if (lineBreakPos < 0 && startPos < chunkLength)
174+
{
175+
_linesBuffer.Append(buf, startPos, chunkLength);
168176
}
169177
}
170178
}

src/VueCliMiddleware.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ VisualStudioVersion = 15.0.28010.2026
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VueCliMiddleware", "VueCliMiddleware.csproj", "{6EF79200-935A-4EBB-BB10-EB233A360210}"
77
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VueCliMiddleware.Tests", "..\VueCliMiddleware.Tests\VueCliMiddleware.Tests.csproj", "{36DA789C-1C87-48A3-9A9E-804E9BA5C141}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
@@ -15,6 +17,10 @@ Global
1517
{6EF79200-935A-4EBB-BB10-EB233A360210}.Debug|Any CPU.Build.0 = Debug|Any CPU
1618
{6EF79200-935A-4EBB-BB10-EB233A360210}.Release|Any CPU.ActiveCfg = Release|Any CPU
1719
{6EF79200-935A-4EBB-BB10-EB233A360210}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{36DA789C-1C87-48A3-9A9E-804E9BA5C141}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{36DA789C-1C87-48A3-9A9E-804E9BA5C141}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{36DA789C-1C87-48A3-9A9E-804E9BA5C141}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{36DA789C-1C87-48A3-9A9E-804E9BA5C141}.Release|Any CPU.Build.0 = Release|Any CPU
1824
EndGlobalSection
1925
GlobalSection(SolutionProperties) = preSolution
2026
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)