Fix race condition in new OnStarting and OnCompleted tests.

This commit is contained in:
Chris R 2016-08-10 15:49:02 -07:00
parent fe6ecfde65
commit cd886802fe
1 changed files with 22 additions and 18 deletions

View File

@ -19,6 +19,7 @@ using System;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Http.Features;
using Xunit; using Xunit;
@ -135,21 +136,21 @@ namespace Microsoft.AspNetCore.Server.WebListener
[Fact] [Fact]
public async Task Response_Empty_CallsOnStartingAndOnCompleted() public async Task Response_Empty_CallsOnStartingAndOnCompleted()
{ {
var onStartingCalled = false; var onStartingCalled = new ManualResetEvent(false);
var onCompletedCalled = false; var onCompletedCalled = new ManualResetEvent(false);
string address; string address;
using (Utilities.CreateHttpServer(out address, httpContext => using (Utilities.CreateHttpServer(out address, httpContext =>
{ {
httpContext.Response.OnStarting(state => httpContext.Response.OnStarting(state =>
{ {
onStartingCalled = true;
Assert.Same(state, httpContext); Assert.Same(state, httpContext);
onStartingCalled.Set();
return Task.FromResult(0); return Task.FromResult(0);
}, httpContext); }, httpContext);
httpContext.Response.OnCompleted(state => httpContext.Response.OnCompleted(state =>
{ {
onCompletedCalled = true;
Assert.Same(state, httpContext); Assert.Same(state, httpContext);
onCompletedCalled.Set();
return Task.FromResult(0); return Task.FromResult(0);
}, httpContext); }, httpContext);
return Task.FromResult(0); return Task.FromResult(0);
@ -157,28 +158,29 @@ namespace Microsoft.AspNetCore.Server.WebListener
{ {
var response = await SendRequestAsync(address); var response = await SendRequestAsync(address);
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(onStartingCalled); Assert.True(onStartingCalled.WaitOne(0));
Assert.True(onCompletedCalled); // Fires after the response completes
Assert.True(onCompletedCalled.WaitOne(TimeSpan.FromSeconds(5)));
} }
} }
[Fact] [Fact]
public async Task Response_OnStartingThrows_StillCallsOnCompleted() public async Task Response_OnStartingThrows_StillCallsOnCompleted()
{ {
var onStartingCalled = false; var onStartingCalled = new ManualResetEvent(false);
var onCompletedCalled = false; var onCompletedCalled = new ManualResetEvent(false);
string address; string address;
using (Utilities.CreateHttpServer(out address, httpContext => using (Utilities.CreateHttpServer(out address, httpContext =>
{ {
httpContext.Response.OnStarting(state => httpContext.Response.OnStarting(state =>
{ {
onStartingCalled = true; onStartingCalled.Set();
throw new Exception("Failed OnStarting"); throw new Exception("Failed OnStarting");
}, httpContext); }, httpContext);
httpContext.Response.OnCompleted(state => httpContext.Response.OnCompleted(state =>
{ {
onCompletedCalled = true;
Assert.Same(state, httpContext); Assert.Same(state, httpContext);
onCompletedCalled.Set();
return Task.FromResult(0); return Task.FromResult(0);
}, httpContext); }, httpContext);
return Task.FromResult(0); return Task.FromResult(0);
@ -186,28 +188,29 @@ namespace Microsoft.AspNetCore.Server.WebListener
{ {
var response = await SendRequestAsync(address); var response = await SendRequestAsync(address);
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
Assert.True(onStartingCalled); Assert.True(onStartingCalled.WaitOne(0));
Assert.True(onCompletedCalled); // Fires after the response completes
Assert.True(onCompletedCalled.WaitOne(TimeSpan.FromSeconds(5)));
} }
} }
[Fact] [Fact]
public async Task Response_OnStartingThrowsAfterWrite_WriteThrowsAndStillCallsOnCompleted() public async Task Response_OnStartingThrowsAfterWrite_WriteThrowsAndStillCallsOnCompleted()
{ {
var onStartingCalled = false; var onStartingCalled = new ManualResetEvent(false);
var onCompletedCalled = false; var onCompletedCalled = new ManualResetEvent(false);
string address; string address;
using (Utilities.CreateHttpServer(out address, httpContext => using (Utilities.CreateHttpServer(out address, httpContext =>
{ {
httpContext.Response.OnStarting(state => httpContext.Response.OnStarting(state =>
{ {
onStartingCalled = true; onStartingCalled.Set();
throw new InvalidTimeZoneException("Failed OnStarting"); throw new InvalidTimeZoneException("Failed OnStarting");
}, httpContext); }, httpContext);
httpContext.Response.OnCompleted(state => httpContext.Response.OnCompleted(state =>
{ {
onCompletedCalled = true;
Assert.Same(state, httpContext); Assert.Same(state, httpContext);
onCompletedCalled.Set();
return Task.FromResult(0); return Task.FromResult(0);
}, httpContext); }, httpContext);
Assert.Throws<InvalidTimeZoneException>(() => httpContext.Response.Body.Write(new byte[10], 0, 10)); Assert.Throws<InvalidTimeZoneException>(() => httpContext.Response.Body.Write(new byte[10], 0, 10));
@ -216,8 +219,9 @@ namespace Microsoft.AspNetCore.Server.WebListener
{ {
var response = await SendRequestAsync(address); var response = await SendRequestAsync(address);
Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.True(onStartingCalled); Assert.True(onStartingCalled.WaitOne(0));
Assert.True(onCompletedCalled); // Fires after the response completes
Assert.True(onCompletedCalled.WaitOne(TimeSpan.FromSeconds(5)));
} }
} }