Exemple #1
0
        public void Should_release_value_when_there_are_no_more_references()
        {
            var table = new ConditionalWeakTable <TypeWithStrongReferenceThroughTable, TypeWithWeakReference>();

            var strong = new TypeWithStrongReferenceThroughTable();
            var weak   = new TypeWithWeakReference()
            {
                WeakReference = new WeakReference(strong)
            };

            table.Add(strong, weak);

            GC.Collect();

            TypeWithWeakReference result = null;

            Assert.That(table.TryGetValue(strong, out result), Is.True);
            Assert.That(result, Is.SameAs(weak));

            var weakHandleToStrong = new WeakReference(strong);

            strong = null;

            GC.Collect();

            Assert.That(weakHandleToStrong.IsAlive, Is.False);
        }
        public void Should_release_value_when_there_are_no_more_references()
        {
            var table = new ConditionalWeakTable<TypeWithStrongReferenceThroughTable, TypeWithWeakReference>();

            var strong = new TypeWithStrongReferenceThroughTable();
            var weak = new TypeWithWeakReference()
                {
                    WeakReference = new WeakReference(strong)
                };

            table.Add(strong, weak);

            GC.Collect();

            TypeWithWeakReference result = null;
            Assert.That(table.TryGetValue(strong, out result), Is.True);
            Assert.That(result, Is.SameAs(weak));

            var weakHandleToStrong = new WeakReference(strong);

            strong = null;

            GC.Collect();

            Assert.That(weakHandleToStrong.IsAlive, Is.False);
        }
Exemple #3
0
        public void Should_release_value_when_there_are_no_more_references()
        {
            // Arrange
            var table = new ConditionalWeakTable <TypeWithStrongReferenceThroughTable, TypeWithWeakReference>();

            var strong = new TypeWithStrongReferenceThroughTable();
            var weak   = new TypeWithWeakReference()
            {
                WeakReference = new WeakReference(strong)
            };

            table.Add(strong, weak);

            var weakHandleToStrong = new WeakReference(strong);

            // Act
            strong = null;
            GC.Collect();

            // Assert
            weakHandleToStrong.IsAlive.Should().BeFalse();
        }
Exemple #4
0
        public void Should_retain_value_when_references_exist()
        {
            // Arrange
            var table = new ConditionalWeakTable <TypeWithStrongReferenceThroughTable, TypeWithWeakReference>();

            var strong = new TypeWithStrongReferenceThroughTable();
            var weak   = new TypeWithWeakReference()
            {
                WeakReference = new WeakReference(strong)
            };

            table.Add(strong, weak);

            // Act
            GC.Collect();

            // Assert
            TypeWithWeakReference result = null;

            table.TryGetValue(strong, out result).Should().BeTrue();
            result.Should().BeSameAs(weak);
        }