public void Test_ArrayPoolBufferWriterOfT_AllocateAndGetMemoryAndSpan() { var writer = new ArrayPoolBufferWriter <byte>(); Assert.AreEqual(writer.Capacity, 256); Assert.AreEqual(writer.FreeCapacity, 256); Assert.AreEqual(writer.WrittenCount, 0); Assert.IsTrue(writer.WrittenMemory.IsEmpty); Assert.IsTrue(writer.WrittenSpan.IsEmpty); Span <byte> span = writer.GetSpan(43); Assert.IsTrue(span.Length >= 43); writer.Advance(43); Assert.AreEqual(writer.Capacity, 256); Assert.AreEqual(writer.FreeCapacity, 256 - 43); Assert.AreEqual(writer.WrittenCount, 43); Assert.AreEqual(writer.WrittenMemory.Length, 43); Assert.AreEqual(writer.WrittenSpan.Length, 43); Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.Advance(-1)); Assert.ThrowsException <ArgumentOutOfRangeException>(() => writer.GetMemory(-1)); Assert.ThrowsException <ArgumentException>(() => writer.Advance(1024)); writer.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenMemory); Assert.ThrowsException <ObjectDisposedException>(() => writer.WrittenSpan.Length); Assert.ThrowsException <ObjectDisposedException>(() => writer.Capacity); Assert.ThrowsException <ObjectDisposedException>(() => writer.FreeCapacity); Assert.ThrowsException <ObjectDisposedException>(() => writer.Clear()); Assert.ThrowsException <ObjectDisposedException>(() => writer.Advance(1)); }
public void Test_ArrayPoolBufferWriterOfT_AsStream() { var writer = new ArrayPoolBufferWriter <byte>(); Span <byte> data = Guid.NewGuid().ToByteArray(); data.CopyTo(writer.GetSpan(data.Length)); writer.Advance(data.Length); Assert.AreEqual(writer.WrittenCount, data.Length); Stream stream = writer.AsStream(); Assert.AreEqual(stream.Length, data.Length); byte[] result = new byte[16]; stream.Read(result, 0, result.Length); Assert.IsTrue(data.SequenceEqual(result)); stream.Dispose(); Assert.ThrowsException <ObjectDisposedException>(() => writer.Capacity); }
private void WriteHeader(ArrayPoolBufferWriter <byte> buf, int length) { const int maxBufSizeVarint32 = 5; Span <byte> span = buf.GetSpan(maxBufSizeVarint32 + 1); span[0] = ProtobufConsts.protoRepeatedTag1; int totWritten = LocalWriteVarint32((uint)length, 1, span) + 1; buf.Advance(totWritten); }
private async Task BufferFromStreamAsync(Stream stream, ArrayPoolBufferWriter <byte> buf, int length, CancellationToken token) { WriteHeader(buf, length); Memory <byte> mem = buf.GetMemory(length).Slice(0, length); int totRead = await stream.ReadAsync(mem, token).ConfigureAwait(false); token.ThrowIfCancellationRequested(); if (totRead != length) { throw new StreamSerializationException($"Unexpected length read while deserializing body. Expected {length}, got {totRead}"); } buf.Advance(length); }
public async Task SerializeAsync(T t, CancellationToken token = default) { Interlocked.Increment(ref ParallelGatekeeperSingleton.wrapperDepth); try { currentBatch.GetSpan(1)[0] = t; currentBatch.Advance(1); await CompleteBatch(false, token).ConfigureAwait(false); } finally { Interlocked.Decrement(ref ParallelGatekeeperSingleton.wrapperDepth); } }
public void Test_ArrayPoolBufferWriterOfT_Clear() { using var writer = new ArrayPoolBufferWriter <byte>(); Span <byte> span = writer.GetSpan(4).Slice(0, 4); byte[] data = { 1, 2, 3, 4 }; data.CopyTo(span); writer.Advance(4); Assert.AreEqual(writer.WrittenCount, 4); Assert.IsTrue(span.SequenceEqual(data)); writer.Clear(); Assert.AreEqual(writer.WrittenCount, 0); Assert.IsTrue(span.ToArray().All(b => b == 0)); }
public void Test_ArrayPoolBufferWriterOfT_AsStream() { const int GuidSize = 16; var writer = new ArrayPoolBufferWriter <byte>(); var guid = Guid.NewGuid(); // Here we first get a stream with the extension targeting ArrayPoolBufferWriter<T>. // This will wrap it into a custom internal stream type and produce a write-only // stream that essentially mirrors the IBufferWriter<T> functionality as a stream. using (Stream writeStream = writer.AsStream()) { writeStream.Write(guid); } Assert.AreEqual(writer.WrittenCount, GuidSize); // Here we get a readable stream instead, and read from it to ensure // the previous data was written correctly from the writeable stream. using (Stream stream = writer.WrittenMemory.AsStream()) { Assert.AreEqual(stream.Length, GuidSize); byte[] result = new byte[GuidSize]; stream.Read(result, 0, result.Length); // Read the guid data and ensure it matches our initial guid Assert.IsTrue(new Guid(result).Equals(guid)); } // Do a dummy write just to ensure the writer isn't disposed here. // This is because we got a stream from a memory, not a memory owner. writer.Write((byte)42); writer.Advance(1); writer.Dispose(); // Now check that the writer is actually disposed instead Assert.ThrowsException <ObjectDisposedException>(() => writer.Capacity); }