From 5175f6d13922748f483a42dedfdeb092d28400ce Mon Sep 17 00:00:00 2001 From: Steve Harter Date: Thu, 7 Jan 2021 17:54:01 -0600 Subject: [PATCH] Avoid OutOfMemoryException in some test environments (#45710) --- .../ArrayBufferWriterTests.Byte.cs | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs b/src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs index 8b079c8..c41ef5a 100644 --- a/src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs +++ b/src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs @@ -99,20 +99,26 @@ namespace System.Buffers.Tests public void GetMemory_ExceedMaximumBufferSize() { const int MaxArrayLength = 0X7FEFFFFF; - int initialCapacity = int.MaxValue / 2 + 1; - var output = new ArrayBufferWriter(initialCapacity); - output.Advance(initialCapacity); - - // Validate we can't double the buffer size, but can grow - Memory memory = output.GetMemory(1); - - // The buffer should grow more than the 1 byte requested otherwise performance will not be usable - // between 1GB and 2GB. The current implementation maxes out the buffer size to MaxArrayLength. - Assert.Equal(MaxArrayLength - initialCapacity, memory.Length); - - Assert.Throws(() => output.GetMemory(int.MaxValue)); + try + { + var output = new ArrayBufferWriter(initialCapacity); + output.Advance(initialCapacity); + + // Validate we can't double the buffer size, but can grow + Memory memory; + memory = output.GetMemory(1); + + // The buffer should grow more than the 1 byte requested otherwise performance will not be usable + // between 1GB and 2GB. The current implementation maxes out the buffer size to MaxArrayLength. + Assert.Equal(MaxArrayLength - initialCapacity, memory.Length); + Assert.Throws(() => output.GetMemory(int.MaxValue)); + } + catch (OutOfMemoryException) + { + // On memory constrained devices, we can get an OutOfMemoryException, which we can safely ignore. + } } } } -- 2.7.4