public async Task DeserializeFromStreamSequencingTest( string value, int bufferSize, int expectedRentCalls, string getBuffersMethodName) { MethodInfo?getBuffersMethod = typeof(Utf8JsonStreamReaderTests).GetMethod(getBuffersMethodName, BindingFlags.Static | BindingFlags.NonPublic); if (getBuffersMethod == null) { throw new InvalidOperationException($"getBuffersMethodName [{getBuffersMethodName}] could not be found reflectively."); } int rentCalls = 0; int returnCalls = 0; Utf8JsonStreamReader.RentBufferFunc = (size) => { rentCalls++; return(new byte[size]); }; Utf8JsonStreamReader.ReturnBufferAction = (buffer) => returnCalls++; using TestStream stream = new((Memory <byte>[])getBuffersMethod.Invoke(null, null) !); TestClass instance = new(); await Utf8JsonStreamReader.DeserializeAsync(stream, instance, ReadMethod, bufferSize).ConfigureAwait(false); Assert.AreEqual(1, instance.PropertyName); Assert.AreEqual(value, instance.PropertyName2); Assert.AreEqual(expectedRentCalls, rentCalls); Assert.AreEqual(expectedRentCalls, returnCalls); }
public async Task DeserializeFromStreamTest() { int rentCalls = 0; int returnCalls = 0; Utf8JsonStreamReader.RentBufferFunc = (size) => { rentCalls++; return(new byte[size]); }; Utf8JsonStreamReader.ReturnBufferAction = (buffer) => returnCalls++; using MemoryStream stream = new(Encoding.UTF8.GetBytes("{\"PropertyName\":1, \"PropertyName2\":\"value\"}")); TestClass instance = new(); await Utf8JsonStreamReader.DeserializeAsync(stream, instance, ReadMethod, bufferSize : 8192).ConfigureAwait(false); Assert.AreEqual(1, instance.PropertyName); Assert.AreEqual("value", instance.PropertyName2); Assert.AreEqual(1, rentCalls); Assert.AreEqual(1, returnCalls); }