Skip to content

Commit

Permalink
Trying to prevent the weird test timing issues.
Browse files Browse the repository at this point in the history
Added xunit runner config file to prevent test parallelization.
Changed formatting, cleared the buffer and checked for the right named object, and added locking.
Subtracted 50 milliseconds from the test start time to compensate for the build lab reporting earlier timestamps for telemetry items.
  • Loading branch information
dnduffy committed Dec 9, 2016
1 parent 7e0635d commit b2815de
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
48 changes: 28 additions & 20 deletions test/FunctionalTestUtils/TelemetryTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.DataContracts;
Expand All @@ -20,38 +21,45 @@
public abstract class TelemetryTestsBase
{
protected const int TestTimeoutMs = 10000;
private object noParallelism = new object();

[MethodImpl(MethodImplOptions.NoOptimization)]
public void ValidateBasicRequest(InProcessServer server, string requestPath, RequestTelemetry expected)
{
DateTimeOffset testStart = new DateTimeOffset(Stopwatch.GetTimestamp(), TimeSpan.Zero);
var timer = Stopwatch.StartNew();
lock (noParallelism)
{
// Subtract 50 milliseconds to hack around strange behavior on build server where the RequestTelemetry.Timestamp is somehow sometimes earlier than now by a few milliseconds.
expected.Timestamp = DateTimeOffset.Now.Subtract(TimeSpan.FromMilliseconds(50));
server.BackChannel.Buffer.Clear();
Stopwatch timer = Stopwatch.StartNew();

var httpClientHandler = new HttpClientHandler();
httpClientHandler.UseDefaultCredentials = true;
var httpClientHandler = new HttpClientHandler();
httpClientHandler.UseDefaultCredentials = true;

Task<HttpResponseMessage> task;
using (var httpClient = new HttpClient(httpClientHandler, true))
{
task = httpClient.GetAsync(server.BaseHost + requestPath);
task.Wait(TestTimeoutMs);
}
Task<HttpResponseMessage> task;
using (HttpClient httpClient = new HttpClient(httpClientHandler, true))
{
task = httpClient.GetAsync(server.BaseHost + requestPath);
task.Wait(TestTimeoutMs);
}

timer.Stop();
server.Dispose();
timer.Stop();
server.Dispose();

var actual = server.BackChannel.Buffer.OfType<RequestTelemetry>().Single();
RequestTelemetry actual = server.BackChannel.Buffer.OfType<RequestTelemetry>().Where(t => t.Name == expected.Name).Single();
server.BackChannel.Buffer.Clear();

Assert.Equal(expected.ResponseCode, actual.ResponseCode);
Assert.Equal(expected.Name, actual.Name);
Assert.Equal(expected.Success, actual.Success);
Assert.Equal(expected.Url, actual.Url);
InRange(actual.Timestamp, testStart, new DateTimeOffset(Stopwatch.GetTimestamp(), TimeSpan.Zero));
Assert.True(actual.Duration < timer.Elapsed, "duration");
Assert.Equal(expected.ResponseCode, actual.ResponseCode);
Assert.Equal(expected.Name, actual.Name);
Assert.Equal(expected.Success, actual.Success);
Assert.Equal(expected.Url, actual.Url);
InRange(actual.Timestamp, expected.Timestamp, DateTimeOffset.Now);
Assert.True(actual.Duration < timer.Elapsed, "duration");
}
}

public void ValidateBasicException(InProcessServer server, string requestPath, ExceptionTelemetry expected)
{
DateTimeOffset testStart = DateTimeOffset.Now;
var httpClientHandler = new HttpClientHandler();
httpClientHandler.UseDefaultCredentials = true;

Expand Down
2 changes: 1 addition & 1 deletion test/WebApiShimFw46.FunctionalTests/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"delaySign": true,
"preserveCompilationContext": true,
"copyToOutput": {
"includeFiles": [ "config.json" ]
"includeFiles": [ "config.json", "xunit.runner.json" ]
}
},
"testRunner": "xunit",
Expand Down
4 changes: 4 additions & 0 deletions test/WebApiShimFw46.FunctionalTests/xunit.runner.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"parallelizeAssembly": false,
"parallelizeTestCollections": false
}

0 comments on commit b2815de

Please sign in to comment.