public void TestFinalizer() { string filename; try { TempFile file = new TempFile(); filename = file.TempPath; Assert.IsTrue(File.Exists(file.TempPath)); IDisposable flock = file.Open(); file.Dispose(); Assert.IsTrue(File.Exists(file.TempPath));//dua, it's still open flock.Dispose(); file = null; } finally { } //wait for GC to collect tempfile GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.IsFalse(File.Exists(filename)); }
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 TestFileStreamCache() { Stream stream; using (TempFile tempFile = new TempFile()) { using (StreamCache cache = new StreamCache(new FileStreamFactory(tempFile.TempPath, FileMode.Open))) { using (stream = cache.Open()) { stream.SetLength(100); stream.WriteByte(1); } } Assert.AreEqual(100, tempFile.Length); using (stream = tempFile.Open()) Assert.AreEqual(1, stream.ReadByte()); } }
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 TestFinalizerReschedule() { IDisposable flock; string filename; try { TempFile file = new TempFile(); filename = file.TempPath; Assert.IsTrue(File.Exists(file.TempPath)); flock = file.Open(); file.Dispose(); Assert.IsTrue(File.Exists(file.TempPath));//dua, it's still open file = null; } finally { } //wait for GC to collect tempfile GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.IsTrue(File.Exists(filename)); //now the finalizer should have fire, as proven by TestFinalizer(), see if the //rescheduled object will finalize... flock.Dispose(); GC.Collect(0, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); Assert.IsFalse(File.Exists(filename)); }
public void TestFileReadAccess() { TempFile file = new TempFile(); Stream r = file.Read(); Assert.IsFalse(r.CanWrite); Assert.IsTrue(r.CanRead); Stream o = file.Open(); Assert.IsTrue(o.CanWrite); Assert.IsTrue(o.CanRead); o.Dispose(); r.Dispose(); file.Dispose(); }
public void TestParseDocument() { XmlLightDocument doc = new HtmlLightDocument(document); XmlLightDocument doc2; using (TempFile t = new TempFile()) { using (TextWriter tw = new StreamWriter(t.Open())) doc.WriteXml(tw); new XhtmlValidation(XhtmlDTDSpecification.XhtmlTransitional_10).Validate(t.TempPath); doc2 = new XmlLightDocument(t.ReadAllText()); Assert.AreEqual(doc.InnerXml, doc2.InnerXml); } }