Exemplo n.º 1
0
        public void TryAdd()
        {
            const string value = "value";

            var sharedList = new SharedList <string>(LockingStrategy);

            bool doInsert = false;

            using (ISharedCollectionLock l = sharedList.GetReadLock())
            {
                if (!sharedList.Contains(value))
                {
                    doInsert = true;
                }
            }

            if (doInsert)
            {
                using (ISharedCollectionLock l = sharedList.GetWriteLock())
                {
                    if (!sharedList.Contains(value))
                    {
                        sharedList.Add(value);
                    }
                }
            }

            CollectionAssert.AreEqual(new List <string> {
                value
            }, sharedList.BackingList);
        }
Exemplo n.º 2
0
        public void DisposedWriteLockDeniesWrite()
        {
            var sharedList = new SharedList <string>(LockingStrategy);

            ISharedCollectionLock l = sharedList.GetWriteLock();

            l.Dispose();

            sharedList[0] = "foo";
        }
Exemplo n.º 3
0
        public void DisposedWriteLockDeniesRead()
        {
            var d = new SharedList <string>(LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            d.Contains("foo");
        }
Exemplo n.º 4
0
        public void DisposedWriteLockDeniesWrite()
        {
            var sharedList = new SharedList <string>(this.LockingStrategy);

            ISharedCollectionLock l = sharedList.GetWriteLock();

            l.Dispose();

            Assert.Throws <WriteLockRequiredException>(() => sharedList[0] = "foo");
        }
Exemplo n.º 5
0
        public void DisposedWriteLockDeniesRead()
        {
            var d = new SharedList <string>(this.LockingStrategy);

            ISharedCollectionLock l = d.GetWriteLock();

            l.Dispose();

            Assert.Throws <ReadLockRequiredException>(() => d.Contains("foo"));
        }
Exemplo n.º 6
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
                }
            }
        }
Exemplo n.º 7
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
                    }
                });
            }
        }