Exemple #1
0
        public void TestStateChanges()
        {
            var lck = Ignite.GetOrCreateLock(TestUtils.TestName);
            var cfg = lck.Configuration;

            Assert.IsFalse(cfg.IsFailoverSafe);
            Assert.IsFalse(cfg.IsFair);
            Assert.AreEqual(TestUtils.TestName, cfg.Name);

            Assert.False(lck.IsEntered());
            Assert.False(lck.IsBroken());

            Assert.IsTrue(lck.TryEnter());

            Assert.IsTrue(lck.IsEntered());
            Assert.False(lck.IsBroken());

            lck.Exit();

            Assert.False(lck.IsEntered());
            Assert.False(lck.IsBroken());

            lck.Remove();

            Assert.IsTrue(lck.IsRemoved());
        }
Exemple #2
0
        public void TestEnterBlocksWhenLockedByAnotherThread()
        {
            long state = 0;

            var lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);

            lock1.Enter();

            // ReSharper disable once AccessToModifiedClosure
            var task = Task.Factory.StartNew(() =>
            {
                var lock2 = Ignite.GetOrCreateLock(TestUtils.TestName);
                Interlocked.Increment(ref state);
                lock2.Enter();
                Interlocked.Increment(ref state);
                lock2.Exit();
                Interlocked.Increment(ref state);
            });

            TestUtils.WaitForTrueCondition(() => Interlocked.Read(ref state) == 1);
            Assert.AreEqual(1, Interlocked.Read(ref state));

            lock1.Exit();
            task.Wait();
            Assert.AreEqual(3, Interlocked.Read(ref state));
        }
Exemple #3
0
        public void TestLockConfigurationCantBeModifiedAfterLockCreation()
        {
            var cfg = new LockConfiguration
            {
                Name           = TestUtils.TestName,
                IsFair         = true,
                IsFailoverSafe = true
            };

            var lck = Ignite.GetOrCreateLock(cfg, true);

            // Change original instance.
            cfg.Name           = "y";
            cfg.IsFair         = false;
            cfg.IsFailoverSafe = false;

            // Change returned instance.
            lck.Configuration.Name           = "y";
            lck.Configuration.IsFair         = false;
            lck.Configuration.IsFailoverSafe = false;

            // Verify: actual config has not changed.
            Assert.AreEqual(TestUtils.TestName, lck.Configuration.Name);
            Assert.IsTrue(lck.Configuration.IsFair);
            Assert.IsTrue(lck.Configuration.IsFailoverSafe);
        }
Exemple #4
0
        public void TestTryEnterReturnsTrueWhenUnlocked()
        {
            var lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);

            Assert.IsTrue(lock1.TryEnter());
            Assert.IsTrue(lock1.TryEnter(TimeSpan.Zero));
            Assert.IsTrue(lock1.TryEnter(TimeSpan.FromMilliseconds(50)));

            lock1.Exit();
        }
Exemple #5
0
        public void TestExitThrowsCorrectExceptionWhenNotEntered()
        {
            var lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);

            var ex      = Assert.Throws <SynchronizationLockException>(() => lock1.Exit());
            var innerEx = ex.InnerException as JavaException;

            Assert.IsNotNull(innerEx);
            Assert.AreEqual("java.lang.IllegalMonitorStateException", innerEx.JavaClassName);
        }
Exemple #6
0
        public void TestRemovedBeforeUseLockThrowsIgniteException()
        {
            var lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);
            var lock2 = Ignite2.GetOrCreateLock(TestUtils.TestName);

            lock1.Remove();

            var ex = Assert.Throws <IgniteException>(() => lock2.Enter());

            Assert.AreEqual("Failed to find reentrant lock with given name: " + lock2.Configuration.Name, ex.Message);
        }
Exemple #7
0
        public void TestFairLockGuaranteesOrder()
        {
            const int count = 20;

            var cfg = new LockConfiguration
            {
                Name           = TestUtils.TestName,
                IsFair         = true,
                IsFailoverSafe = true
            };

            var lck = Ignite.GetOrCreateLock(cfg, true);

            lck.Enter();

            var locks   = new ConcurrentQueue <int>();
            var threads = new Thread[count];

            var evt = new AutoResetEvent(false);

            for (int i = 0; i < count; i++)
            {
                var id = i;

                var thread = new Thread(() =>
                {
                    evt.Set();
                    lck.Enter();
                    locks.Enqueue(id);
                    lck.Exit();
                });

                thread.Start();

                evt.WaitOne();

                Thread.Sleep(100);

                threads[i] = thread;
            }

            lck.Exit();

            foreach (var thread in threads)
            {
                thread.Join();
            }

            Assert.AreEqual(count, locks.Count);
            CollectionAssert.IsOrdered(locks);
        }
Exemple #8
0
        public void TestTryEnterReturnsFalseWhenLocked()
        {
            var lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);
            var lock2 = Ignite.GetOrCreateLock(TestUtils.TestName);

            lock1.Enter();

            Task.Factory.StartNew(() =>
            {
                Assert.IsFalse(lock2.TryEnter());
                Assert.IsFalse(lock2.TryEnter(TimeSpan.Zero));
                Assert.IsFalse(lock2.TryEnter(TimeSpan.FromMilliseconds(50)));
            }).Wait();

            lock1.Exit();
        }
Exemple #9
0
        public void TestReentrancy()
        {
            const int count = 10;
            var       lock1 = Ignite.GetOrCreateLock(TestUtils.TestName);

            for (var i = 0; i < count; i++)
            {
                lock1.Enter();
                Assert.IsTrue(lock1.IsEntered());
            }

            for (var i = 0; i < count; i++)
            {
                Assert.IsTrue(lock1.IsEntered());
                lock1.Exit();
            }

            Assert.IsFalse(lock1.IsEntered());
        }
Exemple #10
0
        public void TestEnteredLockThrowsOnRemove()
        {
            var cfg = new LockConfiguration
            {
                Name = TestUtils.TestName
            };

            var lck = Ignite.GetOrCreateLock(cfg, true);

            lck.Enter();
            Assert.IsTrue(lck.IsEntered());

            var ex = Assert.Throws <IgniteException>(() => lck.Remove());

            StringAssert.StartsWith("Failed to remove reentrant lock with blocked threads", ex.Message);

            lck.Exit();
            lck.Remove();

            Assert.IsNull(Ignite.GetOrCreateLock(cfg, false));
        }
Exemple #11
0
 public void TestGetOrCreateLockReturnsNullOnMissingLockWhenCreateFlagIsNotSet()
 {
     Assert.IsNull(Ignite.GetOrCreateLock(new LockConfiguration {
         Name = TestUtils.TestName
     }, false));
 }