|
3 | 3 |
|
4 | 4 | using System.Collections.Concurrent;
|
5 | 5 | using System.Collections.Generic;
|
| 6 | +using System.Diagnostics; |
6 | 7 | using System.Diagnostics.Tracing;
|
| 8 | +using System.Globalization; |
7 | 9 | using System.Linq;
|
8 | 10 | using System.Numerics;
|
| 11 | +using System.Reflection; |
9 | 12 | using System.Threading.Tasks;
|
10 | 13 | using Microsoft.DotNet.RemoteExecutor;
|
11 |
| -using Microsoft.DotNet.XUnitExtensions; |
12 | 14 | using Xunit;
|
13 | 15 |
|
14 | 16 | namespace System.Buffers.ArrayPool.Tests
|
@@ -604,5 +606,88 @@ public static IEnumerable<object[]> BytePoolInstances()
|
604 | 606 | yield return new object[] { ArrayPool<byte>.Create(1024*1024, 1) };
|
605 | 607 | yield return new object[] { ArrayPool<byte>.Shared };
|
606 | 608 | }
|
| 609 | + |
| 610 | + [ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))] |
| 611 | + [InlineData("", "", "2147483647", "8")] |
| 612 | + [InlineData("0", "0", "2147483647", "8")] |
| 613 | + [InlineData("1", "2", "1", "2")] |
| 614 | + [InlineData("2", "1", "2", "1")] |
| 615 | + [InlineData("4", "123", "4", "123")] |
| 616 | + [InlineData("1000", "123", "1000", "123")] |
| 617 | + [InlineData(" 1 ", " 2 ", "1", "2")] |
| 618 | + [InlineData( |
| 619 | + " 1 ", |
| 620 | + " " + |
| 621 | + " " + |
| 622 | + " " + |
| 623 | + " " + |
| 624 | + " " + |
| 625 | + " " + |
| 626 | + "2" + |
| 627 | + " " + |
| 628 | + " " + |
| 629 | + " " + |
| 630 | + " " + |
| 631 | + " " + |
| 632 | + " " + |
| 633 | + " " + |
| 634 | + " " + |
| 635 | + " " + |
| 636 | + " " + |
| 637 | + " " + |
| 638 | + " " + |
| 639 | + " " + |
| 640 | + " ", |
| 641 | + "2147483647", "8")] |
| 642 | + public void SharedPool_SetEnvironmentVariables_ValuesRespected( |
| 643 | + string partitionCount, string maxArraysPerPartition, string expectedPartitionCount, string expectedMaxArraysPerPartition) |
| 644 | + { |
| 645 | + // This test relies on private reflection into the shared pool implementation. |
| 646 | + // If those details change, this test will need to be updated accordingly. |
| 647 | + |
| 648 | + var psi = new ProcessStartInfo(); |
| 649 | + psi.Environment.Add("DOTNET_SYSTEM_BUFFERS_SHAREDARRAYPOOL_MAXPARTITIONCOUNT", partitionCount); |
| 650 | + psi.Environment.Add("DOTNET_SYSTEM_BUFFERS_SHAREDARRAYPOOL_MAXARRAYSPERPARTITION", maxArraysPerPartition); |
| 651 | + |
| 652 | + RemoteExecutor.Invoke((partitionCount, maxArraysPerPartition, expectedPartitionCount, expectedMaxArraysPerPartition) => |
| 653 | + { |
| 654 | + Type staticsType = typeof(ArrayPool<>).Assembly.GetType("System.Buffers.SharedArrayPoolStatics"); |
| 655 | + Assert.NotNull(staticsType); |
| 656 | + |
| 657 | + FieldInfo partitionCountField = staticsType.GetField("s_partitionCount", BindingFlags.NonPublic | BindingFlags.Static); |
| 658 | + Assert.NotNull(partitionCountField); |
| 659 | + int partitionCountValue = (int)partitionCountField.GetValue(null); |
| 660 | + if (int.Parse(expectedPartitionCount) > 0) |
| 661 | + { |
| 662 | + Assert.Equal(Math.Min(int.Parse(expectedPartitionCount), Environment.ProcessorCount), partitionCountValue); |
| 663 | + } |
| 664 | + else |
| 665 | + { |
| 666 | + Assert.Equal(Environment.ProcessorCount, partitionCountValue); |
| 667 | + } |
| 668 | + |
| 669 | + FieldInfo maxArraysPerPartitionField = staticsType.GetField("s_maxArraysPerPartition", BindingFlags.NonPublic | BindingFlags.Static); |
| 670 | + Assert.NotNull(maxArraysPerPartitionField); |
| 671 | + int maxArraysPerPartitionValue = (int)maxArraysPerPartitionField.GetValue(null); |
| 672 | + if (int.Parse(expectedMaxArraysPerPartition) > 0) |
| 673 | + { |
| 674 | + Assert.Equal(int.Parse(expectedMaxArraysPerPartition), maxArraysPerPartitionValue); |
| 675 | + } |
| 676 | + else |
| 677 | + { |
| 678 | + Assert.Equal(8, maxArraysPerPartitionValue); |
| 679 | + } |
| 680 | + |
| 681 | + // Make sure the pool is still usable |
| 682 | + for (int i = 0; i < 2; i++) |
| 683 | + { |
| 684 | + byte[] array = ArrayPool<byte>.Shared.Rent(123); |
| 685 | + Assert.NotNull(array); |
| 686 | + Assert.InRange(array.Length, 123, int.MaxValue); |
| 687 | + ArrayPool<byte>.Shared.Return(array); |
| 688 | + } |
| 689 | + |
| 690 | + }, partitionCount, maxArraysPerPartition, expectedPartitionCount, expectedMaxArraysPerPartition, new RemoteInvokeOptions() { StartInfo = psi }).Dispose(); |
| 691 | + } |
607 | 692 | }
|
608 | 693 | }
|
0 commit comments