public static async Task WriteDuringReadAsync()
 {
     using var writer = new FileBufferingWriter();
     writer.Write(new byte[] { 1, 2, 3 });
     using var manager = writer.GetWrittenContent();
     Equal(new byte[] { 1, 2, 3 }, manager.Memory.ToArray());
     Throws <InvalidOperationException>(writer.Clear);
     Throws <InvalidOperationException>(() => writer.WriteByte(2));
     Throws <InvalidOperationException>(() => writer.GetWrittenContent());
     await ThrowsAsync <InvalidOperationException>(() => writer.WriteAsync(new byte[2], 0, 2));
     await ThrowsAsync <InvalidOperationException>(writer.GetWrittenContentAsync().AsTask);
 }
        public static void ReadWriteApm(int threshold, bool asyncIO)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: asyncIO);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            var ar = writer.BeginWrite(bytes, 0, byte.MaxValue, null, "state1");

            Equal("state1", ar.AsyncState);
            True(ar.AsyncWaitHandle.WaitOne(DefaultTimeout));
            writer.EndWrite(ar);

            ar = writer.BeginWrite(bytes, byte.MaxValue, bytes.Length - byte.MaxValue, null, "state2");
            Equal("state2", ar.AsyncState);
            True(ar.AsyncWaitHandle.WaitOne(DefaultTimeout));
            writer.EndWrite(ar);

            Equal(bytes.Length, writer.Length);
            using var manager = writer.GetWrittenContent();
            Equal(bytes, manager.Memory.ToArray());
        }
        public static void StressTest3()
        {
            var buffer = new byte[1024 * 1024 * 10];    // 10 MB

            new Random().NextBytes(buffer);
            using var writer = new FileBufferingWriter(asyncIO: false);
            writer.Write(buffer);
            False(writer.TryGetWrittenContent(out _));
            using var content = writer.GetWrittenContent();
            True(buffer.AsSpan().SequenceEqual(content.Memory.Span));
        }
        public static void ReuseAfterCleanup(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using (var manager = writer.GetWrittenContent())
                Equal(bytes, manager.Memory.ToArray());

            writer.Clear();
            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using (var manager = writer.GetWrittenContent())
                Equal(bytes, manager.Memory.ToArray());
        }
        public static void StressTest(int threshold)
        {
            var dict = new Dictionary <string, string>
            {
                { "Key1", "Value1" },
                { "Key2", "Value2" }
            };
            var formatter = new BinaryFormatter();

            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            formatter.Serialize(writer, dict);
            using var manager = writer.GetWrittenContent();
            using var source  = StreamSource.AsStream(manager.Memory);
            Equal(dict, formatter.Deserialize(source));
        }
        public static void CompatWithReadOnlySequence(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            writer.Write(bytes, 0, 450);
            writer.Write(bytes.AsSpan(450));
            Equal(bytes.Length, writer.Length);
            using var source = writer.GetWrittenContent(10);
            Equal(bytes, source.Sequence.ToArray());
        }
        public static void ReadWriteWithInitialCapacity(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, initialCapacity: 5, asyncIO: false);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            writer.Write(bytes, 0, byte.MaxValue);
            writer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using var manager = writer.GetWrittenContent();
            Equal(bytes, manager.Memory.ToArray());
            if (writer.TryGetWrittenContent(out var content))
            {
                Equal(bytes, content.ToArray());
            }
        }
        public static void BufferedReadWrite(int threshold)
        {
            using var writer = new FileBufferingWriter(memoryThreshold: threshold, asyncIO: false);
            var bytes = new byte[500];

            for (byte i = 0; i < byte.MaxValue; i++)
            {
                bytes[i] = i;
            }

            IBufferWriter <byte> buffer = writer;

            buffer.Write(new ReadOnlySpan <byte>(bytes, 0, byte.MaxValue));
            buffer.Write(bytes.AsSpan(byte.MaxValue));
            Equal(bytes.Length, writer.Length);
            using var manager = writer.GetWrittenContent();
            Equal(bytes, manager.Memory.ToArray());
            if (writer.TryGetWrittenContent(out var content))
            {
                Equal(bytes, content.ToArray());
            }
        }