예제 #1
0
        public void TestCombinedStream()
        {
            DisposableFlag f1 = new DisposableFlag();
            DisposableFlag f2 = new DisposableFlag();
            Assert.IsFalse(f1.Disposed || f2.Disposed);

            Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1);
            Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2);
            Assert.AreEqual(ms1.Length, ms2.Length);

            int size = (int)ms1.Length;
            byte[] bytes = new byte[size * 2];

            using (Stream cs = new CombinedStream(ms1, ms2))
            {
                Assert.IsTrue(cs.CanRead);

                Assert.IsFalse(f1.Disposed);
                Assert.AreEqual(size, cs.Read(bytes, 0, size));
                Assert.IsFalse(f1.Disposed); //still not disposed util read of 0 bytes
                Assert.AreEqual(1, cs.Read(bytes, size, 1));//read 1 more byte
                Assert.IsTrue(f1.Disposed);
                //now finish the second one...
                Assert.IsFalse(f2.Disposed);
                Assert.AreEqual(size - 1, cs.Read(bytes, size + 1, size - 1));
                Assert.IsFalse(f2.Disposed);//still not done
                Assert.AreEqual(-1, cs.ReadByte());
                Assert.IsTrue(f2.Disposed);
            }

            Assert.AreEqual("HelloThere", Encoding.ASCII.GetString(bytes));

            //both were disposed
            Assert.IsTrue(f1.Disposed && f2.Disposed);
        }
예제 #2
0
        public void TestDisposingStream()
        {
            DisposableFlag f = new DisposableFlag();
            Assert.IsFalse(f.Disposed);

            using (Stream s = new DisposingStream(Stream.Null).WithDisposeOf(f))
                s.Close();

            Assert.IsTrue(f.Disposed);
        }
예제 #3
0
        public void TestCombinedStreamDisposee()
        {
            DisposableFlag f1 = new DisposableFlag();
            DisposableFlag f2 = new DisposableFlag();
            Assert.IsFalse(f1.Disposed || f2.Disposed);

            Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1);
            Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2);

            using (Stream cs = new CombinedStream(ms1, ms2))
            { }

            Assert.IsTrue(f1.Disposed && f2.Disposed);//even though not read, we did dispose?
        }