Ejemplo n.º 1
0
        public void It_should_generate_only_unique_ids_even_when_time_goes_backward()
        {
            const long sequenceMask = -1L ^ (-1L << 12);
            var        worker       = new StaticTimeWorker(0, 0);

            // reported at https://github.com/twitter/snowflake/issues/6
            // first we generate 2 ids with the same time, so that we get the sequqence to 1
            Assert.That(worker.Sequence, Is.EqualTo(0));
            Assert.That(worker.Time, Is.EqualTo(1));
            var id1 = worker.NextId();

            Assert.That(id1 >> 22, Is.EqualTo(1));
            Assert.That(id1 & sequenceMask, Is.EqualTo(0));

            Assert.That(worker.Sequence, Is.EqualTo(0));
            Assert.That(worker.Time, Is.EqualTo(1));
            var id2 = worker.NextId();

            Assert.That(id2 >> 22, Is.EqualTo(1));
            Assert.That(id2 & sequenceMask, Is.EqualTo(1));

            //then we set the time backwards

            worker.Time = 0;
            Assert.That(worker.Sequence, Is.EqualTo(1));
            Assert.Throws <InvalidSystemClock>(() => worker.NextId());
            Assert.That(worker.Sequence, Is.EqualTo(1));  // this used to get reset to 0, which would cause conflicts

            worker.Time = 1;
            var id3 = worker.NextId();

            Assert.That(id3 >> 22, Is.EqualTo(1));
            Assert.That(id3 & sequenceMask, Is.EqualTo(2));
        }
Ejemplo n.º 2
0
        public void It_should_generate_only_unique_ids_even_when_time_goes_backward()
        {
            const long sequenceMask = -1L ^ (-1L << 12);
            var worker = new StaticTimeWorker(0, 0);

            // reported at https://github.com/twitter/snowflake/issues/6
            // first we generate 2 ids with the same time, so that we get the sequqence to 1
            Assert.That(worker.Sequence, Is.EqualTo(0));
            Assert.That(worker.Time, Is.EqualTo(1));
            var id1 = worker.NextId();

            Assert.That(id1 >> 22, Is.EqualTo(1)); 
            Assert.That(id1 & sequenceMask, Is.EqualTo(0));

            Assert.That(worker.Sequence, Is.EqualTo(0)); 
            Assert.That(worker.Time, Is.EqualTo(1));
            var id2 = worker.NextId();

            Assert.That(id2 >> 22, Is.EqualTo(1)); 
            Assert.That(id2 & sequenceMask, Is.EqualTo(1));

            //then we set the time backwards

            worker.Time = 0;
            Assert.That(worker.Sequence, Is.EqualTo(1));
            Assert.Throws<InvalidSystemClock>(() => worker.NextId());
            Assert.That(worker.Sequence, Is.EqualTo(1));  // this used to get reset to 0, which would cause conflicts

            worker.Time = 1;
            var id3 = worker.NextId();
            Assert.That(id3 >> 22, Is.EqualTo(1)); 
            Assert.That(id3 & sequenceMask, Is.EqualTo(2));
        }