public void TestWriteAndSignal()
 {
     using (ManualResetEvent mre = new ManualResetEvent(false))
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Write(new byte[100], 0, 100, mre);
         Assert.IsTrue(mre.WaitOne(60000, false));
         Assert.AreEqual(100, io.Position);
         Assert.AreEqual(100, io.Length);
     }
 }
 public void TestWriteAndFlush()
 {
     using (TempFile temp = new TempFile())
     using (TestStream io = new TestStream(temp.Open()))
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         const int sz = 1000;
         const int iter = 10000;
         var bytes = new byte[sz];
         for (int i = 0; i < iter; i++)
             wtr.Write(bytes, 0, sz);
         wtr.Flush();
         Assert.IsTrue(io.Flushed);
         Assert.AreEqual(sz * iter, io.Position);
         Assert.AreEqual(sz * iter, io.Length);
     }
 }
 public void TestWriteOffset()
 {
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Write(new byte[100], 0, 100);
         wtr.Write(1, new byte[] { 42 }, 0, 1);
         wtr.Flush();
         Assert.AreEqual(2, io.Position);
         Assert.AreEqual(100, io.Length);
         io.Position = 1;
         Assert.AreEqual(42, io.ReadByte());
     }
 }
 public void TestInvalidLength()
 {
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Write(new byte[10], 5, 50);
     }
 }
 public void TestInvalidOffset()
 {
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Write(new byte[10], 11, 1);
     }
 }
 public void TestNullBuffer()
 {
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Write(null, 0, 1);
     }
 }
 public void TestClosedRaisesError()
 {
     using (TestStream io = new TestStream())
     using (BackgroundWriter wtr = new BackgroundWriter(io))
     {
         wtr.Close();
         wtr.Write(new byte[100], 0, 100);
     }
 }
        public void TestWriteFileAsyncFlush()
        {
            using (ManualResetEvent mre = new ManualResetEvent(false))
            using (TempFile temp = new TempFile())
            using (TestStream io = new TestStream(temp.Open()))
            using (BackgroundWriter wtr = new BackgroundWriter(io))
            {
                const int sz = 1000;
                const int iter = 10000;
                var bytes = new byte[sz];
                for (int i = 0; i < iter; i++)
                    wtr.Write(bytes, 0, sz);
                wtr.BeginFlush();
                Assert.IsFalse(io.Flushed);

                wtr.Perform(s => mre.Set());
                Assert.IsTrue(mre.WaitOne(60000, false));

                Assert.IsTrue(io.Flushed);
                Assert.AreEqual(sz * iter, io.Position);
                Assert.AreEqual(sz * iter, io.Length);
            }
        }
        public void TestWriteAndReadOffset()
        {
            using (TestStream io = new TestStream())
            using (BackgroundWriter wtr = new BackgroundWriter(io))
            {
                wtr.Write(0L, new byte[100], 0, 100);
                wtr.Perform(s => Thread.Sleep(50));
                wtr.Write(100L, new byte[] { 99 }, 0, 1);
                wtr.Write(100L, new byte[] { 42, 43 }, 0, 2);

                // Read scans the pending writes for writes at the provided offset and returns the last result
                byte[] read = new byte[100];
                Assert.AreEqual(2, wtr.Read(100L, read, 0, 100));
                Assert.AreEqual(42, (int)read[0]);
                Assert.AreEqual(43, (int)read[1]);
            }
        }