diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/FlushPointTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/FlushPointTest.cs index 5e84f0ffbd..53cdd2a494 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/FlushPointTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/FlushPointTest.cs @@ -1,90 +1,58 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System; -using System.IO; -using System.Text; +using System.Net.Http; using System.Threading.Tasks; -using Microsoft.AspNet.Builder; -using Microsoft.Extensions.DependencyInjection; -using RazorWebSite; using Xunit; namespace Microsoft.AspNet.Mvc.FunctionalTests { - public class FlushPointTest + public class FlushPointTest : IClassFixture> { - private const string SiteName = nameof(RazorWebSite); - private readonly Action _app = new Startup().Configure; - private readonly Action _configureServices = new Startup().ConfigureServices; + public FlushPointTest(MvcTestFixture fixture) + { + Client = fixture.Client; + } + + public HttpClient Client { get; } [Fact] public async Task FlushPointsAreExecutedForPagesWithLayouts() { - var waitService = new WaitService(); - var server = TestHelper.CreateServer(_app, SiteName, - services => - { - _configureServices(services); - services.AddInstance(waitService); - }); - var client = server.CreateClient(); + var expected = @"Page With Layout + +RenderBody content + + + Content that takes time to produce + +"; // Act - var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithLayout"); + var body = await Client.GetStringAsync("http://localhost/FlushPoint/PageWithLayout"); - // Assert - 1 - Assert.Equal(@"Page With Layout", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 2 - Assert.Equal(@"RenderBody content", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 3 - Assert.Equal(@"Content that takes time to produce", - GetTrimmedString(stream)); + // Assert + Assert.Equal(expected, body, ignoreLineEndingDifferences: true); } [Fact] public async Task FlushPointsAreExecutedForPagesWithoutLayouts() { - var waitService = new WaitService(); - var server = TestHelper.CreateServer(_app, SiteName, - services => - { - _configureServices(services); - services.AddInstance(waitService); - }); - var client = server.CreateClient(); + var expected = @"Initial content + +Secondary content + +Inside partial + +After flush inside partial
" + + @"" + + @"
"; // Act - var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithoutLayout"); + var body = await Client.GetStringAsync("http://localhost/FlushPoint/PageWithoutLayout"); - // Assert - 1 - Assert.Equal("Initial content", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 2 - Assert.Equal("Secondary content", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 3 - Assert.Equal("Inside partial", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 4 - Assert.Equal( - @"After flush inside partial
" + - @"", - GetTrimmedString(stream), - ignoreLineEndingDifferences: true); - waitService.WaitForServer(); - - // Assert - 5 - Assert.Equal( - @"
", - GetTrimmedString(stream)); + // Assert + Assert.Equal(expected, body, ignoreLineEndingDifferences: true); } [Theory] @@ -92,112 +60,44 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests [InlineData("PageWithRenderSectionAsync", "FlushAsync invoked inside RenderSectionAsync")] public async Task FlushPointsAreExecutedForPagesWithComponentsPartialsAndSections(string action, string title) { - var waitService = new WaitService(); - var server = TestHelper.CreateServer(_app, SiteName, - services => - { - _configureServices(services); - services.AddInstance(waitService); - }); - var client = server.CreateClient(); + var expected = $@"{ title } +RenderBody content - // Act - var stream = await client.GetStreamAsync("http://localhost/FlushPoint/" + action); - // Assert - 1 - Assert.Equal( - $@"{ title } -RenderBody content", - GetTrimmedString(stream), - ignoreLineEndingDifferences: true); - waitService.WaitForServer(); - - // Assert - 2 - Assert.Equal( - @"partial-content +partial-content Value from TaskReturningString -

section-content

", - GetTrimmedString(stream), - ignoreLineEndingDifferences: true); - waitService.WaitForServer(); - - // Assert - 3 - Assert.Equal( - @"component-content +

section-content

+ component-content Content that takes time to produce -More content from layout", - GetTrimmedString(stream), - ignoreLineEndingDifferences: true); +More content from layout +"; + + // Act + var body = await Client.GetStringAsync("http://localhost/FlushPoint/" + action); + + // Assert + Assert.Equal(expected, body, ignoreLineEndingDifferences: true); } [Fact] public async Task FlushPointsNestedLayout() { // Arrange - var waitService = new WaitService(); - var server = TestHelper.CreateServer(_app, SiteName, - services => - { - _configureServices(services); - services.AddInstance(waitService); - }); - var client = server.CreateClient(); + var expected = @"Inside Nested Layout +Nested Page With Layout + + + + Nested content that takes time to produce +"; // Act - var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithNestedLayout"); + var body = await Client.GetStringAsync("http://localhost/FlushPoint/PageWithNestedLayout"); - // Assert - 1 - Assert.Equal( - @"Inside Nested Layout -Nested Page With Layout", - GetTrimmedString(stream), - ignoreLineEndingDifferences: true); - waitService.WaitForServer(); - - // Assert - 2 - Assert.Equal("Nested content that takes time to produce", GetTrimmedString(stream)); - } - - [Fact] - public async Task FlushBeforeCallingLayout() - { - var waitService = new WaitService(); - var server = TestHelper.CreateServer(_app, SiteName, - services => - { - _configureServices(services); - services.AddInstance(waitService); - }); - var client = server.CreateClient(); - - var expectedMessage = "Layout page '/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml'" + - " cannot be rendered after 'FlushAsync' has been invoked."; - - // Act - var stream = await client.GetStreamAsync("http://localhost/FlushPoint/PageWithFlushBeforeLayout"); - - // Assert - 1 - Assert.Equal("Initial content", GetTrimmedString(stream)); - waitService.WaitForServer(); - - // Assert - 2 - try - { - GetTrimmedString(stream); - } - catch (Exception ex) - { - Assert.Equal(expectedMessage, ex.InnerException.Message); - } - } - - private string GetTrimmedString(Stream stream) - { - var buffer = new byte[1024]; - var count = stream.Read(buffer, 0, buffer.Length); - return Encoding.UTF8.GetString(buffer, 0, count).Trim(); + // Assert + Assert.Equal(expected, body, ignoreLineEndingDifferences: true); } } } diff --git a/test/WebSites/RazorWebSite/Services/WaitService.cs b/test/WebSites/RazorWebSite/Services/WaitService.cs deleted file mode 100644 index bfdb7808e6..0000000000 --- a/test/WebSites/RazorWebSite/Services/WaitService.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; -using System.Threading; - -namespace RazorWebSite -{ - public class WaitService - { - private static readonly TimeSpan _waitTime = TimeSpan.FromSeconds(60); - private readonly ManualResetEventSlim _serverResetEvent = new ManualResetEventSlim(); - private readonly ManualResetEventSlim _clientResetEvent = new ManualResetEventSlim(); - - public void NotifyClient() - { - _clientResetEvent.Set(); - } - - public void WaitForClient() - { - _clientResetEvent.Set(); - - if (!_serverResetEvent.Wait(_waitTime)) - { - throw new InvalidOperationException("Timeout exceeded"); - } - - _serverResetEvent.Reset(); - } - - public void WaitForServer() - { - _serverResetEvent.Set(); - - if (!_clientResetEvent.Wait(_waitTime)) - { - throw new InvalidOperationException("Timeout exceeded"); - } - - _clientResetEvent.Reset(); - } - } -} \ No newline at end of file diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml index 3473b46cdd..f7575ef3bc 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithFlushBeforeLayout.cshtml @@ -1,9 +1,5 @@ -@inject WaitService WaitService -Initial content +Initial content @await FlushAsync() -@{ - WaitService.WaitForClient(); -} @{ Layout = "/Views/Shared/_LayoutWithFlush.cshtml"; diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithLayout.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithLayout.cshtml index a0710f2a89..a50631d8e5 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithLayout.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithLayout.cshtml @@ -1,5 +1,4 @@ -@inject WaitService WaitService -@{ +@{ Layout = "/Views/Shared/_LayoutWithFlush.cshtml"; ViewBag.Title = "Page With Layout"; } @@ -8,7 +7,6 @@ RenderBody content { @{ await FlushAsync(); - WaitService.WaitForClient(); } Content that takes time to produce } \ No newline at end of file diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithNestedLayout.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithNestedLayout.cshtml index 9dd03ccac4..c4c740af43 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithNestedLayout.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithNestedLayout.cshtml @@ -1,5 +1,4 @@ -@inject WaitService WaitService -@{ +@{ Layout = "/Views/Shared/_NestedLayoutWithFlush.cshtml"; ViewBag.Title = "Nested Page With Layout"; } diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithPartialsAndViewComponents.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithPartialsAndViewComponents.cshtml index 0b979014f9..3c728f2ef0 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithPartialsAndViewComponents.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithPartialsAndViewComponents.cshtml @@ -1,5 +1,4 @@ -@inject WaitService WaitService -@inject TaskReturningService TaskReturningService +@inject TaskReturningService TaskReturningService @{ Layout = "/Views/Shared/_LayoutWithPartialAndFlush.cshtml"; @@ -12,7 +11,6 @@ RenderBody content

section-content

@{ await FlushAsync(); - WaitService.WaitForClient(); } @await Component.InvokeAsync("ComponentThatSetsTitle") Content that takes time to produce diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithSectionInvokedViaRenderSectionAsync.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithSectionInvokedViaRenderSectionAsync.cshtml index 660e934810..a386653b73 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithSectionInvokedViaRenderSectionAsync.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithSectionInvokedViaRenderSectionAsync.cshtml @@ -1,5 +1,4 @@ -@inject WaitService WaitService -@inject TaskReturningService TaskReturningService +@inject TaskReturningService TaskReturningService @{ Layout = "/Views/Shared/_LayoutWithRenderSectionAsync.cshtml"; @@ -12,7 +11,6 @@ RenderBody content

section-content

@{ await FlushAsync(); - WaitService.WaitForClient(); } @await Component.InvokeAsync("ComponentThatSetsTitle") Content that takes time to produce diff --git a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithoutLayout.cshtml b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithoutLayout.cshtml index 217ba18738..90f033739e 100644 --- a/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithoutLayout.cshtml +++ b/test/WebSites/RazorWebSite/Views/FlushPoint/PageWithoutLayout.cshtml @@ -1,14 +1,7 @@ -@inject WaitService WaitService -Initial content +Initial content @await FlushAsync() -@{ - WaitService.WaitForClient(); -} Secondary content @await FlushAsync() -@{ - WaitService.WaitForClient(); -} @{ await Html.RenderPartialAsync("_PartialWithFlush"); } @@ -16,9 +9,5 @@ Secondary content { @Html.TextBox("Name1") @await FlushAsync() - WaitService.WaitForClient(); @Html.TextBox("Name2") } -@{ - WaitService.NotifyClient(); -} \ No newline at end of file diff --git a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithFlush.cshtml b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithFlush.cshtml index ca791c8768..fb0b8e70b5 100644 --- a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithFlush.cshtml +++ b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithFlush.cshtml @@ -1,11 +1,4 @@ -@inject WaitService WaitService -@ViewBag.Title +@ViewBag.Title @await FlushAsync() -@{ - WaitService.WaitForClient(); -} @RenderBody() @await RenderSectionAsync("content") -@{ - WaitService.NotifyClient(); -} \ No newline at end of file diff --git a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithPartialAndFlush.cshtml b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithPartialAndFlush.cshtml index 5f4e3bc31a..bf9067b633 100644 --- a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithPartialAndFlush.cshtml +++ b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithPartialAndFlush.cshtml @@ -1,13 +1,6 @@ -@inject WaitService WaitService -@ViewBag.Title +@ViewBag.Title @RenderBody() @await FlushAsync() -@{ - WaitService.WaitForClient(); -} @await Html.PartialAsync("_PartialThatSetsTitle") -@await RenderSectionAsync("content") +@RenderSection("content") More content from layout -@{ - WaitService.NotifyClient(); -} diff --git a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithRenderSectionAsync.cshtml b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithRenderSectionAsync.cshtml index 8dfc88f054..f55ae1216c 100644 --- a/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithRenderSectionAsync.cshtml +++ b/test/WebSites/RazorWebSite/Views/Shared/_LayoutWithRenderSectionAsync.cshtml @@ -1,10 +1,6 @@ -@inject WaitService WaitService -@ViewBag.Title +@ViewBag.Title @RenderBody() -@{ - await FlushAsync(); - WaitService.WaitForClient(); -} +@await FlushAsync() @await Html.PartialAsync("_PartialThatSetsTitle") @await RenderSectionAsync("content") More content from layout diff --git a/test/WebSites/RazorWebSite/Views/Shared/_NestedLayoutWithFlush.cshtml b/test/WebSites/RazorWebSite/Views/Shared/_NestedLayoutWithFlush.cshtml index d127d570ac..0ff6bc49b1 100644 --- a/test/WebSites/RazorWebSite/Views/Shared/_NestedLayoutWithFlush.cshtml +++ b/test/WebSites/RazorWebSite/Views/Shared/_NestedLayoutWithFlush.cshtml @@ -1,5 +1,4 @@ -@inject WaitService WaitService -@{ +@{ Layout = "/Views/Shared/_LayoutWithRenderSectionOnly.cshtml"; ViewBag.Title = "Page With Layout"; } @@ -9,7 +8,6 @@ Inside Nested Layout { @{ await FlushAsync(); - WaitService.WaitForClient(); } Nested content that takes time to produce } diff --git a/test/WebSites/RazorWebSite/Views/Shared/_PartialWithFlush.cshtml b/test/WebSites/RazorWebSite/Views/Shared/_PartialWithFlush.cshtml index de296ced2a..4320ef921b 100644 --- a/test/WebSites/RazorWebSite/Views/Shared/_PartialWithFlush.cshtml +++ b/test/WebSites/RazorWebSite/Views/Shared/_PartialWithFlush.cshtml @@ -1,7 +1,3 @@ -@inject WaitService WaitService -Inside partial +Inside partial @await FlushAsync() -@{ - WaitService.WaitForClient(); -} After flush inside partial \ No newline at end of file