Avoid OutOfMemoryException in some test environments (#45710)
authorSteve Harter <steveharter@users.noreply.github.com>
Thu, 7 Jan 2021 23:54:01 +0000 (17:54 -0600)
committerGitHub <noreply@github.com>
Thu, 7 Jan 2021 23:54:01 +0000 (17:54 -0600)
src/libraries/System.Memory/tests/ArrayBufferWriter/ArrayBufferWriterTests.Byte.cs

index 8b079c8..c41ef5a 100644 (file)
@@ -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<byte>(initialCapacity);
-            output.Advance(initialCapacity);
-
-            // Validate we can't double the buffer size, but can grow
-            Memory<byte> 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<OutOfMemoryException>(() => output.GetMemory(int.MaxValue));
+            try
+            {
+                var output = new ArrayBufferWriter<byte>(initialCapacity);
+                output.Advance(initialCapacity);
+
+                // Validate we can't double the buffer size, but can grow
+                Memory<byte> 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<OutOfMemoryException>(() => output.GetMemory(int.MaxValue));
+            }
+            catch (OutOfMemoryException)
+            {
+                // On memory constrained devices, we can get an OutOfMemoryException, which we can safely ignore.
+            }
         }
     }
 }