public void TestLockOnClosedTransaction()
        {
            using (var connection = new SqlConnection(SqlDistributedLockTest.ConnectionString))
            {
                connection.Open();

                IDisposable handle;
                using (var transaction = connection.BeginTransaction())
                {
                    handle = new SqlDistributedReaderWriterLock("b", transaction).AcquireWriteLock();
                    SqlTransactionScopedDistributedLockTest.IsHeld("b").ShouldEqual(true);
                }
                TestHelper.AssertDoesNotThrow(handle.Dispose);
                SqlTransactionScopedDistributedLockTest.IsHeld("b").ShouldEqual(false);
            }
        }
        public void TestIsNotScopedToTransaction()
        {
            using (var connection = new SqlConnection(SqlDistributedLockTest.ConnectionString))
            {
                connection.Open();

                using (var handle = new SqlDistributedLock("a", connection).Acquire())
                {
                    using (var transaction = connection.BeginTransaction())
                    {
                        transaction.Rollback();
                    }

                    SqlTransactionScopedDistributedLockTest.IsHeld("a").ShouldEqual(true);
                }
            }
        }
        public void TestCloseLockOnClosedConnection()
        {
            using (var connection = new SqlConnection(SqlDistributedLockTest.ConnectionString))
            {
                TestHelper.AssertThrows <InvalidOperationException>(() => new SqlDistributedLock("a", connection).Acquire());

                connection.Open();

                var @lock  = new SqlDistributedLock("a", connection);
                var handle = @lock.Acquire();
                SqlTransactionScopedDistributedLockTest.IsHeld("a").ShouldEqual(true);

                connection.Dispose();

                TestHelper.AssertDoesNotThrow(handle.Dispose);

                // lock can be re-acquired
                SqlTransactionScopedDistributedLockTest.IsHeld("a").ShouldEqual(false);
            }
        }
        public void TestLockOnCommittedBackTransaction()
        {
            using (var connection = new SqlConnection(SqlDistributedLockTest.ConnectionString))
            {
                connection.Open();

                using (var transaction = connection.BeginTransaction())
                {
                    var handle = new SqlDistributedReaderWriterLock("d", transaction).AcquireWriteLock();
                    SqlTransactionScopedDistributedLockTest.IsHeld("d").ShouldEqual(true);

                    transaction.Commit();

                    TestHelper.AssertDoesNotThrow(handle.Dispose);
                    SqlTransactionScopedDistributedLockTest.IsHeld("d").ShouldEqual(false);

                    TestHelper.AssertThrows <InvalidOperationException>(() => new SqlDistributedReaderWriterLock("d", transaction).AcquireWriteLock());
                }
            }
        }