Пример #1
0
        public void GetOrCreate()
        {
            var cnt = 0;
            var key = 10;
            var src = new CacheCollection <int, int>(n => Sigma(n));

            src.Created += (s, e) => ++ cnt;
            Parallel.For(0, 10, _ => src.GetOrCreate(key));
            Task.Delay(200).Wait();

            Assert.That(cnt, Is.EqualTo(1));
            Assert.That(src.Count, Is.EqualTo(1));
            Assert.That(src.TryGetValue(key, out var _), Is.True, nameof(src.TryGetValue));
            Assert.That(src.TryGetValue(999, out var _), Is.False, nameof(src.TryGetValue));
            Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
            Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

            src.Remove(key);
            src.Clear();
            Assert.That(src.Count, Is.EqualTo(0));
        }
Пример #2
0
        public void GetOrCreate()
        {
            using (var src = new CacheCollection <int, int>(i => Sigma(i)))
            {
                var n   = 0;
                var key = 10;

                src.Created += (s, e) => ++ n;
                Parallel.For(0, 10, e => src.GetOrCreate(key));

                Assert.That(Wait(() => n > 0), "Timeout");
                Assert.That(src.Count, Is.EqualTo(1), nameof(src.Count));
                Assert.That(src.TryGetValue(key, out _), Is.True, nameof(src.TryGetValue));
                Assert.That(src.TryGetValue(999, out _), Is.False, nameof(src.TryGetValue));
                Assert.That(src.Contains(key), Is.True, nameof(src.Contains));
                Assert.That(src.GetOrCreate(key), Is.EqualTo(55), nameof(src.GetOrCreate));

                src.Remove(key);
                src.Clear();
                Assert.That(src.Count, Is.EqualTo(0));
            }
        }