Ejemplo n.º 1
0
        [Retry(5)] // TODO make this test non-flaky.
        public async Task EventuallyCollectNulls()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            Assert.AreEqual(0, d.Test.GetValues(1).Length);

            // gen 1
            d.Set(1, "one");
            Assert.AreEqual(1, d.Test.GetValues(1).Length);

            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            await d.CollectAsync();

            SnapDictionary <int, string> .TestHelper.GenVal[] tv = d.Test.GetValues(1);
            Assert.AreEqual(1, tv.Length);
            Assert.AreEqual(1, tv[0].Gen);

            SnapDictionary <int, string> .Snapshot s = d.CreateSnapshot();
            Assert.AreEqual("one", s.Get(1));

            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);

            Assert.AreEqual(1, d.Count);
            Assert.AreEqual(1, d.SnapCount);
            Assert.AreEqual(1, d.GenCount);

            // gen 2
            d.Clear(1);
            tv = d.Test.GetValues(1);
            Assert.AreEqual(2, tv.Length);
            Assert.AreEqual(2, tv[0].Gen);

            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            Assert.AreEqual(1, d.Count);
            Assert.AreEqual(1, d.SnapCount);
            Assert.AreEqual(1, d.GenCount);

            // nothing to collect
            await d.CollectAsync();

            GC.KeepAlive(s);
            Assert.AreEqual(2, d.Test.GetValues(1).Length);

            Assert.AreEqual(1, d.Count);
            Assert.AreEqual(1, d.SnapCount);
            Assert.AreEqual(1, d.GenCount);

            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            // collect snapshot
            // don't collect liveGen+
            s = null;     // without being disposed
            GC.Collect(); // should release the generation reference
            await d.CollectAsync();

            Assert.AreEqual(1, d.Test.GetValues(1).Length); // "one" value is gone
            Assert.AreEqual(1, d.Count);                    // still have 1 item
            Assert.AreEqual(0, d.SnapCount);                // snapshot is gone
            Assert.AreEqual(0, d.GenCount);                 // and generation has been dequeued

            // liveGen/nextGen
            s = d.CreateSnapshot();
            s = null;

            // collect liveGen
            GC.Collect();

            Assert.IsTrue(d.Test.GenObjs.TryPeek(out global::Umbraco.Cms.Infrastructure.PublishedCache.Snap.GenObj genObj));
            genObj = null;

            // in Release mode, it works, but in Debug mode, the weak reference is still alive
            // and for some reason we need to do this to ensure it is collected
#if DEBUG
            await d.CollectAsync();

            GC.Collect();
#endif

            Assert.IsTrue(d.Test.GenObjs.TryPeek(out genObj));
            Assert.IsFalse(genObj.WeakGenRef.IsAlive); // snapshot is gone, along with its reference

            await d.CollectAsync();

            Assert.AreEqual(0, d.Test.GetValues(1).Length); // null value is gone
            Assert.AreEqual(0, d.Count);                    // item is gone
            Assert.AreEqual(0, d.Test.GenObjs.Count);
            Assert.AreEqual(0, d.SnapCount);                // snapshot is gone
            Assert.AreEqual(0, d.GenCount);                 // and generation has been dequeued
        }
Ejemplo n.º 2
0
        public async Task CollectNulls()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            // gen 1
            d.Set(1, "one");
            Assert.AreEqual(1, d.Test.GetValues(1).Length);
            d.Set(1, "one");
            Assert.AreEqual(1, d.Test.GetValues(1).Length);
            d.Set(1, "uno");
            Assert.AreEqual(1, d.Test.GetValues(1).Length);

            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            SnapDictionary <int, string> .Snapshot s1 = d.CreateSnapshot();

            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);

            // gen 2
            Assert.AreEqual(1, d.Test.GetValues(1).Length);
            d.Set(1, "one");
            Assert.AreEqual(2, d.Test.GetValues(1).Length);
            d.Set(1, "uno");
            Assert.AreEqual(2, d.Test.GetValues(1).Length);

            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            SnapDictionary <int, string> .Snapshot s2 = d.CreateSnapshot();

            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);

            // gen 3
            Assert.AreEqual(2, d.Test.GetValues(1).Length);
            d.Set(1, "one");
            Assert.AreEqual(3, d.Test.GetValues(1).Length);
            d.Set(1, "uno");
            Assert.AreEqual(3, d.Test.GetValues(1).Length);
            d.Clear(1);
            Assert.AreEqual(3, d.Test.GetValues(1).Length);

            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            SnapDictionary <int, string> .TestHelper.GenVal[] tv = d.Test.GetValues(1);
            Assert.AreEqual(3, tv[0].Gen);
            Assert.AreEqual(2, tv[1].Gen);
            Assert.AreEqual(1, tv[2].Gen);

            Assert.AreEqual(0, d.Test.FloorGen);

            // nothing to collect
            await d.CollectAsync();

            GC.KeepAlive(s1);
            GC.KeepAlive(s2);
            Assert.AreEqual(0, d.Test.FloorGen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);
            Assert.AreEqual(2, d.SnapCount);
            Assert.AreEqual(3, d.Test.GetValues(1).Length);

            // one snapshot to collect
            s1 = null;
            GC.Collect();
            GC.KeepAlive(s2);
            await d.CollectAsync();

            Assert.AreEqual(1, d.Test.FloorGen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);
            Assert.AreEqual(1, d.SnapCount);
            Assert.AreEqual(2, d.Test.GetValues(1).Length);

            // another snapshot to collect
            s2 = null;
            GC.Collect();
            await d.CollectAsync();

            Assert.AreEqual(2, d.Test.FloorGen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);
            Assert.AreEqual(0, d.SnapCount);

            // and everything is gone?
            // no, cannot collect the live gen because we'd need to lock
            Assert.AreEqual(1, d.Test.GetValues(1).Length);

            d.CreateSnapshot();
            GC.Collect();
            await d.CollectAsync();

            // poof, gone
            Assert.AreEqual(0, d.Test.GetValues(1).Length);
        }