예제 #1
0
        public void TryAdd()
        {
            const string KEY   = "key";
            const string VALUE = "value";

            var sharedDictionary = new SharedDictionary <string, string>(LockingStrategy);

            bool doInsert = false;

            using (ISharedCollectionLock l = sharedDictionary.GetReadLock())
            {
                if (!sharedDictionary.ContainsKey(KEY))
                {
                    doInsert = true;
                }
            }

            if (doInsert)
            {
                using (ISharedCollectionLock l = sharedDictionary.GetWriteLock())
                {
                    if (!sharedDictionary.ContainsKey(KEY))
                    {
                        sharedDictionary.Add(KEY, VALUE);
                    }
                }
            }

            CollectionAssert.AreEqual(new Dictionary <string, string> {
                { KEY, VALUE }
            }, sharedDictionary.BackingDictionary);
        }
예제 #2
0
        public void DisposedWriteLockDeniesWrite()
        {
            var sharedList = new SharedList <string>(LockingStrategy);

            ISharedCollectionLock l = sharedList.GetWriteLock();

            l.Dispose();

            sharedList[0] = "foo";
        }
예제 #3
0
        public void DisposedWriteLockDeniesRead()
        {
            var d = new SharedList <string>(LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            d.Contains("foo");
        }
예제 #4
0
        public void CanGetAnotherLockAfterDisposingLock()
        {
            var d = new SharedList <string>(LockingStrategy);
            ISharedCollectionLock l = d.GetReadLock();

            l.Dispose();

            l = d.GetReadLock();
            l.Dispose();
        }
        public void DisposedReadLockDeniesRead()
        {
            var d = new SharedDictionary <string, string>(LockingStrategy);

            ISharedCollectionLock l = d.GetReadLock();

            l.Dispose();

            d.ContainsKey("foo");
        }
        public void DisposedWriteLockDeniesWrite()
        {
            var d = new SharedDictionary <string, string>(LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            d["ke"] = "foo";
        }
예제 #7
0
        public void DisposedWriteLockDeniesWrite()
        {
            var d = new SharedDictionary <string, string>(this.LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            Assert.Throws <WriteLockRequiredException>(() => d["ke"] = "foo");
        }
예제 #8
0
        public void DisposedWriteLockDeniesRead()
        {
            var d = new SharedDictionary <string, string>(this.LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            Assert.Throws <ReadLockRequiredException>(() => d.ContainsKey("foo"));
        }
예제 #9
0
        public void DisposedWriteLockDeniesWrite()
        {
            var sharedList = new SharedList <string>(this.LockingStrategy);

            ISharedCollectionLock l = sharedList.GetWriteLock();

            l.Dispose();

            Assert.Throws <WriteLockRequiredException>(() => sharedList[0] = "foo");
        }
예제 #10
0
        public void WriteLockEnablesRead()
        {
            var sharedList = InitSharedList("value");

            string actualValue = null;

            using (ISharedCollectionLock l = sharedList.GetWriteLock())
            {
                actualValue = sharedList[0];
            }

            Assert.AreEqual("value", actualValue);
        }
        public void WriteLockEnablesRead()
        {
            var d = InitSharedDictionary("key", "value");

            string actualValue = null;

            using (ISharedCollectionLock l = d.GetWriteLock())
            {
                actualValue = d["key"];
            }

            Assert.AreEqual("value", actualValue);
        }
예제 #12
0
        public void TwoDictsShareALockWriteTest()
        {
            ILockStrategy ls = new ReaderWriterLockStrategy();
            var           d1 = new SharedList <string>(ls);
            var           d2 = new SharedList <string>(ls);

            using (ISharedCollectionLock readLock = d1.GetReadLock())
            {
                using (ISharedCollectionLock writeLock = d2.GetWriteLock())
                {
                    //do nothing
                }
            }
        }
예제 #13
0
        public static void ClearCache()
        {
            CachingProvider.Instance().Clear("Prefix", "DNN_");
            using (ISharedCollectionLock writeLock = dictionaryCache.GetWriteLock())
            {
                dictionaryCache.Clear();
            }

            //log the cache clear event
            var log = new LogInfo {
                LogTypeKey = EventLogController.EventLogType.CACHE_REFRESH.ToString()
            };

            log.LogProperties.Add(new LogDetailInfo("*", "Refresh"));
            LogController.Instance.AddLog(log);
        }
예제 #14
0
        private static object GetCachedDataFromDictionary(CacheItemArgs cacheItemArgs, CacheItemExpiredCallback cacheItemExpired)
        {
            object cachedObject = null;

            bool isFound;

            using (ISharedCollectionLock readLock = dictionaryCache.GetReadLock())
            {
                isFound = dictionaryCache.TryGetValue(cacheItemArgs.CacheKey, out cachedObject);
            }

            if (!isFound)
            {
                // get object from data source using delegate
                try
                {
                    if (cacheItemExpired != null)
                    {
                        cachedObject = cacheItemExpired(cacheItemArgs);
                    }
                    else
                    {
                        cachedObject = null;
                    }
                }
                catch (Exception ex)
                {
                    cachedObject = null;
                    Exceptions.LogException(ex);
                }

                using (ISharedCollectionLock writeLock = dictionaryCache.GetWriteLock())
                {
                    if (!dictionaryCache.ContainsKey(cacheItemArgs.CacheKey))
                    {
                        if (cachedObject != null)
                        {
                            dictionaryCache[cacheItemArgs.CacheKey] = cachedObject;
                        }
                    }
                }
            }

            return(cachedObject);
        }
예제 #15
0
        public void TwoDictsShareALockWriteTest()
        {
            ILockStrategy ls = new ReaderWriterLockStrategy();
            var           d1 = new SharedList <string>(ls);
            var           d2 = new SharedList <string>(ls);

            using (ISharedCollectionLock readLock = d1.GetReadLock())
            {
                Assert.Throws <LockRecursionException>(
                    () =>
                {
                    using (ISharedCollectionLock writeLock = d2.GetWriteLock())
                    {
                        // do nothing
                    }
                });
            }
        }
예제 #16
0
 public NaiveLockingEnumerator(IEnumerator<T> enumerator, ISharedCollectionLock readLock)
 {
     _enumerator = enumerator;
     _readLock = readLock;
 }