示例#1
0
        public void DuplicateStreamIsCollected()
        {
            VerifySupportsExceptionsInDelegates();

            var handle = DoWork();

            CollectGarbage();

            Assert.False(SKObject.GetInstance <SKManagedStream>(handle, out _));

            IntPtr DoWork()
            {
                var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
                var stream = new SKManagedStream(dotnet, true);

                Assert.Equal(1, stream.ReadByte());
                Assert.Equal(2, stream.ReadByte());

                var dupe1 = stream.Duplicate();

                Assert.Equal(1, dupe1.ReadByte());
                Assert.Equal(2, dupe1.ReadByte());

                Assert.Throws <InvalidOperationException>(() => stream.Duplicate());

                stream.Dispose();

                var dupe2 = dupe1.Duplicate();

                Assert.Equal(1, dupe2.ReadByte());
                Assert.Equal(2, dupe2.ReadByte());

                return(dupe2.Handle);
            }
        }
示例#2
0
        public void FullOwnershipIsTransferredToTheChildIfTheParentIsDisposed()
        {
            VerifySupportsExceptionsInDelegates();

            var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
            var stream = new SKManagedStream(dotnet, true);

            Assert.Equal(1, stream.ReadByte());
            Assert.Equal(2, stream.ReadByte());

            var dupe1 = stream.Duplicate();

            Assert.Equal(1, dupe1.ReadByte());
            Assert.Equal(2, dupe1.ReadByte());

            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());

            stream.Dispose();

            var dupe2 = dupe1.Duplicate();

            Assert.Equal(1, dupe2.ReadByte());
            Assert.Equal(2, dupe2.ReadByte());

            Assert.Throws <InvalidOperationException>(() => dupe1.Duplicate());

            dupe1.Dispose();
            dupe2.Dispose();

            Assert.Throws <ObjectDisposedException>(() => dotnet.Position);
        }
示例#3
0
        public void StreamCannotBeDuplicatedMultipleTimes()
        {
            VerifySupportsExceptionsInDelegates();

            var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
            var stream = new SKManagedStream(dotnet, true);

            Assert.Equal(1, stream.ReadByte());
            Assert.Equal(2, stream.ReadByte());

            var dupe = stream.Duplicate();

            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());

            Assert.Equal(1, dupe.ReadByte());
        }
示例#4
0
        public void MiddleDuplicateCanBeRemoved()
        {
            VerifySupportsExceptionsInDelegates();

            var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
            var stream = new SKManagedStream(dotnet, true);

            Assert.Equal(1, stream.ReadByte());
            Assert.Equal(2, stream.ReadByte());

            var dupe1 = stream.Duplicate();

            Assert.Equal(1, dupe1.ReadByte());
            Assert.Equal(2, dupe1.ReadByte());
            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());

            var dupe2 = dupe1.Duplicate();

            Assert.Equal(1, dupe2.ReadByte());
            Assert.Equal(2, dupe2.ReadByte());
            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());
            Assert.Throws <InvalidOperationException>(() => dupe1.Duplicate());

            dupe1.Dispose();
            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());

            dupe2.Dispose();
            var dupe3 = stream.Duplicate();

            Assert.Equal(1, dupe3.ReadByte());
            Assert.Equal(2, dupe3.ReadByte());
            Assert.Throws <InvalidOperationException>(() => stream.Duplicate());
        }
示例#5
0
        public void DotNetStreamIsNotClosedPrematurely()
        {
            var dotnet = CreateTestStream();
            var stream = new SKManagedStream(dotnet, true);

            Assert.Equal(0, stream.Position);

            var dupe = stream.Duplicate();

            Assert.Equal(0, dupe.Position);

            stream.Dispose();
            Assert.Equal(0, dupe.Position);

            dupe.Dispose();
            Assert.Throws <ObjectDisposedException>(() => dotnet.Position);
        }
示例#6
0
        public void StreamCanBeDuplicatedButTheOriginalCannotBeRead()
        {
            VerifySupportsExceptionsInDelegates();

            var dotnet = new MemoryStream(new byte[] { 1, 2, 3, 4, 5 });
            var stream = new SKManagedStream(dotnet, true);

            Assert.Equal(1, stream.ReadByte());
            Assert.Equal(2, stream.ReadByte());
            Assert.Equal(3, stream.ReadByte());

            var dupe = stream.Duplicate();

            Assert.NotSame(stream, dupe);
            Assert.IsType <SKManagedStream>(dupe);
            Assert.Equal(1, dupe.ReadByte());
            Assert.Equal(2, dupe.ReadByte());
            Assert.Equal(3, dupe.ReadByte());
            Assert.Throws <InvalidOperationException>(() => stream.ReadByte());
        }