Esempio n. 1
0
        public void LockTest()
        {
            var userCount       = 1 * 1000;
            var storageMaxCount = userCount;

            var cache = new TestCacheUserStorage(storageMaxCount);

            cache.BeginUnloadCount = int.MaxValue;
            cache.StopUnloadCount  = int.MaxValue;
            cache.CreateUsers(userCount);
            var users = cache.GetAllUsers();

            var request = new CacheRequest()
            {
                ObjectId = users.First().Id
            };

            CacheLock lockObj = null;

            // чтение потом запись
            var readObj1 = cache.Read(request);

            lockObj = ((TestCacheUserWriteObject)readObj1).Response.Lock;
            Assert.False(lockObj.IsEntered());
            using (var writeObj = cache.Write(request))
            {
                Assert.True(lockObj.IsEntered());
            }
            Assert.False(lockObj.IsEntered());

            // запись потом чтение
            using (var writeObj1 = cache.Write(request))
            {
                Assert.True(lockObj.IsEntered());
                var readObj2 = cache.Read(request);
                Assert.True(lockObj.IsEntered());
                Assert.True(lockObj.IsEntered());
            }
            Assert.False(lockObj.IsEntered());

            // чтение потом чтение
            using (var writeObj1 = cache.Write(request))
            {
                Assert.True(lockObj.IsEntered());
                Assert.Equal(1, lockObj.Count);
                using (var writeObj2 = cache.Write(request))
                {
                    Assert.True(lockObj.IsEntered());
                    Assert.Equal(2, lockObj.Count);
                    Assert.True(ReferenceEquals(writeObj1, writeObj2));
                }
                Assert.True(lockObj.IsEntered());
                Assert.Equal(1, lockObj.Count);
            }
            Assert.False(lockObj.IsEntered());
            Assert.Equal(0, lockObj.Count);
        }