Пример #1
0
        public async ValueTask DisposerAsyncDisposesContainedInstances_InReverseOfOrderAdded()
        {
            var disposeOrder = new List <object>();

            var asyncInstance1 = new AsyncDisposeTracker();

            asyncInstance1.Disposing += (s, e) => disposeOrder.Add(asyncInstance1);
            var asyncOnlyInstance2 = new AsyncOnlyDisposeTracker();

            asyncOnlyInstance2.Disposing += (s, e) => disposeOrder.Add(asyncOnlyInstance2);
            var syncInstance3 = new DisposeTracker();

            syncInstance3.Disposing += (s, e) => disposeOrder.Add(syncInstance3);
            var syncInstance4 = new DisposeTracker();

            syncInstance4.Disposing += (s, e) => disposeOrder.Add(syncInstance4);

            var disposer = new Disposer();

            disposer.AddInstanceForDisposal(asyncInstance1);
            disposer.AddInstanceForDisposal(syncInstance3);
            disposer.AddInstanceForDisposal(syncInstance4);
            disposer.AddInstanceForAsyncDisposal(asyncOnlyInstance2);

            await disposer.DisposeAsync();

            Assert.Collection(
                disposeOrder,
                o1 => Assert.Same(asyncOnlyInstance2, o1),
                o2 => Assert.Same(syncInstance4, o2),
                o3 => Assert.Same(syncInstance3, o3),
                o4 => Assert.Same(asyncInstance1, o4));
        }
Пример #2
0
        public async Task DisposingOwnedAsynchronously_CallsDisposeAsyncOnLifetimeTokenIfAsyncDisposableDeclared()
        {
            var lifetime = new AsyncDisposeTracker();
            var owned    = new Owned <string>("unused", lifetime);
            await owned.DisposeAsync();

            Assert.True(lifetime.IsAsyncDisposed);
            Assert.False(lifetime.IsSyncDisposed);
        }
Пример #3
0
        public void DisposerDisposesOfObjectsSyncIfIAsyncDisposableDeclaredButSyncDisposeCalled()
        {
            var instance = new AsyncDisposeTracker();

            var disposer = new Disposer();

            disposer.AddInstanceForDisposal(instance);
            Assert.False(instance.IsSyncDisposed);
            Assert.False(instance.IsAsyncDisposed);
            disposer.Dispose();
            Assert.True(instance.IsSyncDisposed);
            Assert.False(instance.IsAsyncDisposed);
        }
Пример #4
0
        public async ValueTask CannotAddObjectsToDisposerAfterAsyncDispose()
        {
            var instance = new AsyncDisposeTracker();

            var disposer = new Disposer();

            disposer.AddInstanceForDisposal(instance);
            Assert.False(instance.IsSyncDisposed);
            Assert.False(instance.IsAsyncDisposed);
            await disposer.DisposeAsync();

            Assert.False(instance.IsSyncDisposed);
            Assert.True(instance.IsAsyncDisposed);

            Assert.Throws <ObjectDisposedException>(() =>
            {
                disposer.AddInstanceForDisposal(instance);
            });
        }
Пример #5
0
        public async ValueTask DisposerDisposesOfObjectsAsyncIfIAsyncDisposableDeclared()
        {
            var instance = new AsyncDisposeTracker();

            var disposer = new Disposer();

            disposer.AddInstanceForDisposal(instance);
            Assert.False(instance.IsSyncDisposed);
            Assert.False(instance.IsAsyncDisposed);
            var result = disposer.DisposeAsync();

            Assert.False(instance.IsSyncDisposed);

            // Dispose is happening async, so this won't be true yet.
            Assert.False(instance.IsAsyncDisposed);

            // Now we wait.
            await result;

            Assert.False(instance.IsSyncDisposed);
            Assert.True(instance.IsAsyncDisposed);
        }