示例#1
0
        public void WriteLocking()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            // 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);

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

            Assert.AreEqual(1, s1.Gen);
            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);
            Assert.AreEqual("one", s1.Get(1));

            // gen 2
            Assert.AreEqual(1, 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, s2.Gen);
            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);
            Assert.AreEqual("uno", s2.Get(1));

            using (d.GetScopedWriteLock(GetScopeProvider()))
            {
                // gen 3
                Assert.AreEqual(2, d.Test.GetValues(1).Length);
                d.SetLocked(1, "ein");
                Assert.AreEqual(3, d.Test.GetValues(1).Length);

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

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

                Assert.AreEqual(2, s3.Gen);
                Assert.AreEqual(3, d.Test.LiveGen);
                Assert.IsTrue(d.Test.NextGen); // has NOT changed when (non) creating snapshot
                Assert.AreEqual("uno", s3.Get(1));
            }

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

            Assert.AreEqual(3, s4.Gen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);
            Assert.AreEqual("ein", s4.Get(1));
        }
示例#2
0
        public void WriteLockingFirstSnapshot()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            // 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);

            using (d.GetScopedWriteLock(GetScopeProvider()))
            {
                SnapDictionary <int, string> .Snapshot s1 = d.CreateSnapshot();

                Assert.AreEqual(0, s1.Gen);
                Assert.AreEqual(1, d.Test.LiveGen);
                Assert.IsTrue(d.Test.NextGen);
                Assert.IsNull(s1.Get(1));
            }

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

            Assert.AreEqual(1, s2.Gen);
            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);
            Assert.AreEqual("one", s2.Get(1));
        }
示例#3
0
        public async Task RandomTest2()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            d.Set(1, "one");
            d.Set(2, "two");

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

            Assert.AreEqual("one", v1);

            d.Clear(1);

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

            Assert.AreEqual(null, v2);

            v1 = s1.Get(1);
            Assert.AreEqual("one", v1);

            Assert.AreEqual(2, d.SnapCount);

            s1 = null;
            GC.Collect();
            await d.CollectAsync();

            // 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
            GC.Collect();
            await d.CollectAsync();
#endif

            Assert.AreEqual(1, d.SnapCount);
            v2 = s2.Get(1);
            Assert.AreEqual(null, v2);

            s2 = null;
            GC.Collect();
            await d.CollectAsync();

            Assert.AreEqual(0, d.SnapCount);
        }
示例#4
0
 public TValue Get(TKey key)
 {
     if (_gen < 0)
     {
         throw new ObjectDisposedException("snapshot" /*+ " (" + _thisCount + ")"*/);
     }
     return(_store.Get(key, _gen));
 }
示例#5
0
        public void MissingReturnsNull()
        {
            var d = new SnapDictionary <int, string>();

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

            Assert.IsNull(s.Get(1));
        }
示例#6
0
        public void DeletedReturnsNull()
        {
            var d = new SnapDictionary <int, string>();

            // gen 1
            d.Set(1, "one");

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

            // gen 2
            d.Clear(1);

            SnapDictionary <int, string> .Snapshot s2 = d.CreateSnapshot();
            Assert.IsNull(s2.Get(1));

            Assert.AreEqual("one", s1.Get(1));
        }
示例#7
0
        public void ScopeLocking1()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            // gen 1
            d.Set(1, "one");
            SnapDictionary <int, string> .Snapshot s1 = d.CreateSnapshot();
            Assert.AreEqual(1, s1.Gen);
            Assert.AreEqual("one", s1.Get(1));

            d.Set(1, "uno");
            SnapDictionary <int, string> .Snapshot s2 = d.CreateSnapshot();
            Assert.AreEqual(2, s2.Gen);
            Assert.AreEqual("uno", s2.Get(1));

            var scopeContext = new ScopeContext();
            ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);

            using (d.GetScopedWriteLock(scopeProvider))
            {
                // creating a snapshot in a write-lock does NOT return the "current" content
                // it uses the previous snapshot, so new snapshot created only on release
                d.SetLocked(1, "ein");
                SnapDictionary <int, string> .Snapshot s3 = d.CreateSnapshot();
                Assert.AreEqual(2, s3.Gen);
                Assert.AreEqual("uno", s3.Get(1));

                // but live snapshot contains changes
                SnapDictionary <int, string> .Snapshot ls = d.Test.LiveSnapshot;
                Assert.AreEqual("ein", ls.Get(1));
                Assert.AreEqual(3, ls.Gen);
            }

            SnapDictionary <int, string> .Snapshot s4 = d.CreateSnapshot();
            Assert.AreEqual(2, s4.Gen);
            Assert.AreEqual("uno", s4.Get(1));

            scopeContext.ScopeExit(true);

            SnapDictionary <int, string> .Snapshot s5 = d.CreateSnapshot();
            Assert.AreEqual(3, s5.Gen);
            Assert.AreEqual("ein", s5.Get(1));
        }
 public TValue Get(TKey key)
 {
     EnsureNotDisposed();
     return(_store.Get(key, _gen));
 }
示例#9
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
        }
示例#10
0
        public void DontPanic()
        {
            var d = new SnapDictionary <int, string>();

            d.Test.CollectAuto = false;

            Assert.IsNull(d.Test.GenObj);

            // gen 1
            d.Set(1, "one");
            Assert.IsTrue(d.Test.NextGen);
            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsNull(d.Test.GenObj);

            SnapDictionary <int, string> .Snapshot s1 = d.CreateSnapshot();
            Assert.IsFalse(d.Test.NextGen);
            Assert.AreEqual(1, d.Test.LiveGen);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(1, d.Test.GenObj.Gen);

            Assert.AreEqual(1, s1.Gen);
            Assert.AreEqual("one", s1.Get(1));

            d.Set(1, "uno");
            Assert.IsTrue(d.Test.NextGen);
            Assert.AreEqual(2, d.Test.LiveGen);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(1, d.Test.GenObj.Gen);

            var scopeContext = new ScopeContext();
            ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);

            // scopeProvider.Context == scopeContext -> writer is scoped
            // writer is scope contextual and scoped
            //  when disposed, nothing happens
            //  when the context exists, the writer is released
            using (d.GetScopedWriteLock(scopeProvider))
            {
                d.SetLocked(1, "ein");
                Assert.IsTrue(d.Test.NextGen);
                Assert.AreEqual(3, d.Test.LiveGen);
                Assert.IsNotNull(d.Test.GenObj);
                Assert.AreEqual(2, d.Test.GenObj.Gen);
            }

            // writer has not released
            Assert.IsTrue(d.Test.IsLocked);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(2, d.Test.GenObj.Gen);

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

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

            Assert.IsTrue(d.Test.IsLocked);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(2, d.Test.GenObj.Gen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

            // release writer
            scopeContext.ScopeExit(true);

            Assert.IsFalse(d.Test.IsLocked);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(2, d.Test.GenObj.Gen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsTrue(d.Test.NextGen);

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

            Assert.IsFalse(d.Test.IsLocked);
            Assert.IsNotNull(d.Test.GenObj);
            Assert.AreEqual(3, d.Test.GenObj.Gen);
            Assert.AreEqual(3, d.Test.LiveGen);
            Assert.IsFalse(d.Test.NextGen);
        }
示例#11
0
        public void ScopeLocking2()
        {
            var d = new SnapDictionary <int, string>();

            SnapDictionary <int, string> .TestHelper t = d.Test;
            t.CollectAuto = false;

            // gen 1
            d.Set(1, "one");
            SnapDictionary <int, string> .Snapshot s1 = d.CreateSnapshot();
            Assert.AreEqual(1, s1.Gen);
            Assert.AreEqual("one", s1.Get(1));

            d.Set(1, "uno");
            SnapDictionary <int, string> .Snapshot s2 = d.CreateSnapshot();
            Assert.AreEqual(2, s2.Gen);
            Assert.AreEqual("uno", s2.Get(1));

            Assert.AreEqual(2, t.LiveGen);
            Assert.IsFalse(t.NextGen);

            var scopeContext = new ScopeContext();
            ICoreScopeProvider scopeProvider = GetScopeProvider(scopeContext);

            using (d.GetScopedWriteLock(scopeProvider))
            {
                // creating a snapshot in a write-lock does NOT return the "current" content
                // it uses the previous snapshot, so new snapshot created only on release
                d.SetLocked(1, "ein");
                SnapDictionary <int, string> .Snapshot s3 = d.CreateSnapshot();
                Assert.AreEqual(2, s3.Gen);
                Assert.AreEqual("uno", s3.Get(1));

                // we made some changes, so a next gen is required
                Assert.AreEqual(3, t.LiveGen);
                Assert.IsTrue(t.NextGen);
                Assert.IsTrue(t.IsLocked);

                // but live snapshot contains changes
                SnapDictionary <int, string> .Snapshot ls = t.LiveSnapshot;
                Assert.AreEqual("ein", ls.Get(1));
                Assert.AreEqual(3, ls.Gen);
            }

            // nothing is committed until scope exits
            Assert.AreEqual(3, t.LiveGen);
            Assert.IsTrue(t.NextGen);
            Assert.IsTrue(t.IsLocked);

            // no changes until exit
            SnapDictionary <int, string> .Snapshot s4 = d.CreateSnapshot();
            Assert.AreEqual(2, s4.Gen);
            Assert.AreEqual("uno", s4.Get(1));

            scopeContext.ScopeExit(false);

            // now things have changed
            Assert.AreEqual(2, t.LiveGen);
            Assert.IsFalse(t.NextGen);
            Assert.IsFalse(t.IsLocked);

            // no changes since not completed
            SnapDictionary <int, string> .Snapshot s5 = d.CreateSnapshot();
            Assert.AreEqual(2, s5.Gen);
            Assert.AreEqual("uno", s5.Get(1));
        }