Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,26 @@ public async Task WriteAndCopyToStreamAsync()
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After successfully setting ABW to init capacity, verify that output.GetMemory(int.max) fails.

Suggested change
Assert.Equal(MaxArrayLength - initialCapacity, memory.Length);
Assert.Equal(MaxArrayLength - initialCapacity, memory.Length);
Assert.Throws<OutOfMemoryException>(() => output.GetMemory(int.MaxValue));

Assert.Throws<OutOfMemoryException>(() => output.GetMemory(int.MaxValue));
}
catch (OutOfMemoryException)
{
// On memory constrained devices, we can get an OutOfMemoryException, which we can safely ignore.
}
}
}
}