Example #1
0
        public void RefreshKeyWithUniquePrefix_Success()
        {
            string defaultPrefix = "unique";

            // We need to simulate the Timeguid logic to derive at the initial key value.
            TimeGuid stGuid = new TimeGuid(DateTime.Now);


            UniqueKeys uniqueKey = new UniqueKeys();
            string     key       = uniqueKey.GetKey(defaultPrefix);
            string     expKey    = defaultPrefix + uniqueKey.WhatIsKeySeparator + stGuid.ToString;

            Assert.AreEqual(
                expKey, key,
                "A1:  Expected initial key value was not correct. Note:  On Failures run a second time.  There is a very slight chance that due to a second rollover the values could be different.  Expected: " +
                expKey +
                "   Actual: " +
                key);


            // Now get a new key.  Note the TimeGuid portion should be updated and the increment should be updated.
            Thread.Sleep(1000);
            string   key2    = uniqueKey.RefreshKey(defaultPrefix);
            TimeGuid stGuid2 = new TimeGuid(DateTime.Now);
            string   expKey2 = defaultPrefix + uniqueKey.WhatIsKeySeparator + stGuid2;

            Assert.AreEqual(
                expKey2, key2, "A2: Expected key after Refresh was not equal to the value returned by UniqueKeys.  Exp: " + expKey2 + "   Actual:  " + key2);


            TestContext.WriteLine("Unique Key Value:    {0}", key2);
            TestContext.WriteLine("Expected Key Value:  {0}", expKey2);
        }
Example #2
0
        public void RefreshKeyWorks()
        {
            string defaultPrefix = "Key";

            KeepWithinSingleSecond();


            // We need to simulate the Timeguid logic to derive at the initial key value.
            TimeGuid stGuid = new TimeGuid(DateTime.Now);


            UniqueKeys uniqueKey = new UniqueKeys(constantTimeGuid: true);
            string     key       = uniqueKey.GetKey(defaultPrefix);
            string     expKey    = defaultPrefix + uniqueKey.WhatIsKeySeparator + stGuid + uniqueKey.WhatIsIncrementSeparator + "1";

            Assert.AreEqual(expKey, key, "A10:  Expected initial key value was not correct.   Expected: " + expKey + "   Actual: " + key);

            string key2 = uniqueKey.GetKey(defaultPrefix);

            // Now refresh the key.
            string key3 = uniqueKey.RefreshKey(defaultPrefix);

            // Because key3 requested RefreshKey, we should get a new TimeGuid as it would have made the thread sleep for at least 1 second.
            TimeGuid newGuid = new TimeGuid(DateTime.Now);


            Assert.IsTrue(key2.EndsWith(uniqueKey.WhatIsIncrementSeparator + "2"), "A20:  Key2 should have ended with a 2.");
            Assert.IsTrue(key3.EndsWith(newGuid.ToString), "A21:  Key3 should have ended with a 1 as it was called with the RefreshKey.");

            string expKey3 = defaultPrefix + uniqueKey.WhatIsKeySeparator + newGuid;

            Assert.AreEqual(
                expKey3, key3, "A30: Expected key after Refresh was not equal to the value returned by UniqueKeys.  Exp: " + expKey3 + "   Actual:  " + key3);
        }