public void SharedTest1()
        {
            var commandLock = new CommandLock();

            try
            {
                // should be able to aquire multiple shared locks
                Task t1 = commandLock.AquireShared();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);

                Task<ExclusiveLockToken> t3 = commandLock.AquireExclusive();
                Assert.False(t3.IsCompleted);

                commandLock.ReleaseShared();
                Assert.False(t3.IsCompleted);

                commandLock.ReleaseShared();
                Assert.True(t3.IsCompleted);
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
        public void ExclusiveThenSharedTest()
        {
            var commandLock = new CommandLock();

            try
            {
                // Part 1 - try and aquire the shared lock while the exclusive lock is held
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.False(t2.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.Close();
                Assert.True(t2.IsCompleted, "Closing t1 should signal t2");
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "Closing the token should zero it out");

                commandLock.ReleaseShared();


                // Part 2 - release the exclusive lock before attempting to aquire the shared lock
                t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                t1.Result.Close();

                t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted, "Shared lock should be immediately aquired");
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
Beispiel #3
0
        public void Close()
        {
            int value = _value;
            if (value != 0)
            {
                var commandLock = _commandLock;
                _value = 0;
                _commandLock = null;

                commandLock.ReleaseExclusive(value);
            }
        }
Beispiel #4
0
        internal ExclusiveLockToken(CommandLock commandLock, int value)
        {
            // Either both commandLock and value are non-zero (non-Null case), or both are zero (Null case)
            if (commandLock == null)
                throw new ArgumentNullException("commandLock");

            if (value == 0)
                throw new ArgumentOutOfRangeException("value");

            _commandLock = commandLock;
            _value = value;
        }
Beispiel #5
0
        public void Close()
        {
            int value = _value;

            if (value != 0)
            {
                var commandLock = _commandLock;
                _value       = 0;
                _commandLock = null;

                commandLock.ReleaseExclusive(value);
            }
        }
Beispiel #6
0
        public void ConvertToSharedLock()
        {
            int value = _value;
            if (value == 0)
            {
                throw new ObjectDisposedException("ExclusiveLockToken");
            }

            var commandLock = _commandLock;
            _value = 0;
            _commandLock = null;

            commandLock.ConvertExclusiveLockToShared(value);
        }
Beispiel #7
0
        public void ConvertToSharedLock()
        {
            int value = _value;

            if (value == 0)
            {
                throw new ObjectDisposedException("ExclusiveLockToken");
            }

            var commandLock = _commandLock;

            _value       = 0;
            _commandLock = null;

            commandLock.ConvertExclusiveLockToShared(value);
        }
Beispiel #8
0
        internal ExclusiveLockToken(CommandLock commandLock, int value)
        {
            // Either both commandLock and value are non-zero (non-Null case), or both are zero (Null case)
            if (commandLock == null)
            {
                throw new ArgumentNullException("commandLock");
            }

            if (value == 0)
            {
                throw new ArgumentOutOfRangeException("value");
            }

            _commandLock = commandLock;
            _value       = value;
        }
Beispiel #9
0
        public void ExclusiveTest1()
        {
            using (var commandLock = new CommandLock())
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                Task<ExclusiveLockToken> t2 = commandLock.AquireExclusive();
                Assert.False(t2.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.Close();
                Assert.True(t2.IsCompleted, "Closing t1 should signal t2");
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "Closing the token should zero it out");
            }
        }
Beispiel #10
0
        public void CloseAbortsOperationsTest()
        {
            Task[] pendingTasks = new Task[2];

            var commandLock = new CommandLock();

            try
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                pendingTasks[0] = commandLock.AquireExclusive();
                Assert.False(pendingTasks[0].IsCompleted);

                pendingTasks[1] = commandLock.AquireShared();
                Assert.False(pendingTasks[1].IsCompleted);
            }
            finally
            {
                commandLock.Close("CloseAbortsOperationsTest complete");
            }

            foreach (Task t in pendingTasks)
            {
                Assert.True(t.IsCompleted);
                Assert.IsType(typeof(DebuggerDisposedException), t.Exception.InnerException);
                Assert.Equal("CloseAbortsOperationsTest complete", t.Exception.InnerException.Message);
            }
        }
Beispiel #11
0
        public void ConvertToSharedLockTest2()
        {
            // NOTE: This test covers the case that there are NOT pending shared locks
            var commandLock = new CommandLock();

            try
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                ExclusiveLockToken token = t1.Result;
                token.ConvertToSharedLock();
                Assert.True(ExclusiveLockToken.IsNullOrClosed(token), "ConvertToSharedLock should have nulled out the token");

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);
            }
            finally
            {
                commandLock.Close("Test complete");
            }
        }
Beispiel #12
0
        public void SharedThenExclusiveTest()
        {
            using (var commandLock = new CommandLock())
            {
                // should be able to aquire multiple shared locks
                Task t1 = commandLock.AquireShared();
                Assert.True(t1.IsCompleted);

                Task t2 = commandLock.AquireShared();
                Assert.True(t2.IsCompleted);

                commandLock.ReleaseShared();
                commandLock.ReleaseShared();

                Task<ExclusiveLockToken> t3 = commandLock.AquireExclusive();
                Assert.True(t3.IsCompleted);
            }
        }
Beispiel #13
0
        public void CloseAbortsOperationsTest()
        {
            Task[] pendingTasks = new Task[2];

            using (var commandLock = new CommandLock())
            {
                Task<ExclusiveLockToken> t1 = commandLock.AquireExclusive();
                Assert.True(t1.IsCompleted);

                pendingTasks[0] = commandLock.AquireExclusive();
                Assert.False(pendingTasks[0].IsCompleted);

                pendingTasks[1] = commandLock.AquireShared();
                Assert.False(pendingTasks[1].IsCompleted);

                commandLock.Close();
            }

            foreach (Task t in pendingTasks)
            {
                Assert.True(t.IsCompleted);
                Assert.IsType(typeof(ObjectDisposedException), t.Exception.InnerException);
            }
        }