From c362498148e527cff1e0acfa031d1a98e38b61e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20K=C3=B6plinger?= Date: Thu, 17 Feb 2022 14:20:14 +0000 Subject: [PATCH] Improve PipeWriterTests.CompleteWithLargeWriteThrows Allocate the buffer outside of the loop so we don't hit OOM issues. While looking at the test I noticed that it actually had a bug too: Nothing was asserting that we indeed throw an InvalidOperationException when the writer is completed. Adding `Assert.ThrowsAsync` revealed that the current loop iteration count was too low to hit the exception reliably within the 10ms delay so instead check for the execution time. Fixes https://github.com/dotnet/runtime/issues/64930 --- .../tests/PipeWriterTests.cs | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/libraries/System.IO.Pipelines/tests/PipeWriterTests.cs b/src/libraries/System.IO.Pipelines/tests/PipeWriterTests.cs index 8d94e720bf12c..2ac96e7292815 100644 --- a/src/libraries/System.IO.Pipelines/tests/PipeWriterTests.cs +++ b/src/libraries/System.IO.Pipelines/tests/PipeWriterTests.cs @@ -228,33 +228,34 @@ public async Task WritesUsingGetMemoryWorks() pipe.Reader.Complete(); } - [Fact] - [SkipOnPlatform(TestPlatforms.Browser, "allocates too much memory")] + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsThreadingSupported))] public async Task CompleteWithLargeWriteThrows() { + var completeDelay = TimeSpan.FromMilliseconds(10); var pipe = new Pipe(); pipe.Reader.Complete(); var task = Task.Run(async () => { - await Task.Delay(10); + await Task.Delay(completeDelay); pipe.Writer.Complete(); }); - try + // Complete while writing + await Assert.ThrowsAsync(async () => { - for (int i = 0; i < 1000; i++) + var testStartTime = DateTime.UtcNow; + var buffer = new byte[10000000]; + int i = 0; + while (true) { - var buffer = new byte[10000000]; await pipe.Writer.WriteAsync(buffer); - } - } - catch (InvalidOperationException) - { - // Complete while writing - } - await task; + // abort test if we're executing for more than 10 times the completeDelay (check every 1000th iteration) + if (i++ % 1000 == 0 && DateTime.UtcNow - testStartTime > completeDelay * 10) + break; + } + }); } [Fact]