Ejemplo n.º 1
0
        public void DoubleDisposeTest()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService);

            disposalScope.Dispose();

            Assert.True(eventFired);

            eventFired = false;

            disposalScope.Dispose();

            Assert.False(eventFired);
        }
Ejemplo n.º 2
0
        public void DoubleDisposeTest()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService);

            disposalScope.Dispose();

            Assert.True(eventFired);

            eventFired = false;

            disposalScope.Dispose();

            Assert.False(eventFired);
        }
Ejemplo n.º 3
0
        public void DisposalScope_ThrowExceptionDuringCleanUp()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService, disposed => { throw new Exception(); });

            Assert.ThrowsAny <AggregateException>(() => disposalScope.Dispose());
        }
Ejemplo n.º 4
0
        public void DisposableScope_WithNullCleanup_Threading_Test()
        {
            DisposableItem.CreateCount  = 0;
            DisposableItem.DisposeCount = 0;

            var scope = new DisposalScope();

            var syncEvent = new ManualResetEvent(false);

            var task1 = Task.Run(() => AddToScopeWithCleanup(scope, syncEvent, null));
            var task2 = Task.Run(() => AddToScopeWithCleanup(scope, syncEvent, null));

            syncEvent.Set();

            Task.WaitAll(task1, task2);

            scope.Dispose();

            Assert.Equal(DisposableItem.CreateCount, DisposableItem.DisposeCount);
        }
Ejemplo n.º 5
0
        public void CleanUpDelegateTest()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;
            bool cleanUpCalled = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService,
                disposed =>
                {
                    Assert.True(ReferenceEquals(disposableService, disposed));

                    cleanUpCalled = true;
                });

            disposalScope.Dispose();

            Assert.True(eventFired);
            Assert.True(cleanUpCalled);
        }
Ejemplo n.º 6
0
        public void CleanUpDelegateTest()
        {
            var disposalScope     = new DisposalScope();
            var disposableService = new DisposableService();
            var eventFired        = false;
            var cleanUpCalled     = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService,
                                        disposed =>
            {
                Assert.True(ReferenceEquals(disposableService, disposed));

                cleanUpCalled = true;
            });

            disposalScope.Dispose();

            Assert.True(eventFired);
            Assert.True(cleanUpCalled);
        }
Ejemplo n.º 7
0
        public void ThrowExceptionDuringCleanUp()
        {
            DisposalScope disposalScope = new DisposalScope();
            DisposableService disposableService = new DisposableService();
            bool eventFired = false;

            disposableService.Disposing += (sender, args) => eventFired = true;

            disposalScope.AddDisposable(disposableService, disposed => { throw new Exception(); });

            disposalScope.Dispose();

            Assert.True(eventFired);
        }
        /// <summary>
        /// Locate object
        /// </summary>
        /// <param name="creationDelegate"></param>
        /// <param name="injectionScope"></param>
        /// <param name="injectionContext"></param>
        /// <param name="exportStrategy"></param>
        /// <returns></returns>
        public object Locate(ExportActivationDelegate creationDelegate,
			IInjectionScope injectionScope,
			IInjectionContext injectionContext,
			IExportStrategy exportStrategy)
        {
            object scopeName = valueDelegate(injectionScope, injectionContext);
            Tuple<object, IDisposalScope> trackedObject;

            if (!scopedObjects.TryGetValue(scopeName, out trackedObject))
            {
                IDisposalScope disposalScope = injectionContext.DisposalScope;
                IInjectionScope requestingScope = injectionContext.RequestingScope;

                IDisposalScope newDisposalScope = new DisposalScope();

                injectionContext.DisposalScope = newDisposalScope;
                injectionContext.RequestingScope = exportStrategy.OwningScope;

                object locatedObject = creationDelegate(injectionScope, injectionContext);

                injectionContext.DisposalScope = disposalScope;
                injectionContext.RequestingScope = requestingScope;

                if (locatedObject != null)
                {
                    trackedObject = new Tuple<object, IDisposalScope>(locatedObject, newDisposalScope);

                    scopedObjects[scopeName] = trackedObject;

                    INotifyWhenDisposed notifyWhenDisposed = scopeName as INotifyWhenDisposed;

                    if (notifyWhenDisposed != null)
                    {
                        notifyWhenDisposed.Disposed += (sender, args) =>
                                                       {
                                                           scopedObjects.Remove(scopeName);
                                                                     newDisposalScope.Dispose();
                                                       };
                    }

                    return locatedObject;
                }
            }
            else
            {
                return trackedObject.Item1;
            }

            return null;
        }