public static IEnumerable <object[]> BytePoolInstances() { yield return(new object[] { ArrayPool <byte> .Create() }); yield return(new object[] { ArrayPool <byte> .Create(1024 *1024, 50) }); yield return(new object[] { ArrayPool <byte> .Create(1024 *1024, 1) }); yield return(new object[] { ArrayPool <byte> .Shared }); }
public static void CallingReturnOnValueTypeWithInternalReferenceTypesAndClearingSetsValueTypeToDefault() { ArrayPool<TestStruct> pool = ArrayPool<TestStruct>.Create(); TestStruct[] array = pool.Rent(2); array[0].InternalRef = "foo"; array[1].InternalRef = "bar"; pool.Return(array, clearArray: true); Assert.Equal(default(TestStruct), array[0]); Assert.Equal(default(TestStruct), array[1]); }
public static void ReturningABufferGreaterThanMaxSizeDoesNotThrow() { ArrayPool<byte> pool = ArrayPool<byte>.Create(maxArrayLength: 16, maxArraysPerBucket: 1); byte[] rented = pool.Rent(32); pool.Return(rented); ArrayPool<byte>.Shared.Return(new byte[3 * 1024 * 1024]); ArrayPool<char>.Shared.Return(new char[3 * 1024 * 1024]); ArrayPool<string>.Shared.Return(new string[3 * 1024 * 1024]); }
public static void RentingAllBuffersAndCallingRentAgainWillAllocateBufferAndReturnIt() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, maxArraysPerBucket : 1); byte[] rented1 = pool.Rent(16); byte[] rented2 = pool.Rent(16); Assert.IsNotNull(rented1); Assert.IsNotNull(rented2); }
public static void ReturningANonPooledBufferOfDifferentSizeToThePoolThrows() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, maxArraysPerBucket : 1); byte[] buffer = pool.Rent(15); Assert.Throws <ArgumentException>(() => pool.Return(new byte[1])); buffer = pool.Rent(15); Assert.AreEqual(buffer.Length, 16); }
private static void ConfigurablePool_AllocatedArraysAreCleared <T>() { ArrayPool <T> pool = ArrayPool <T> .Create(); for (int size = 1; size <= 1000; size++) { T[] arr = pool.Rent(size); for (int i = 0; i < arr.Length; i++) { Assert.Equal(default, arr[i]);
public static void TakingAllBuffersFromABucketPlusAnAllocatedOneShouldAllowReturningAllBuffers() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, maxArraysPerBucket : 1); byte[] rented = pool.Rent(16); byte[] allocated = pool.Rent(16); pool.Return(rented); pool.Return(allocated); }
private static TextOutputFormatter PrepareNewtonsoftNdjsonOutputFormatter() { return(new NewtonsoftNdjsonOutputFormatter( new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }, ArrayPool <char> .Create(), new NullLogger <NewtonsoftNdjsonOutputFormatter>() )); }
public static void CallingReturnOnReferenceTypeArrayAndClearingSetsTypesToNull() { ArrayPool<string> pool = ArrayPool<string>.Create(); string[] array = pool.Rent(2); array[0] = "foo"; array[1] = "bar"; pool.Return(array, clearArray: true); Assert.Null(array[0]); Assert.Null(array[1]); }
public static void CallingReturnOnReferenceTypeArrayDoesNotClearTheArray() { ArrayPool<string> pool = ArrayPool<string>.Create(); string[] array = pool.Rent(2); array[0] = "foo"; array[1] = "bar"; pool.Return(array, clearArray: false); Assert.NotNull(array[0]); Assert.NotNull(array[1]); }
public static void Init() { if (m_bUseBytePool) { m_BytePool = ArrayPool <byte> .Create(8 * 1024, 50); } else { m_BytePool = ArrayPool <byte> .System(); } }
public static void CanRentManySizedBuffers() { var pool = ArrayPool <byte> .Create(); for (int i = 1; i < 10000; i++) { byte[] buffer = pool.Rent(i); Assert.Equal(i <= 16 ? 16 : RoundUpToPowerOf2(i), buffer.Length); pool.Return(buffer); } }
public static void CallingReturnWithoutClearingDoesNotClearTheBuffer() { ArrayPool <byte> pool = ArrayPool <byte> .Create(); byte[] buffer = pool.Rent(4); FillArray(buffer); pool.Return(buffer, clearArray: false); CheckFilledArray(buffer, (byte b1, byte b2) => Assert.AreEqual(b1, b2)); }
public void GlobalSetup() { _strings = new List <string>(Size); _bytePool = ArrayPool <byte> .Create(100, 25); var r = new Random(Size); for (int i = 0; i < Size; i++) { _strings.Add(GetRandomString(r, 100)); }
public static void CallingReturnWithClearingDoesClearTheBuffer() { ArrayPool <byte> pool = ArrayPool <byte> .Create(); byte[] buffer = pool.Rent(4); FillArray(buffer); // Note - yes this is bad to hold on to the old instance but we need to validate the contract pool.Return(buffer, clearArray: true); CheckFilledArray(buffer, (byte b1, byte b2) => Assert.Equal(b1, default(byte))); }
public SensorDataReceiver(ILogger <SensorDataReceiver> logger, ISensorData sensorData, IOptions <SensorDataOptions> options) { _logger = logger; _sensorData = sensorData; _options = options.Value; _bufferPool = ArrayPool <byte> .Create( maxArrayLength : SensorDataConstants.SensorDataSize, maxArraysPerBucket : _options.BufferPoolSize); _stateLock = new object(); }
public static void RentingReturningThenRentingABufferShouldNotAllocate() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, numberOfArrays : 1); byte[] bt = pool.Rent(16); int id = bt.GetHashCode(); pool.Return(bt); bt = pool.Rent(16); Assert.Equal(id, bt.GetHashCode()); }
void OnDeserialized(System.Runtime.Serialization.StreamingContext context) { if (interalLockFactory == null) { SetLockFactory(new SingleInstanceLockFactory()); } if (ByteBlockPool == null) { ByteBlockPool = ArrayPool <byte> .Create(); } }
public static void RentingSpecificLengthsYieldsExpectedLengths(int requestedMinimum, int expectedLength) { foreach (ArrayPool <byte> pool in new[] { ArrayPool <byte> .Create(), ArrayPool <byte> .Shared }) { byte[] buffer1 = pool.Rent(requestedMinimum); byte[] buffer2 = pool.Rent(requestedMinimum); Assert.NotNull(buffer1); Assert.Equal(expectedLength, buffer1.Length); Assert.NotNull(buffer2); Assert.Equal(expectedLength, buffer2.Length); Assert.NotSame(buffer1, buffer2); pool.Return(buffer2); pool.Return(buffer1); } foreach (ArrayPool <char> pool in new[] { ArrayPool <char> .Create(), ArrayPool <char> .Shared }) { char[] buffer1 = pool.Rent(requestedMinimum); char[] buffer2 = pool.Rent(requestedMinimum); Assert.NotNull(buffer1); Assert.Equal(expectedLength, buffer1.Length); Assert.NotNull(buffer2); Assert.Equal(expectedLength, buffer2.Length); Assert.NotSame(buffer1, buffer2); pool.Return(buffer2); pool.Return(buffer1); } foreach (ArrayPool <string> pool in new[] { ArrayPool <string> .Create(), ArrayPool <string> .Shared }) { string[] buffer1 = pool.Rent(requestedMinimum); string[] buffer2 = pool.Rent(requestedMinimum); Assert.NotNull(buffer1); Assert.Equal(expectedLength, buffer1.Length); Assert.NotNull(buffer2); Assert.Equal(expectedLength, buffer2.Length); Assert.NotSame(buffer1, buffer2); pool.Return(buffer2); pool.Return(buffer1); } }
public ArrowStreamWriter(Stream baseStream, Schema schema) { BaseStream = baseStream ?? throw new ArgumentNullException(nameof(baseStream)); Schema = schema ?? throw new ArgumentNullException(nameof(schema)); Buffers = ArrayPool <byte> .Create(); Builder = new FlatBufferBuilder(1024); HasWrittenSchema = false; _fieldTypeBuilder = new ArrowTypeFlatbufferBuilder(Builder); }
public Client(string ipAddress) { var ip = IPAddress.Parse(ipAddress); _controlEndPoint = new IPEndPoint(ip, 32100); _dataEndPoint = new IPEndPoint(ip, 32101); _control = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _data = new Socket(ip.AddressFamily, SocketType.Dgram, ProtocolType.Udp); _arrayPool = ArrayPool <ushort> .Create(); }
public JsonHalOutputFormatter(JsonSerializerSettings serializerSettings, IEnumerable <string> halJsonMediaTypes = null) { if (halJsonMediaTypes == null) { halJsonMediaTypes = new string[] { HalJsonType } } ; this.jsonFormatter = new JsonOutputFormatter(serializerSettings, ArrayPool <Char> .Create()); this.halJsonMediaTypes = halJsonMediaTypes; }
public static void RentingAfterPoolExhaustionReturnsSizeForCorrespondingBucket_JustBelowLimit() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 64, maxArraysPerBucket : 2); Assert.Equal(32, pool.Rent(31).Length); // try initial bucket Assert.Equal(32, pool.Rent(31).Length); Assert.Equal(64, pool.Rent(31).Length); // try one more level Assert.Equal(64, pool.Rent(31).Length); Assert.Equal(32, pool.Rent(31).Length); // fall back to original size }
public static void RentBufferFiresRentedDiagnosticEvent() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, maxArraysPerBucket : 1); AutoResetEvent are = new AutoResetEvent(false); ActionFiresSpecificEvent(() => { byte[] bt = pool.Rent(16); Assert.True(are.WaitOne(MaxEventWaitTimeoutInMs)); }, EventLevel.Verbose, 1, are); }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddSingleton <IHttpContextAccessor, HttpContextAccessor>(); #region service registration services.AddTransient <IMembersService, MembersService>(); services.AddTransient <ILoginService, LoginService>(); services.AddTransient <ITaskService, TaskService>(); services.AddTransient <ITaskGroupService, TaskGroupService>(); services.AddTransient <IProjectService, ProjectService>(); services.AddTransient <IReportService, ReportService>(); #endregion service registration services.AddDbContext <DataContext>(options => options.UseSqlServer(Configuration.GetConnectionString("TAIDatabase"))); services.AddSession(); // Add framework services. services.AddMvc(options => { options.Filters.Add(new StatusCodeExceptionAttribute()); options.Filters.Add(new WebServiceExceptionAttribute()); options.OutputFormatters.Clear(); var formatterSettings = JsonSerializerSettingsProvider.CreateSerializerSettings(); formatterSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; var formatter = new JsonOutputFormatter(formatterSettings, ArrayPool <Char> .Create()); options.OutputFormatters.Add(formatter); }); services.AddCors(options => { options.AddPolicy("AllowAllOrigins", builder => { builder.AllowAnyOrigin(); builder.AllowAnyMethod(); builder.AllowAnyHeader(); }); }); services.Configure <RequestLocalizationOptions>(options => { var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("pl-PL") }; options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); }
public static void RentingBufferOverConfiguredMaximumSizeFiresDiagnosticEvent() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, numberOfArrays : 1); AutoResetEvent are = new AutoResetEvent(false); ActionFiresSpecificEvent(() => { byte[] bt = pool.Rent(64); Assert.True(are.WaitOne(MaxEventWaitTimeoutInMs)); }, 2, are); }
public static void FirstCallToRentBufferFiresCreatedDiagnosticEvent() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, numberOfArrays : 1); AutoResetEvent are = new AutoResetEvent(false); ActionFiresSpecificEvent(() => { byte[] bt = pool.Rent(16); Assert.True(are.WaitOne(MaxEventWaitTimeoutInMs)); }, 2, are); }
public static void ExhaustingBufferBucketFiresDiagnosticEvent() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, numberOfArrays : 1); AutoResetEvent are = new AutoResetEvent(false); ActionFiresSpecificEvent(() => { byte[] bt = pool.Rent(16); byte[] bt2 = pool.Rent(16); Assert.True(are.WaitOne(MaxEventWaitTimeoutInMs)); }, 4, are); }
private static INdjsonWriterFactory PrepareNewtonsoftNdjsonWriterFactory() { Mock <IHttpResponseStreamWriterFactory> httpResponseStreamWriterFactory = new (); httpResponseStreamWriterFactory.Setup(m => m.CreateWriter(It.IsAny <Stream>(), It.IsIn(Encoding.UTF8))) .Returns((Stream stream, Encoding encoding) => new StreamWriter(stream)); return(new NewtonsoftNdjsonWriterFactory( httpResponseStreamWriterFactory.Object, Options.Create(new MvcNewtonsoftJsonOptions()), ArrayPool <char> .Create() )); }
public static void ReturnBufferFiresDiagnosticEvent() { ArrayPool <byte> pool = ArrayPool <byte> .Create(maxArrayLength : 16, maxArraysPerBucket : 1); byte[] buffer = pool.Rent(16); Assert.Equal(1, RunWithListener(() => pool.Return(buffer), EventLevel.Verbose, e => { Assert.Equal(3, e.EventId); Assert.Equal(buffer.GetHashCode(), e.Payload[0]); Assert.Equal(buffer.Length, e.Payload[1]); Assert.Equal(pool.GetHashCode(), e.Payload[2]); })); }