예제 #1
0
    private static void TestDispose()
    {
        StartTest("using calls Dispose");
        var disposable = new DisposableTest();

        using (disposable)
        {
        }
        EndTest(disposable.Disposed);
    }
예제 #2
0
        static void Main(string[] args)
        {
            // Using
            using (DisposableTest obj = new DisposableTest())
            {
                Console.WriteLine("Using case");
            }

            // Dispose
            DisposableTest obj2 = new DisposableTest();

            Console.WriteLine("Dispose case");
            obj2.Dispose();
            Console.Read();
        }
예제 #3
0
        public void ImportedExistingInstancesAreNotDisposedByDefault()
        {
            var disposable = new DisposableTest();

            var container = new Container();

            container.Setup(c => c.For <IDisposable>().Import.Existing(disposable));

            using (var locator = container.GetLocator())
            {
                var inst = locator.Get <IDisposable>();
                Assert.Equal(disposable, inst);
            }

            Assert.False(disposable.Disposed);
        }
예제 #4
0
    public void DisposableTestFunc()
    {
        DisposableTest disposableTest = new DisposableTest();

        //do something
        disposableTest.Dispose();


        using (DisposableTest disposableTest1 = new DisposableTest())
        {
            //do something
        }


        //FileStream : Stream : IDisposable
    }
        public void CanDispose()
        {
            var d = new DisposableTest();

            d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false);
            d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false);
            d.IsResourceAllocated.ShouldBeEquivalentTo(true);
            d.WaitDisposeManagedResources.Set();
            d.WaitDisposeUnmanagedResources.Set();

            d.Dispose();
            d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true);
            d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true);
            d.IsResourceAllocated.ShouldBeEquivalentTo(false);
            d.HasDisposedManagedResources.WaitOne(1).ShouldBeEquivalentTo(true);
            d.HasDisposedUnmanagedResources.WaitOne(1).ShouldBeEquivalentTo(true);
        }
    public void DropBefore_disposes_inputs_if_possible(int size, int dropPoint)
    {
        var disposables  = new List <DisposableTest>();
        var disposable   = new DisposableTest();
        var inputHistory = new InputHistory <DisposableTest>(disposable);

        disposables.Add(disposable);
        for (var i = 1; i < size; i++)
        {
            disposable = new DisposableTest();
            inputHistory.Append(disposable);
            disposables.Add(disposable);
        }

        inputHistory.DropBefore((uint)dropPoint);

        Assert.That(disposables.Take(dropPoint).Select(d => d.IsDisposed),
                    Is.All.EqualTo(true));
        Assert.That(disposables.Skip(dropPoint).Select(d => d.IsDisposed),
                    Is.All.EqualTo(false));
        Assert.That(inputHistory.GetFullSequence().Select(i => i.Input.IsDisposed).ToArray(),
                    Is.All.EqualTo(false));
        AssertHistoryMaintainsOrder(inputHistory);
    }
        public void CanDisposeThreadedSafely()
        {
            var d = new DisposableTest();

            d.EnteredDisposeManagedResources.ShouldBeEquivalentTo(false);
            d.EnteredDisposeUnmanagedResorces.ShouldBeEquivalentTo(false);
            Thread disposingThread = new Thread(() =>
            {
                d.Dispose();
                d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true);
                d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true);
                d.IsResourceAllocated.ShouldBeEquivalentTo(false);
            });

            disposingThread.Start();
            for (int i = 0; i < 1000; i++)
            {
                d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false);
                d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false);
                Thread tryToDispose = new Thread(() => d.Dispose());
                tryToDispose.Start();
            }
            d.IsResourceAllocated.ShouldBeEquivalentTo(false);
            d.EnteredDisposeManagedResources.ShouldBeEquivalentTo(true);
            d.EnteredDisposeUnmanagedResorces.ShouldBeEquivalentTo(false);
            d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(false);
            d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(false);
            d.WaitDisposeManagedResources.Set();
            d.WaitDisposeUnmanagedResources.Set();
            d.HasDisposedManagedResources.WaitOne(TimeSpan.FromSeconds(5));
            d.HasDisposedUnmanagedResources.WaitOne(TimeSpan.FromSeconds(5));
            d.FinishedDisposeManagedResources.ShouldBeEquivalentTo(true);
            d.FinishedDisposeUnmanagedResources.ShouldBeEquivalentTo(true);
            d.TimesDisposeManagedRan.ShouldBeEquivalentTo(1);
            d.TimesDisposeUnmanagedRan.ShouldBeEquivalentTo(1);
        }
예제 #8
0
        public void TryDisposeTest()
        {
            var t1 = new DisposableTest();

            t1.TryDispose();
            t1.TryDispose();
            Should.Throw <ObjectDisposedException>(() => t1.Dispose());

            var t2 = new CommonDisposableTest2();

            t2.TryDispose();
            t2.TryDispose();
            Should.Throw <ObjectDisposedException>(() => t2.Dispose());

            // In order to get 100% test coverage of all paths we force an exception to get generated
            // from Dispose and eaten by TryDispose because we never allow the set disposed via doing DI
            // of ISetOnceValue.
            //
            var neverSet = new NeverSetBool();
            var t2Test   = new CommonDisposableTest2(neverSet);

            Should.Throw <ObjectDisposedException>(() => t2Test.Dispose());  // Should report an exception
            t2Test.TryDispose();                                             // Test that an exception didn't get thrown!
            neverSet.Value.ShouldBe(default);