Exemplo n.º 1
0
        public void NonSharedPart_DisposableRecomposabeImport_NoReference_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(NonSharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <NonSharedPartDisposableRecomposable>());

            refTracker.CollectAndAssert();

            // Recompose just to ensure we don't blow up, even though we don't expect anything to happen.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (NonSharedPartDisposableRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;

            Assert.AreEqual(42, exportedValue.Value, "Value shoudl ahve been recomposed.");

            GC.KeepAlive(container);
        }
Exemplo n.º 2
0
        public void SharedPart_DisposableRecomposabeImport_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(SharedPartDisposableRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <SharedPartDisposableRecomposable>());

            refTracker.CollectAndAssert();

            // Lets make sure recomposition doesn't blow anything up here.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (SharedPartDisposableRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;

            Assert.AreEqual(42, exportedValue.Value);

            container.Dispose();

            Assert.IsTrue(exportedValue.IsDisposed, "Any parts should be disposed with the container!");
        }
Exemplo n.º 3
0
        public void NonSharedPart_RecomposableImport_WithReference_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(NonSharedPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);
            batch = null;

            var exportedValue = container.GetExportedValue <NonSharedPartRecomposable>();

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(exportedValue);

            refTracker.CollectAndAssert();

            // Recompose should work because we are still holding a reference to the exported value.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            Assert.AreEqual(42, exportedValue.Value, "Value should have been recomposed");

            GC.KeepAlive(container);
        }
Exemplo n.º 4
0
        public void AnyPart_RecomposabeImport_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(AnyPartRecomposable));
            var container = new CompositionContainer(catalog);

            // Setup dependency
            CompositionBatch batch = new CompositionBatch();
            var valueKey           = batch.AddExportedValue("Value", 21);

            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <AnyPartRecomposable>());
            refTracker.CollectAndAssert();

            // Lets make sure recomposition doesn't blow anything up here.
            batch = new CompositionBatch();
            batch.RemovePart(valueKey);
            batch.AddExportedValue("Value", 42);
            container.Compose(batch);
            batch = null;

            var exportedValue = (AnyPartRecomposable)refTracker.ReferencesNotExpectedToBeCollected[0].Target;

            Assert.AreEqual(42, exportedValue.Value);

            GC.KeepAlive(container);
        }
Exemplo n.º 5
0
        public void GetReleaseExport_SharedPart_ShouldCollectOnlyRoot()
        {
            var container = GetContainer();

            var export        = container.GetExport <SharedImporter>();
            var exportedValue = export.Value;

            container.ReleaseExport(export);

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesExpectedToBeCollected(
                exportedValue);

            refTracker.AddReferencesNotExpectedToBeCollected(
                exportedValue.AnyPartDisposable,
                exportedValue.AnyPartDisposableRecomposable,
                exportedValue.AnyPartRecomposable,
                exportedValue.AnyPartSimple);

            export        = null;
            exportedValue = null;

            refTracker.CollectAndAssert();

            GC.KeepAlive(container);
        }
Exemplo n.º 6
0
        public void ChildContainerDispose_NonSharedPart_ShouldOnlyCleanupChildAndSimpleNonShared()
        {
            var child = CreateParentChildContainerWithNonSharedImporter();

            var exportedValue = child.GetExportedValue <NonSharedImporter>();

            child.Dispose();

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesExpectedToBeCollected(
                exportedValue,                // object in child
                exportedValue.AnyPartSimple,  // No reference parent so collected.
                exportedValue.AnyPartRecomposable);

            // These are in the parent and will not be cleaned out
            refTracker.AddReferencesNotExpectedToBeCollected(
                exportedValue.AnyPartDisposable,
                exportedValue.AnyPartDisposableRecomposable);

            exportedValue = null;

            refTracker.CollectAndAssert();

            GC.KeepAlive(child);
        }
Exemplo n.º 7
0
        public void NonSharedImporter_ReleaseReference_ShouldCollectWholeChain()
        {
            var container = GetContainer();

            var export        = container.GetExport <NonSharedImporter>();
            var exportedValue = export.Value;

            var refTracker = new ReferenceTracker();

            // Non-Disposable references in the chain should be GC'ed
            refTracker.AddReferencesExpectedToBeCollected(
                exportedValue,
                exportedValue.AnyPartRecomposable,
                exportedValue.AnyPartSimple);

            // Disposable references in the chain should NOT be GC'ed
            refTracker.AddReferencesNotExpectedToBeCollected(
                exportedValue.AnyPartDisposable,
                exportedValue.AnyPartDisposableRecomposable);

            export        = null;
            exportedValue = null;

            refTracker.CollectAndAssert();

            GC.KeepAlive(container);
        }
Exemplo n.º 8
0
        public void AddRemovePart_SharedPart_ShouldCollectOnlyRoot()
        {
            var container = GetContainer();

            var exportedValue = new SharedImporter();

            CompositionBatch batch = new CompositionBatch();
            var part = batch.AddPart(exportedValue);

            container.Compose(batch);
            batch = null;

            batch = new CompositionBatch();
            batch.RemovePart(part);
            container.Compose(batch);
            batch = null;

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesExpectedToBeCollected(
                exportedValue);

            refTracker.AddReferencesNotExpectedToBeCollected(
                exportedValue.AnyPartDisposable,
                exportedValue.AnyPartDisposableRecomposable,
                exportedValue.AnyPartRecomposable,
                exportedValue.AnyPartSimple);

            part          = null;
            exportedValue = null;

            refTracker.CollectAndAssert();

            GC.KeepAlive(container);
        }
Exemplo n.º 9
0
        public void AnyPart_Disposable_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(AnyPartDisposable));
            var container = new CompositionContainer(catalog);

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <AnyPartDisposable>());

            GC.KeepAlive(container);
        }
Exemplo n.º 10
0
        public void SharedPart_Simple_ShouldNotBeCollected()
        {
            var catalog   = new TypeCatalog(typeof(SharedPartSimple));
            var container = new CompositionContainer(catalog);

            var refTracker = new ReferenceTracker();

            refTracker.AddReferencesNotExpectedToBeCollected(
                container.GetExportedValue <SharedPartSimple>());

            refTracker.CollectAndAssert();

            GC.KeepAlive(container);
        }