public void TestPerformActionAndSignal() { using (ManualResetEvent mre = new ManualResetEvent(false)) using (TestStream io = new TestStream()) using (BackgroundWriter wtr = new BackgroundWriter(io)) { wtr.Perform(s => s.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 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 TestPerformAction() { using (TestStream io = new TestStream()) using (BackgroundWriter wtr = new BackgroundWriter(io)) { wtr.Perform(s => s.Write(new byte[100], 0, 100)); wtr.Flush(); Assert.AreEqual(100, io.Position); Assert.AreEqual(100, 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]); } }