Skip to content

Commit

Permalink
Fix trace identifier tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Jan 13, 2022
1 parent b757ba3 commit 7521aba
Showing 1 changed file with 50 additions and 25 deletions.
75 changes: 50 additions & 25 deletions src/Servers/Kestrel/Core/test/Http1/Http1ConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Buffers;
using System.Collections;
using System.IO.Pipelines;
using System.Text;
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -146,21 +147,6 @@ public void ResetResetsScheme()
Assert.Equal("http", ((IFeatureCollection)_http1Connection).Get<IHttpRequestFeature>().Scheme);
}

[Fact]
public void ResetResetsTraceIdentifier()
{
_http1Connection.TraceIdentifier = "xyz";

_http1Connection.Reset();

var nextId = ((IFeatureCollection)_http1Connection).Get<IHttpRequestIdentifierFeature>().TraceIdentifier;
Assert.NotEqual("xyz", nextId);

_http1Connection.Reset();
var secondId = ((IFeatureCollection)_http1Connection).Get<IHttpRequestIdentifierFeature>().TraceIdentifier;
Assert.NotEqual(nextId, secondId);
}

[Fact]
public void ResetResetsMinRequestBodyDataRate()
{
Expand All @@ -182,31 +168,73 @@ public void ResetResetsMinResponseDataRate()
}

[Fact]
public void TraceIdentifierCountsRequestsPerHttp1Connection()
public async Task TraceIdentifierCountsRequestsPerHttp1Connection()
{
var connectionId = _http1ConnectionContext.ConnectionId;
var feature = ((IFeatureCollection)_http1Connection).Get<IHttpRequestIdentifierFeature>();
// Reset() is called once in the test ctor

var requestProcessingTask = _http1Connection.ProcessRequestsAsync(new DummyApplication());

var count = 1;
void Reset()
async Task SendRequestAsync()
{
_http1Connection.Reset();
var data = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost:\r\n\r\n");
await _application.Output.WriteAsync(data);

while (true)
{
var read = await _application.Input.ReadAsync();
SequencePosition consumed = read.Buffer.Start;
SequencePosition examined = read.Buffer.End;
try
{
if (TryReadResponse(read, out consumed, out examined))
{
break;
}
}
finally
{
_application.Input.AdvanceTo(consumed, examined);
}
}

count++;
}

var nextId = feature.TraceIdentifier;
Assert.Equal($"{connectionId}:00000001", nextId);

Reset();
await SendRequestAsync();

var secondId = feature.TraceIdentifier;
Assert.Equal($"{connectionId}:00000002", secondId);

var big = 1_000_000;
var big = 10_000;
while (big-- > 0)
{
Reset();
await SendRequestAsync();
}
Assert.Equal($"{connectionId}:{count:X8}", feature.TraceIdentifier);

_http1Connection.StopProcessingNextRequest();
await requestProcessingTask.DefaultTimeout();
}

private static bool TryReadResponse(ReadResult read, out SequencePosition consumed, out SequencePosition examined)
{
consumed = read.Buffer.Start;
examined = read.Buffer.End;

SequenceReader<byte> reader = new SequenceReader<byte>(read.Buffer);
if (reader.TryReadTo(out ReadOnlySequence<byte> _, new byte[] { (byte)'\r', (byte)'\n', (byte)'\r', (byte)'\n' }, advancePastDelimiter: true))
{
consumed = reader.Position;
examined = reader.Position;
return true;
}

return false;
}

[Fact]
Expand All @@ -216,9 +244,6 @@ public void TraceIdentifierGeneratesWhenNull()
var id = _http1Connection.TraceIdentifier;
Assert.NotNull(id);
Assert.Equal(id, _http1Connection.TraceIdentifier);

_http1Connection.Reset();
Assert.NotEqual(id, _http1Connection.TraceIdentifier);
}

[Fact]
Expand Down

0 comments on commit 7521aba

Please sign in to comment.