Пример #1
0
        public void GetOrAdd_WithNonEmptyDictionary_ReturnsAddedObject()
        {
            LocalCache        cache     = new LocalCache();
            TestIntegerObject resultInt = cache.GetOrAdd(typeof(TestIntegerObject), () => new TestIntegerObject(TestInteger), out bool wasAdded) as TestIntegerObject;

            Assert.Equal(TestInteger, resultInt.Value);
            Assert.True(wasAdded, "GetOrAdd added an entry to the cache.");

            TestStringObject resultString = cache.GetOrAdd(typeof(TestStringObject), () => new TestStringObject(TestString1), out wasAdded) as TestStringObject;

            Assert.Equal(TestString1, resultString.Value);
            Assert.True(wasAdded, "GetOrAdd added an entry to the cache.");
        }
Пример #2
0
        public void AddOrUpdate_WithNonEmptyDictionary_ReturnsUpdatedObject()
        {
            FailOnErrors = false;

            LocalCache        cache     = new LocalCache();
            TestIntegerObject resultInt = cache.AddOrUpdate(typeof(TestIntegerObject), () => new TestIntegerObject(TestInteger), out bool wasUpdated) as TestIntegerObject;

            Assert.Equal(TestInteger, resultInt.Value);
            Assert.True(wasUpdated);

            TestStringObject resultString = cache.AddOrUpdate(typeof(TestStringObject), () => new TestStringObject(TestString1), out wasUpdated) as TestStringObject;

            Assert.Equal(TestString1, resultString.Value);
            Assert.True(wasUpdated);
        }
Пример #3
0
        public void GetOrAdd_InParallelWithNonEmptyDictionary_ReturnsAddedObject()
        {
            LocalCache        cache     = new LocalCache();
            TestIntegerObject resultInt = cache.GetOrAdd(typeof(TestIntegerObject), () => new TestIntegerObject(TestInteger), out bool wasAdded) as TestIntegerObject;

            Assert.Equal(TestInteger, resultInt.Value);
            Assert.True(wasAdded, "GetOrAdd added an entry to the cache.");

            int addCount = 0;

            Parallel.For(0, 10, i =>
            {
                TestStringObject resultString = cache.GetOrAdd(typeof(TestStringObject), () => new TestStringObject(TestString1), out bool wasAddedInternal) as TestStringObject;
                Assert.Equal(TestString1, resultString.Value);
                if (wasAddedInternal)
                {
                    Interlocked.Increment(ref addCount);
                }
            });

            Assert.Equal(addCount, 1);
        }