コード例 #1
0
        public async Task SetLockExclusive()
        {
            string id           = _idGenerator.Next().ToString();
            string lockCategory = _uniqueKeys.GetKey("SLE");
            string comment      = "Exclusive";

            // Set Lock
            Assert.IsTrue(await _locker.SetLockExclusive(lockCategory, id, comment), "A10:  Lock was not set");

            // Make sure Exclusive Lock was set.
            LockObject lockObj = await _locker.LockInfo(lockCategory, id);

            Assert.AreEqual(LockType.Exclusive, lockObj.Type);
            Assert.AreEqual(comment, lockObj.Comment);
        }
コード例 #2
0
        public async Task SetLockFullSuiteTests([Range((int)LockType.ReadOnly, (int)LockType.AppLevel3)] int lockTypeInt)
        {
            // We use our own locker with its own Redis DB for this test so we can adjust TTL's
            RedisLocker rl = new RedisLocker(_redisCacheClient, 1, true);
            await rl.FlushAllLocks();

            rl.TTL = 300;
            int    ttl2        = 2000;
            string lockComment = "FX:11-01-19";

            LockType lockType = (LockType)lockTypeInt;

            string testID_1     = _idGenerator.Next(1000, 19999).ToString();
            string lockCategory = _uniqueKeys.GetKey("SLFST");

            // TestID_1:  Set Lock using base lock method with default TTL
            Assert.IsTrue(await rl.SetLock(lockCategory, testID_1, lockComment, lockType), "A10:  Base SetLock did not work for LockType: {0}", lockType);

            // Make sure the correct type of Lock was set.
            LockObject lockObj = await rl.LockInfo(lockCategory, testID_1);

            Assert.AreEqual(lockType, lockObj.Type, "A11:  Lock was supposed to be set to {0}, but actually was {1}", lockType, lockObj.Type);

            // Make sure it's comment is on it.
            Assert.AreEqual(lockComment, lockObj.Comment, "A12:  Lock Comment is incorrect");

            // TestID_2:  Create a standard lock, but with a time in milli-second override
            string testID_2 = _idGenerator.Next(4000, 4999).ToString();

            Assert.IsTrue(await rl.SetLock(lockCategory, testID_2, lockComment, lockType, ttl2), "A13:  Base SetLock with time override did not work for LockType: {0}", lockType);

            // TestID_3:  Create a standard lock, but with a TimeSpan override
            string   testID_3 = _idGenerator.Next(5000, 5999).ToString();
            TimeSpan t5       = new TimeSpan(0, 0, 0, 2);

            Assert.IsTrue(await rl.SetLock(lockCategory, testID_3, lockComment, t5), "A14:  Base SetLock with time override did not work for LockType: {0}", lockType);
            // Make sure it's comment is on it.
            Assert.AreEqual(lockComment, lockObj.Comment, "A15:  Lock Comment is incorrect");

            // TestID_4-6:  Create a lock of a specific type and then test with default TTL (TestID_4), milli-sec override (TestID_5) and TimeSpan override (TestID_6)
            string   testID_4 = _idGenerator.Next(2000, 2999).ToString();
            string   testID_5 = _idGenerator.Next(3000, 3999).ToString();
            string   testID_6 = _idGenerator.Next(6000, 6999).ToString();
            TimeSpan t6       = new TimeSpan(0, 0, 0, 2);


            // Now set lock using Specific Method
            switch (lockType)
            {
            case LockType.Exclusive:
                Assert.IsTrue(await rl.SetLockExclusive(lockCategory, testID_4, lockComment), "A20: Exclusive SetLock failed.");
                Assert.IsTrue(await rl.SetLockExclusive(lockCategory, testID_5, lockComment, ttl2), "A20B: Exclusive SetLock failed.");
                Assert.IsTrue(await rl.SetLockExclusive(lockCategory, testID_6, lockComment, t6), "A20C: Exclusive SetLock failed.");
                break;

            case LockType.ReadOnly:
                Assert.IsTrue(await rl.SetLockReadOnly(lockCategory, testID_4, lockComment), "A21: ReadOnly SetLock failed.");
                Assert.IsTrue(await rl.SetLockReadOnly(lockCategory, testID_5, lockComment, ttl2), "A21B: ReadOnly SetLock failed.");
                Assert.IsTrue(await rl.SetLockReadOnly(lockCategory, testID_6, lockComment, t6), "A21C: ReadOnly SetLock failed.");

                break;

            case LockType.AppLevel1:
                Assert.IsTrue(await rl.SetLockAppLevel1(lockCategory, testID_4, lockComment), "A23: AppLevel1 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel1(lockCategory, testID_5, lockComment, ttl2), "A23B: AppLevel1 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel1(lockCategory, testID_6, lockComment, t6), "A23C: AppLevel1 SetLock failed.");

                break;

            case LockType.AppLevel2:
                Assert.IsTrue(await rl.SetLockAppLevel2(lockCategory, testID_4, lockComment), "A24: AppLevel2 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel2(lockCategory, testID_5, lockComment, ttl2), "A24B: AppLevel2 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel2(lockCategory, testID_6, lockComment, t6), "A24C: AppLevel2 SetLock failed.");
                break;

            case LockType.AppLevel3:
                Assert.IsTrue(await rl.SetLockAppLevel3(lockCategory, testID_4, lockComment), "A25: AppLevel3 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel3(lockCategory, testID_5, lockComment, ttl2), "A25B: AppLevel3 SetLock failed.");
                Assert.IsTrue(await rl.SetLockAppLevel3(lockCategory, testID_6, lockComment, t6), "A25C: AppLevel3 SetLock failed.");
                break;
            }

            // Validate the Lock was created correctly
            lockObj = await rl.LockInfo(lockCategory, testID_4);

            Assert.AreEqual(lockType, lockObj.Type, "A30:  Lock was supposed to be set to {0}, but actually was {1}", lockType, lockObj.Type);
            Assert.AreEqual(lockComment, lockObj.Comment, "A30A:  Lock Comment is incorrect");
            lockObj = await rl.LockInfo(lockCategory, testID_5);

            Assert.AreEqual(lockType, lockObj.Type, "A31:  Lock was supposed to be set to {0}, but actually was {1}", lockType, lockObj.Type);
            Assert.AreEqual(lockComment, lockObj.Comment, "A31A:  Lock Comment is incorrect");
            lockObj = await rl.LockInfo(lockCategory, testID_6);

            Assert.AreEqual(lockType, lockObj.Type, "A32:  Lock was supposed to be set to {0}, but actually was {1}", lockType, lockObj.Type);
            Assert.AreEqual(lockComment, lockObj.Comment, "A32A:  Lock Comment is incorrect");

            // Validate all default ttl locks are expired - indicating they were using the default ttl
            Thread.Sleep(rl.TTL);
            Assert.IsFalse(await rl.Exists(lockCategory, testID_1), "A50:  TestID_1 lock did not expire on time!");
            Assert.IsFalse(await rl.Exists(lockCategory, testID_4), "A51:   TestID_4 lock did not expire on time!");

            // Give the locks a chance to expire
            Thread.Sleep(ttl2);
            Assert.IsFalse(await rl.Exists(lockCategory, testID_5), "A52:  TestID_5 lock did not expire on time!");
            Assert.IsFalse(await rl.Exists(lockCategory, testID_2), "A53:  TestID_2 lock did not expire on time!");
            Assert.IsFalse(await rl.Exists(lockCategory, testID_3), "A54:  TestID_3 lock did not expire on time!");
            Assert.IsFalse(await rl.Exists(lockCategory, testID_6), "A55:  TestID_6 Lock did not expire on time!");
        }