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); }
public void DisposedWriteLockDeniesWrite() { var sharedList = new SharedList <string>(LockingStrategy); ISharedCollectionLock l = sharedList.GetWriteLock(); l.Dispose(); sharedList[0] = "foo"; }
public void DisposedWriteLockDeniesRead() { var d = new SharedList <string>(LockingStrategy); ISharedCollectionLock l = d.GetWriteLock(); l.Dispose(); d.Contains("foo"); }
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"; }
public void DisposedWriteLockDeniesWrite() { var d = new SharedDictionary <string, string>(this.LockingStrategy); ISharedCollectionLock l = d.GetWriteLock(); l.Dispose(); Assert.Throws <WriteLockRequiredException>(() => d["ke"] = "foo"); }
public void DisposedWriteLockDeniesRead() { var d = new SharedDictionary <string, string>(this.LockingStrategy); ISharedCollectionLock l = d.GetWriteLock(); l.Dispose(); Assert.Throws <ReadLockRequiredException>(() => d.ContainsKey("foo")); }
public void DisposedWriteLockDeniesWrite() { var sharedList = new SharedList <string>(this.LockingStrategy); ISharedCollectionLock l = sharedList.GetWriteLock(); l.Dispose(); Assert.Throws <WriteLockRequiredException>(() => sharedList[0] = "foo"); }
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); }
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 } } }
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); }
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); }
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 } }); } }
public NaiveLockingEnumerator(IEnumerator<T> enumerator, ISharedCollectionLock readLock) { _enumerator = enumerator; _readLock = readLock; }