Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public void ReadAll()
        {
            var idLarge = LargeBlobId();
            var id11    = RegularBlobId();
            var id12    = RegularBlobId();
            var shift   = Timestamp.Now.AddDays(1);
            var id21    = RegularBlobId(TimeGuid.NewGuid(shift));
            var id22    = RegularBlobId(TimeGuid.NewGuid(shift));

            WriteByte(idLarge, 255);
            WriteByte(id11, 11);
            WriteByte(id12, 12);
            WriteByte(id21, 21);
            WriteByte(id22, 22);

            timeBasedBlobStorage.ReadAll(1)
            .Select(x => Tuple.Create(x.Item1, x.Item2.Single()))
            .Should()
            .BeEquivalentTo(new[]
            {
                new Tuple <BlobId, byte>(idLarge, 255),
                new Tuple <BlobId, byte>(id11, 11),
                new Tuple <BlobId, byte>(id12, 12),
                new Tuple <BlobId, byte>(id21, 21),
                new Tuple <BlobId, byte>(id22, 22),
            });
        }
Exemplo n.º 3
0
        public void ToByteArray()
        {
            var timeGuid = TimeGuid.NowGuid();
            var bytes    = timeGuid.ToByteArray();

            Assert.That(new TimeGuid(bytes), Is.EqualTo(timeGuid));
        }
Exemplo n.º 4
0
        public void KeyIncrementer_Increments_Success()
        {
            string defaultPrefix = "ABC";

            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();
            string     key       = uniqueKey.GetKey(defaultPrefix);
            string     expKey    = defaultPrefix + uniqueKey.WhatIsKeySeparator + stGuid;

            Assert.AreEqual(
                expKey, key,
                "A10:  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 create 2 more keys.  They should have incrementers attched to them.
            string key2 = uniqueKey.GetKey(defaultPrefix);
            string key3 = uniqueKey.GetKey(defaultPrefix);

            Assert.AreNotEqual(key, key2, "A20:  Expected Key1 and Key2 to be different.");
            Assert.AreNotEqual(key2, key3, "A30:  Expected Key2 and Key3 to be different.");

            Assert.IsTrue(key2.EndsWith(uniqueKey.WhatIsIncrementSeparator + "1"), "A40:  Key2 should have ended with a 1.");
            Assert.IsTrue(key3.EndsWith(uniqueKey.WhatIsIncrementSeparator + "2"), "A50:  Key3 should have ended with a 2.");
        }
Exemplo n.º 5
0
        public void KeyValueSeparator_CanBeSet_Success()
        {
            string defaultPrefix = "ABC";
            string sep           = "^";

            KeepWithinSingleSecond();

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


            // Set separator to be carat.
            UniqueKeys uniqueKey = new UniqueKeys(sep);
            string     key       = uniqueKey.GetKey(defaultPrefix);
            string     expKey    = defaultPrefix + sep + stGuid.ToString;

            Assert.AreEqual(
                expKey, key,
                "A10:  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);
            TestContext.WriteLine("Unique Key Value:    {0}", key);
            TestContext.WriteLine("Expected Key Value:  {0}", expKey);
        }
Exemplo n.º 6
0
        public void CompareTo_ByNode()
        {
            var timestamp     = Timestamp.Now;
            var clockSequence = TimeGuidGenerator.GenerateRandomClockSequence();
            var timeGuid1     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 });
            var timeGuid2     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 });
            var timeGuid3     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x12, 0x34, 0x56, 0x78, 0xfe, 0xff });
            var timeGuid4     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x12, 0x34, 0x56, 0x78, 0xff, 0x00 });
            var timeGuid5     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x12, 0x34, 0x56, 0x78, 0xff, 0xff });
            var timeGuid6     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x12, 0x34, 0x56, 0x79, 0x00, 0x00 });
            var timeGuid7     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00 });
            var timeGuid8     = new TimeGuid(timestamp, clockSequence, new byte[] { 0x7f, 0x01, 0x00, 0x00, 0x00, 0x00 });
            var timeGuid9     = new TimeGuid(timestamp, clockSequence, new byte[] { 0xff, 0x00, 0x00, 0x00, 0x00, 0x00 });
            var timeGuid10    = new TimeGuid(timestamp, clockSequence, new byte[] { 0xff, 0x00, 0x12, 0x34, 0x00, 0x00 });
            var timeGuid11    = new TimeGuid(timestamp, clockSequence, new byte[] { 0xff, 0x00, 0x12, 0x35, 0x00, 0x00 });
            var timeGuid12    = new TimeGuid(timestamp, clockSequence, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0x01 });
            var timeGuid13    = new TimeGuid(timestamp, clockSequence, new byte[] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff });

            Assert.That(timeGuid1.CompareTo(timeGuid2), Is.Negative);
            Assert.That(timeGuid2.CompareTo(timeGuid3), Is.Negative);
            Assert.That(timeGuid3.CompareTo(timeGuid4), Is.Negative);
            Assert.That(timeGuid4.CompareTo(timeGuid5), Is.Negative);
            Assert.That(timeGuid5.CompareTo(timeGuid6), Is.Negative);
            Assert.That(timeGuid6.CompareTo(timeGuid7), Is.Negative);
            Assert.That(timeGuid7.CompareTo(timeGuid8), Is.Negative);
            Assert.That(timeGuid8.CompareTo(timeGuid9), Is.Negative);
            Assert.That(timeGuid9.CompareTo(timeGuid10), Is.Negative);
            Assert.That(timeGuid10.CompareTo(timeGuid11), Is.Negative);
            Assert.That(timeGuid11.CompareTo(timeGuid12), Is.Negative);
            Assert.That(timeGuid12.CompareTo(timeGuid13), Is.Negative);
        }
Exemplo n.º 7
0
        // Validates that the first key has a value of 1 and each subsequent call is increasing by 1.
        public void KeyNumberShouldIncrement_EachCall_Success()
        {
            string defaultPrefix = "Key";

            // 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();
            string     expKey    = defaultPrefix + stGuid + "1";

            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 generate another key:
            string key2    = uniqueKey.GetKey();
            string expKey2 = defaultPrefix + stGuid + "2";

            Assert.AreEqual(
                expKey2, key2,
                "A2:  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);
        }
Exemplo n.º 8
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);
        }
        public bool TryAddNewExceptionInfo([NotNull] TaskMetaInformation taskMeta, [NotNull] Exception exception, out List <TimeGuid> newExceptionInfoIds)
        {
            if (!taskMeta.IsTimeBased())
            {
                throw new InvalidOperationException(string.Format("TaskMeta is not time-based: {0}", taskMeta));
            }
            newExceptionInfoIds = null;
            var newExceptionInfo  = new TaskExceptionInfo(exception);
            var lastExceptionInfo = TryGetLastExceptionInfo(taskMeta);

            if (lastExceptionInfo != null && lastExceptionInfo.ExceptionMessageInfo == newExceptionInfo.ExceptionMessageInfo)
            {
                return(false);
            }
            var      newExceptionInfoId = TimeGuid.NowGuid();
            var      timestamp          = newExceptionInfoId.GetTimestamp().Ticks;
            TimeGuid oldExceptionInfoId;

            newExceptionInfoIds = taskMeta.AddExceptionInfoId(newExceptionInfoId, out oldExceptionInfoId);
            var newExceptionInfoBytes = serializer.Serialize(newExceptionInfo);

            timeBasedBlobStorage.Write(taskMeta.Id, newExceptionInfoId, newExceptionInfoBytes, timestamp, taskMeta.GetTtl());
            if (oldExceptionInfoId != null)
            {
                timeBasedBlobStorage.Delete(taskMeta.Id, oldExceptionInfoId, timestamp);
            }
            return(true);
        }
Exemplo n.º 10
0
        public void NowGuid()
        {
            var nowDateTime = DateTime.UtcNow;
            var nowGuid     = TimeGuid.NowGuid();

            Assert.That(TimeSpan.FromTicks(nowGuid.GetTimestamp().Ticks - nowDateTime.Ticks), Is.LessThan(TimeSpan.FromTicks(100 * 10_000)));
            Assert.That(TimeSpan.FromMilliseconds((nowGuid.GetTimestamp().ToDateTime() - nowDateTime).TotalMilliseconds), Is.LessThan(TimeSpan.FromMilliseconds(100)));
        }
Exemplo n.º 11
0
 public void InvalidV1Guid()
 {
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(Guid.NewGuid()));
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(minGuid));
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(maxGuid));
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(new byte[0]));
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(TimeGuid.NowGuid().ToByteArray().Take(15).ToArray()));
     Assert.Throws <InvalidOperationException>(() => new TimeGuid(TimeGuid.NowGuid().ToByteArray().Concat(new byte[] { 0xff }).ToArray()));
 }
Exemplo n.º 12
0
        public void MaxForTimestamp()
        {
            var timestamp           = Timestamp.Now;
            var maxGuidForTimestamp = TimeGuid.MaxForTimestamp(timestamp);

            Assert.That(maxGuidForTimestamp.GetTimestamp(), Is.EqualTo(timestamp));
            Assert.That(maxGuidForTimestamp.GetClockSequence(), Is.EqualTo(TimeGuidBitsLayout.MaxClockSequence));
            Assert.That(maxGuidForTimestamp.GetNode(), Is.EqualTo(TimeGuidBitsLayout.MaxNode));
        }
Exemplo n.º 13
0
        public void SetUp()
        {
            var now = Timestamp.Now;

            g1          = TimeGuid.NewGuid(now);
            g2          = TimeGuid.NewGuid(now);
            g11         = new TimeGuid(g1.ToByteArray());
            g1PlusDelta = TimeGuid.NewGuid(now + TimeSpan.FromSeconds(1));
        }
Exemplo n.º 14
0
        public void Equals_2TimeGuids_SameTime_EqualEachOther()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today);

            Assert.True(x.Equals(y), "A10:  The two values were the same and should have equaled each other");
        }
Exemplo n.º 15
0
 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
 {
     if (reader.TokenType == JsonToken.String)
     {
         var readAsString = (string)reader.Value;
         return(TimeGuid.Parse(readAsString));
     }
     return(null);
 }
Exemplo n.º 16
0
        public void CompareTo_FirstLessThanSecond()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today.AddMinutes(2));

            Assert.Less(x.CompareTo(y), 0, "A10:  First value should have indicated it was less than second value.");
        }
Exemplo n.º 17
0
        public void Equals_2TimeGuids_FirstMoreThanSecond_NotEqual()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today.AddMinutes(3));
            TimeGuid y = new TimeGuid(today);

            Assert.False(x.Equals(y), "A10:  The two values were not the same and should NOT have equaled each other");
        }
Exemplo n.º 18
0
        public void ValidateMinuteSorting([Range(0, 58)] int minute)
        {
            DateTime d = new DateTime(2019, 01, 11, 0, minute, 5);

            TimeGuid x = new TimeGuid(d);
            TimeGuid y = new TimeGuid(d.AddMinutes(1));

            Assert.True(y > x, "Expected the comparison of y[" + y.ToString + "] > x[" + x.ToString + "] to be true.  It was not.  Something Wrong! ");
            TestContext.WriteLine("Comparing y[" + y.ToString + "] > x[" + x.ToString + "] was successful");
        }
Exemplo n.º 19
0
        public void ToGuid()
        {
            var timeGuid = TimeGuid.NowGuid();

            Console.Out.WriteLine(timeGuid);
            var guid = timeGuid.ToGuid();

            Assert.That(new TimeGuid(guid), Is.EqualTo(timeGuid));
            Assert.That(new TimeGuid(guid).ToGuid(), Is.EqualTo(guid));
        }
Exemplo n.º 20
0
        public void Before()
        {
            var timestamp = Timestamp.Now;

            Assert.That(TimeGuid.MaxForTimestamp(timestamp).Before(), Is.LessThan(TimeGuid.MaxForTimestamp(timestamp)));
            Assert.That(TimeGuid.MinForTimestamp(timestamp).Before(), Is.EqualTo(TimeGuid.MaxForTimestamp(timestamp - TimeSpan.FromTicks(1))));
            Assert.That(new TimeGuid(timestamp, 1, TimeGuidBitsLayout.MinNode).Before(), Is.EqualTo(new TimeGuid(timestamp, 0, TimeGuidBitsLayout.MaxNode)));
            Assert.That(new TimeGuid(timestamp, 1, TimeGuidBitsLayout.MaxNode).Before(), Is.EqualTo(new TimeGuid(timestamp, 1, TimeGuidBitsLayout.DecrementNode(TimeGuidBitsLayout.MaxNode))));
            Assert.Throws <InvalidOperationException>(() => TimeGuid.MinForTimestamp(new Timestamp(DateTime.MinValue)));
        }
Exemplo n.º 21
0
        public void CompareTo_LessThanSymbol()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today.AddMinutes(2));

            Assert.True(x < y, "A10:  x should have been less than y");
            Assert.False(y < x, "A20:  Comparison should have been false.");
        }
Exemplo n.º 22
0
        public void CompareTo_GreaterThanSymbol()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today.AddMinutes(-2));

            Assert.True(x > y, "A10:  x should have been greater than y");
            Assert.False(y > x, "A20:  Comparison should have been false.");
        }
Exemplo n.º 23
0
        public void TimeGuidWorks()
        {
            var nextSeq = TimeGuid.NextSeq;
            var target  = TimeGuid.New();
            var target2 = TimeGuid.New();
            var target3 = TimeGuid.New();

            Guid g1 = target;
            //target.Seq.ShouldBe();
        }
        private static TimeGuid GetTimeGuidFromRowKey([NotNull] string rowKey)
        {
            TimeGuid timeGuid;

            if (!TimeGuid.TryParse(rowKey, out timeGuid))
            {
                throw new InvalidOperationException(string.Format("Invalid rowKey: {0}", rowKey));
            }
            return(timeGuid);
        }
        private static TimeGuid GetTimeGuidFromColumnName([NotNull] string columnName)
        {
            TimeGuid timeGuid;

            if (!TimeGuid.TryParse(columnName.Split('_')[1], out timeGuid))
            {
                throw new InvalidOperationException(string.Format("Invalid regular column name: {0}", columnName));
            }
            return(timeGuid);
        }
        private static BlobId GetBlobId([NotNull] string taskId)
        {
            TimeGuid timeGuid;

            if (!TimeGuid.TryParse(taskId, out timeGuid))
            {
                throw new InvalidOperationException(string.Format("Task is not time-based: {0}", taskId));
            }
            return(new BlobId(timeGuid, BlobType.Regular));
        }
Exemplo n.º 27
0
        public void EqualitySigns_2TimeGuides_DifferentValues()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today.AddMinutes(2));

            Assert.True((x != y), "A10:  The two values were not the same and should have returned true");
            Assert.False((x == y), "A20:  The two values were different and should have returned false for == comparison");
        }
Exemplo n.º 28
0
        public void EqualitySigns_2TimeGuides_SameValue()
        {
            DateTime today = DateTime.Now;

            TimeGuid x = new TimeGuid(today);
            TimeGuid y = new TimeGuid(today);

            Assert.True((x == y), "A10:  The two values were the same and should have equaled each other");
            Assert.False((x != y), "A20:  The two values were the same and should have returned False for the != test");
        }
Exemplo n.º 29
0
        public void MinuteConversion_Works([Range(0, 59)] int minute)
        {
            string val = CalculateTimeGuidPositionalValue(minute, "m");

            DateTime d         = new DateTime(2000, 01, 01, 0, minute, 0);
            string   guidValue = TimeGuid.ConvertDateTimeToTimeGuid(d);
            string   guidChar  = guidValue.Substring(1, 1);

            Assert.AreEqual(val, guidChar, "A1:  The minute did not convert to the expected ascii character.  Exp: " + val + "  Actual: " + guidValue);
        }
Exemplo n.º 30
0
        public void SecondConversion_Works([Range(0, 59)] int second)
        {
            string val = CalculateTimeGuidPositionalValue(second, "s");

            DateTime d         = new DateTime(2000, 01, 01, 0, 0, second);
            string   guidValue = TimeGuid.ConvertDateTimeToTimeGuid(d);
            string   guidChar  = guidValue.Substring(2, 1);

            Assert.AreEqual(val, guidChar, "A1:  The second did not convert to the expected ascii character.  Exp: " + val + "  Actual: " + guidValue);
        }