Esempio n. 1
0
        public void LockAndLoadAsync_WaitsForLock()
        {
            const int    numberOfLocks = 512;
            const string key           = "key1";

            multiLock = new MultiLock(numberOfLocks, doEnableReentrantLocking: false);

            long[] entered = { 0L };
            long[] locked  = { 0L };

            multiLock.Take(key);

            var runResult = Task.Run(() =>
            {
                #pragma warning disable 1998
                var _ = multiLock.LockAndLoadAsync(key, async() =>
                {
                    var result = Interlocked.Increment(ref entered[0]);
                    var ln     = GetLockNumber(key);
                    var l      = GetLock(ln);
                    if (l.CurrentCount > 0)
                    {
                        Interlocked.Increment(ref locked[0]);
                    }
                    return(result);
                });
                _.Wait();
                #pragma warning restore 1998
            });

            // Give the task time to start
            Thread.Sleep(100);

            try
            {
                Assert.IsTrue(Interlocked.Read(ref entered[0]) == 0L, "Should wait for lock");
            }
            finally
            {
                multiLock.Release(key);
            }

            runResult.Wait(100);
            Assert.IsTrue(Interlocked.Read(ref entered[0]) != 0L, "Should have gotten lock");
            Assert.IsTrue(Interlocked.Read(ref locked[0]) == 0L, "Lock should have been taken");

            var lockNumber = GetLockNumber(key);
            var lck        = GetLock(lockNumber);

            Assert.IsTrue(lck.CurrentCount > 0, "Lock should have been released");
        }
Esempio n. 2
0
        public void LockAndLoadAsync_WaitsForLock_Timeout_NoThrow()
        {
            const int    numberOfLocks = 512;
            const string key           = "key1";

            multiLock = new MultiLock(numberOfLocks, 10, false, doEnableReentrantLocking: false);

            long[] entered = { 0L };
            long[] locked  = { 0L };

            multiLock.Take(key);

            var lockNumber = GetLockNumber(key);
            var lck        = GetLock(lockNumber);

            try
            {
                #pragma warning disable 1998
                var _ = multiLock.LockAndLoadAsync(key, async() =>
                {
                    var result = Interlocked.Increment(ref entered[0]);
                    var ln     = GetLockNumber(key);
                    var l      = GetLock(ln);
                    if (l.CurrentCount > 0)
                    {
                        Interlocked.Increment(ref locked[0]);
                    }
                    return(result);
                });
                _.Wait();
                #pragma warning restore 1998
            }
            finally
            {
                var lockCount = lck.CurrentCount;

                multiLock.Release(key);

                Assert.IsTrue(Interlocked.Read(ref entered[0]) != 0L, "Should have timed out and entered");
                Assert.IsTrue(lockCount == 0, "Lock should have been taken");
                Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released");;
            }
        }
Esempio n. 3
0
        public void TakeAndRelease_CheckLock()
        {
            const int    numberOfLocks = 13;
            const string key           = "theKey";

            multiLock = new MultiLock(numberOfLocks);

            var lockNumber = GetLockNumber(key);
            var lck        = GetLock(lockNumber);

            multiLock.Take(key);
            try
            {
                Assert.IsTrue(lck.CurrentCount == 0, "Lock should have been taken");
            }
            finally
            {
                multiLock.Release(key);
            }

            Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released");
        }
Esempio n. 4
0
        public void LockAndLoad_WaitsForLock_Timeout_Throw()
        {
            const int    numberOfLocks = 512;
            const string key           = "key1";

            multiLock = new MultiLock(numberOfLocks, 10, true, doEnableReentrantLocking: false);

            long[] entered = { 0L };
            long[] locked  = { 0L };

            multiLock.Take(key);

            var lockNumber = GetLockNumber(key);
            var lck        = GetLock(lockNumber);

            try
            {
                multiLock.LockAndLoad(key, () =>
                {
                    var result = Interlocked.Increment(ref entered[0]);
                    var ln     = GetLockNumber(key);
                    var l      = GetLock(ln);
                    if (l.CurrentCount > 0)
                    {
                        Interlocked.Increment(ref locked[0]);
                    }
                    return(result);
                });
            }
            finally
            {
                var lockCount = lck.CurrentCount;

                multiLock.Release(key);

                Assert.IsTrue(lockCount == 0, "Lock should have been taken");
                Assert.IsTrue(lck.CurrentCount == 1, "Lock should have been released");
            }
        }