Пример #1
0
        public async Task GetOne_AfterGet_ValueMustEqual()
        {
            var pool = new ValueObjectUsingPool();
            var a    = await pool.GetAsync(1);

            var b = await pool.GetAsync(1);

            Assert.AreEqual(a, b);
            Assert.AreEqual(1, pool.Keys.Single());
            Assert.IsTrue(pool.ContainsKey(1));
            Assert.IsFalse(pool.ContainsKey(0));
            var val = pool.GetUseCount(1);

            Assert.AreEqual(2, val.Value);
            var count = pool.GetUseCount(0);

            Assert.IsNull(count);
        }
Пример #2
0
        public async Task ConcurrentGet_ValueMustEqual()
        {
            var pool  = new ValueObjectUsingPool();
            var objs  = new object[20];
            var tasks = Enumerable.Range(0, objs.Length)
                        .Select(x => pool.GetAsync(1).ContinueWith(y => objs[x] = y.Result))
                        .ToArray();
            await Task.WhenAll(tasks);

            var val = await pool.GetAsync(1);

            Assert.IsNotNull(val);
            for (int i = 0; i < objs.Length; i++)
            {
                if (objs[i] != val)
                {
                    Assert.Fail(i.ToString());
                }
            }
            var count = pool.GetUseCount(1);

            Assert.AreEqual(21, count.Value);
        }