예제 #1
0
        public void TestCascadingEagerCache()
        {
            // We set one cell and two caches in cascade
            var cell   = new Cell <int>(2);
            var cache1 = new EagerCache <int>(() => cell.Value + 1);
            var cache2 = new EagerCache <int>(() => cache1.Value * 2);

            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(6, cache2.Value);

            // Checking that setting the base cell invalidate all caches
            cell.Value = 3;
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(8, cache2.Value);

            // Set cache1 to an arbitrary value. This should invalidate the cache2
            cache1.Value = 34;
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(68, cache2.Value);

            // Setting the cell to a different value should have no impact
            cell.Value++;
            Assert.AreEqual(0, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(68, cache2.Value);

            // Clearing the cache1 should invalidate cache2
            cache1.Clear();
            Assert.AreEqual(1, cache1.BeaconCount);
            Assert.AreEqual(1, cache2.BeaconCount);
            Assert.AreEqual(10, cache2.Value);
        }
예제 #2
0
        public void TestBasicEagerCache()
        {
            // Setting a cell and a cache depending on that cell
            var cell  = new Cell <int>(2);
            var count = 0;
            var cache = new EagerCache <int>(() =>
            {
                count++;
                return(cell.Value * 2);
            });

            // The cache is already initialized
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, count);
            Assert.AreEqual(1, cache.BeaconCount);

            // Confirming the value
            var value = cache.Value;

            Assert.AreEqual(4, value);

            // Reading the value again doesn't trigger a recalculation
            value = cache.Value;
            Assert.AreEqual(4, value);
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, count);
            Assert.AreEqual(1, cache.BeaconCount);

            // If we change the cell, the cache recalculates immediately
            cell.Value = 3;
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(2, count);
            Assert.AreEqual(1, cache.BeaconCount);
            Assert.AreEqual(6, cache.Value);

            // Setting the cache cuts all dependencies
            cache.Value = 28;
            Assert.IsTrue(cache.IsSet);
            Assert.AreEqual(28, cache.Value);
            Assert.AreEqual(0, cache.BeaconCount);

            // This time, no dependencies on cell
            cell.Value++;
            Assert.AreEqual(28, cache.Value);

            // If we clear the cache, it recalculates its value based on its callback
            cache.Clear();
            Assert.IsFalse(cache.IsSet);
            Assert.AreEqual(1, cache.BeaconCount);
            Assert.AreEqual(8, cache.Value);
        }