示例#1
0
        public void ShouldReturnIdsAcrossMultipleGenerators()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store1     = BuildStore(testScope);
                var generator1 = new UniqueIdGenerator(store1)
                {
                    BatchSize = 3
                };
                var store2     = BuildStore(testScope);
                var generator2 = new UniqueIdGenerator(store2)
                {
                    BatchSize = 3
                };

                // Act
                var generatedIds = new[]
                {
                    generator1.NextId(testScope.IdScopeName), //1
                    generator1.NextId(testScope.IdScopeName), //2
                    generator1.NextId(testScope.IdScopeName), //3
                    generator2.NextId(testScope.IdScopeName), //4
                    generator1.NextId(testScope.IdScopeName), //7
                    generator2.NextId(testScope.IdScopeName), //5
                    generator2.NextId(testScope.IdScopeName), //6
                    generator2.NextId(testScope.IdScopeName), //10
                    generator1.NextId(testScope.IdScopeName), //8
                    generator1.NextId(testScope.IdScopeName)  //9
                };

                // Assert
                Assert.Equal(new long[] { 1, 2, 3, 4, 7, 5, 6, 10, 8, 9 }, generatedIds);
            }
        }
示例#2
0
        public void ShouldReturnIdsFromThirdBatchIfSecondBatchTakenByAnotherGenerator()
        {
            // Arrange
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            using (var testScope = new TestScope(account))
            {
                var store1     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator1 = new UniqueIdGenerator(store1)
                {
                    BatchSize = 3
                };
                var store2     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator2 = new UniqueIdGenerator(store2)
                {
                    BatchSize = 3
                };

                // Act
                generator1.NextId(testScope.IdScopeName);              //1
                generator1.NextId(testScope.IdScopeName);              //2
                generator1.NextId(testScope.IdScopeName);              //3
                generator2.NextId(testScope.IdScopeName);              //4
                var lastId = generator1.NextId(testScope.IdScopeName); //7

                // Assert
                Assert.AreEqual(7, lastId);
            }
        }
示例#3
0
        public void ShouldReturnIdsFromThirdBatchIfSecondBatchTakenByAnotherGenerator()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store1     = BuildStore(testScope);
                var generator1 = new UniqueIdGenerator(store1)
                {
                    BatchSize = 3
                };
                var store2     = BuildStore(testScope);
                var generator2 = new UniqueIdGenerator(store2)
                {
                    BatchSize = 3
                };

                // Act
                generator1.NextId(testScope.IdScopeName);              //1
                generator1.NextId(testScope.IdScopeName);              //2
                generator1.NextId(testScope.IdScopeName);              //3
                generator2.NextId(testScope.IdScopeName);              //4
                var lastId = generator1.NextId(testScope.IdScopeName); //7

                // Assert
                Assert.AreEqual(7, lastId);
            }
        }
        public void NextIdShouldReturnNumbersSequentially()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetNextBatch("test", 3).Returns(1, 251);

            var subject = new UniqueIdGenerator(store) { BatchSize = 3 };

            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
            Assert.AreEqual(3, subject.NextId("test"));
        }
        public void NextIdShouldReturnNumbersSequentially()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetData("test").Returns("0", "250");
            store.TryOptimisticWrite("test", "3").Returns(true);

            var subject = new UniqueIdGenerator(store)
            {
                BatchSize = 3
            };

            Assert.AreEqual(0, subject.NextId("test"));
            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
        }
示例#6
0
        public void NextIdShouldReturnNumbersSequentially()
        {
            var store = new Mock <IOptimisticDataStore>();

            store.SetupSequence(s => s.GetData("test")).Returns("0").Returns("250");
            store.Setup(s => s.TryOptimisticWrite("test", "3")).Returns(true);

            var subject = new UniqueIdGenerator(store.Object)
            {
                BatchSize = 3
            };

            Assert.Equal(0, subject.NextId("test"));
            Assert.Equal(1, subject.NextId("test"));
            Assert.Equal(2, subject.NextId("test"));
        }
示例#7
0
        public void NextIdShouldReturnNumbersSequentially()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns("0", "250");
            store.TryOptimisticWrite("test", "3").Returns(true);

            var subject = new UniqueIdGenerator(store)
            {
                BatchSize = 3
            };

            Assert.AreEqual(0, subject.NextId("test"));
            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
        }
        public long GetNextIdForUser()
        {
            string scopeName = "usersIds";
            long   id        = _generatorForUsers.NextId(scopeName);

            return(id);
        }
        public long GetNextIdForMessage()
        {
            string scopeName = "messagesIds";
            long   id        = _generatorForMessages.NextId(scopeName);

            return(id);
        }
        public void NextIdShouldThrowExceptionOnNullData()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetData("test").Returns((string)null);

            var generator = new UniqueIdGenerator(store);

            generator.NextId("test");
        }
示例#11
0
        public void NextIdShouldThrowExceptionOnNullData()
        {
            var store = new Mock <IOptimisticDataStore>();

            store.Setup(s => s.GetData("test")).Returns(default(string));

            var generator = new UniqueIdGenerator(store.Object);

            Assert.Throws <UniqueIdGenerationException>(() => generator.NextId("test"));
        }
示例#12
0
        public void NextIdShouldThrowExceptionOnNullData()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns((string)null);

            var generator = new UniqueIdGenerator(store);

            generator.NextId("test");
        }
示例#13
0
        public void ShouldNotUpdateBlobAtEndOfBatch()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store     = BuildStore(testScope);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 3
                };

                // Act
                generator.NextId(testScope.IdScopeName); //1
                generator.NextId(testScope.IdScopeName); //2
                generator.NextId(testScope.IdScopeName); //3

                // Assert
                Assert.AreEqual("4", testScope.ReadCurrentPersistedValue());
            }
        }
示例#14
0
        public void NextIdShouldThrowExceptionOnCorruptData()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns("abc");
            Assert.Throws <UniqueIdGenerationException>(() =>
            {
                var generator = new UniqueIdGenerator(store);

                generator.NextId("test");
            });
        }
示例#15
0
        public void ShouldReturnIdsAcrossMultipleGenerators()
        {
            // Arrange
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            using (var testScope = new TestScope(account))
            {
                var store1     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator1 = new UniqueIdGenerator(store1)
                {
                    BatchSize = 3
                };
                var store2     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator2 = new UniqueIdGenerator(store2)
                {
                    BatchSize = 3
                };

                // Act
                var generatedIds = new[]
                {
                    generator1.NextId(testScope.IdScopeName), //1
                    generator1.NextId(testScope.IdScopeName), //2
                    generator1.NextId(testScope.IdScopeName), //3
                    generator2.NextId(testScope.IdScopeName), //4
                    generator1.NextId(testScope.IdScopeName), //7
                    generator2.NextId(testScope.IdScopeName), //5
                    generator2.NextId(testScope.IdScopeName), //6
                    generator2.NextId(testScope.IdScopeName), //10
                    generator1.NextId(testScope.IdScopeName), //8
                    generator1.NextId(testScope.IdScopeName)  //9
                };

                // Assert
                CollectionAssert.AreEqual(
                    new[] { 1, 2, 3, 4, 7, 5, 6, 10, 8, 9 },
                    generatedIds);
            }
        }
示例#16
0
        public void NextIdShouldThrowExceptionOnNullData()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns((string)null);

            Assert.That(() =>
            {
                var generator = new UniqueIdGenerator(store);
                generator.NextId("test");
            }
                        , Throws.TypeOf <UniqueIdGenerationException>());
        }
示例#17
0
        public void ShouldNotUpdateBlobAtEndOfBatch()
        {
            // Arrange
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            using (var testScope = new TestScope(account))
            {
                var store     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 3
                };

                // Act
                generator.NextId(testScope.IdScopeName); //1
                generator.NextId(testScope.IdScopeName); //2
                generator.NextId(testScope.IdScopeName); //3

                // Assert
                Assert.AreEqual("4", testScope.ReadCurrentBlobValue());
            }
        }
    public static long GetUniqueId()
    {
        if (_instance != null)
        {
            return(_instance.NextId("identificador"));
        }

        var connectionString = "DefaultEndpointsProtocol=https;AccountName=hefesoftautentication;AccountKey=G943HAZgCsBqPwANE8tfKWaPq0FDM68gaUH4fCQz+W1NTGOBswZvQka1SFnquoYv+xrcPQQew7LQFcelJHycxw==";
        var storageAccount   = CloudStorageAccount.Parse(connectionString);
        IOptimisticDataStore blobDataStore = new BlobOptimisticDataStore(storageAccount, "Consecutivos");

        _instance = new UniqueIdGenerator(blobDataStore);
        return(_instance.NextId("identificador"));
    }
示例#19
0
        public void NextIdShouldThrowExceptionWhenRetriesAreExhausted()
        {
            var store = new Mock <IOptimisticDataStore>();

            store.Setup(s => s.GetData("test")).Returns("0");
            store.SetupSequence(s => s.TryOptimisticWrite("test", "3")).Returns(false).Returns(false).Returns(false).Returns(true);

            var generator = new UniqueIdGenerator(store.Object)
            {
                MaxWriteAttempts = 3
            };

            var exception = Assert.Throws <UniqueIdGenerationException>(() => generator.NextId("test"));

            Assert.StartsWith("Failed to update the data store after 3 attempts.", exception.Message);
        }
示例#20
0
        public void ShouldInitializeBlobForFirstIdInNewScope()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store     = BuildStore(testScope);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 3
                };

                // Act
                generator.NextId(testScope.IdScopeName); //1

                // Assert
                Assert.AreEqual("4", testScope.ReadCurrentPersistedValue());
            }
        }
示例#21
0
        public void ShouldReturnOneForFirstIdInNewScope()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store     = BuildStore(testScope);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 3
                };

                // Act
                var generatedId = generator.NextId(testScope.IdScopeName);

                // Assert
                Assert.AreEqual(1, generatedId);
            }
        }
        public void NextIdShouldThrowExceptionWhenRetriesAreExhausted()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetNextBatch("test", 100).Returns(-1, -1, -1, 1);

            var generator = new UniqueIdGenerator(store) { MaxWriteAttempts = 3 };

            try
            {
                generator.NextId("test");
            }
            catch (Exception ex)
            {
                StringAssert.StartsWith("Failed to update the data store after 3 attempts.", ex.Message);
                return;
            }
            Assert.Fail("NextId should have thrown and been caught in the try block");
        }
示例#23
0
        public void ShouldSupportUsingOneGeneratorFromMultipleThreads()
        {
            // Arrange
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            using (var testScope = new TestScope(account))
            {
                var store     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 1000
                };
                const int testLength = 10000;

                // Act
                var generatedIds = new ConcurrentQueue <long>();
                var threadIds    = new ConcurrentQueue <int>();
                var scopeName    = testScope.IdScopeName;
                Parallel.For(
                    0,
                    testLength,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                    i =>
                {
                    generatedIds.Enqueue(generator.NextId(scopeName));
                    threadIds.Enqueue(Thread.CurrentThread.ManagedThreadId);
                });

                // Assert we generated the right count of ids
                Assert.AreEqual(testLength, generatedIds.Count);

                // Assert there were no duplicates
                Assert.IsFalse(generatedIds.GroupBy(n => n).Where(g => g.Count() != 1).Any());

                // Assert we used multiple threads
                var uniqueThreadsUsed = threadIds.Distinct().Count();
                if (uniqueThreadsUsed == 1)
                {
                    Assert.Inconclusive("The test failed to actually utilize multiple threads");
                }
            }
        }
        public void NextIdShouldRollOverToNewBlockWhenCurrentBlockIsExhausted()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetNextBatch("test", 3).Returns(1, 251);

            var subject = new UniqueIdGenerator(store) { BatchSize = 3 };

            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
            Assert.AreEqual(3, subject.NextId("test"));
            Assert.AreEqual(251, subject.NextId("test"));
            Assert.AreEqual(252, subject.NextId("test"));
            Assert.AreEqual(253, subject.NextId("test"));
        }
示例#25
0
        public void ShouldSupportUsingOneGeneratorFromMultipleThreads()
        {
            // Arrange
            using (var testScope = BuildTestScope())
            {
                var store     = BuildStore(testScope);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 1000
                };
                const int testLength = 10000;

                // Act
                var generatedIds = new ConcurrentQueue <long>();
                var threadIds    = new ConcurrentQueue <int>();
                var scopeName    = testScope.IdScopeName;
                Parallel.For(
                    0,
                    testLength,
                    new ParallelOptions {
                    MaxDegreeOfParallelism = 10
                },
                    i =>
                {
                    generatedIds.Enqueue(generator.NextId(scopeName));
                    threadIds.Enqueue(Thread.CurrentThread.ManagedThreadId);
                });

                // Assert we generated the right count of ids
                Assert.Equal(testLength, generatedIds.Count);

                // Assert there were no duplicates
                Assert.DoesNotContain(generatedIds.GroupBy(n => n), g => g.Count() != 1);

                // Assert we used multiple threads
                var uniqueThreadsUsed = threadIds.Distinct().Count();
                if (uniqueThreadsUsed == 1)
                {
                    Assert.False(true, "The test failed to actually utilize multiple threads");
                }
            }
        }
示例#26
0
        public void ShouldReturnOneForFirstIdInNewScope()
        {
            // Arrange
            var account = CloudStorageAccount.DevelopmentStorageAccount;

            using (var testScope = new TestScope(account))
            {
                var store     = new BlobOptimisticDataStore(account, testScope.ContainerName);
                var generator = new UniqueIdGenerator(store)
                {
                    BatchSize = 3
                };

                // Act
                var generatedId = generator.NextId(testScope.IdScopeName);

                // Assert
                Assert.AreEqual(1, generatedId);
            }
        }
示例#27
0
        public void NextIdShouldThrowExceptionWhenRetriesAreExhausted()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns("0");
            store.TryOptimisticWrite("test", "3").Returns(false, false, false, true);

            var generator = new UniqueIdGenerator(store)
            {
                MaxWriteAttempts = 3
            };

            try
            {
                generator.NextId("test");
            }
            catch (Exception ex)
            {
                StringAssert.StartsWith("Failed to update the data store after 3 attempts.", ex.Message);
                return;
            }
            Assert.Fail("NextId should have thrown and been caught in the try block");
        }
示例#28
0
        public void NextIdShouldRollOverToNewBlockWhenCurrentBlockIsExhausted()
        {
            var store = Substitute.For<IOptimisticDataStore>();
            store.GetData("test").Returns("0", "250");
            store.TryOptimisticWrite("test", "3").Returns(true);
            store.TryOptimisticWrite("test", "253").Returns(true);

            var subject = new UniqueIdGenerator(store)
            {
                BatchSize = 3
            };

            Assert.AreEqual(0, subject.NextId("test"));
            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
            Assert.AreEqual(250, subject.NextId("test"));
            Assert.AreEqual(251, subject.NextId("test"));
            Assert.AreEqual(252, subject.NextId("test"));
        }
示例#29
0
        public void NextIdShouldRollOverToNewBlockWhenCurrentBlockIsExhausted()
        {
            var store = new Mock <IOptimisticDataStore>();

            store.SetupSequence(s => s.GetData("test")).Returns("0").Returns("250");
            store.Setup(s => s.TryOptimisticWrite("test", "3")).Returns(true);
            store.Setup(s => s.TryOptimisticWrite("test", "253")).Returns(true);

            var subject = new UniqueIdGenerator(store.Object)
            {
                BatchSize = 3
            };

            Assert.Equal(0, subject.NextId("test"));
            Assert.Equal(1, subject.NextId("test"));
            Assert.Equal(2, subject.NextId("test"));
            Assert.Equal(250, subject.NextId("test"));
            Assert.Equal(251, subject.NextId("test"));
            Assert.Equal(252, subject.NextId("test"));
        }
示例#30
0
        public void NextIdShouldRollOverToNewBlockWhenCurrentBlockIsExhausted()
        {
            var store = Substitute.For <IOptimisticDataStore>();

            store.GetData("test").Returns("0", "250");
            store.TryOptimisticWrite("test", "3").Returns(true);
            store.TryOptimisticWrite("test", "253").Returns(true);

            var subject = new UniqueIdGenerator(store)
            {
                BatchSize = 3
            };

            Assert.AreEqual(0, subject.NextId("test"));
            Assert.AreEqual(1, subject.NextId("test"));
            Assert.AreEqual(2, subject.NextId("test"));
            Assert.AreEqual(250, subject.NextId("test"));
            Assert.AreEqual(251, subject.NextId("test"));
            Assert.AreEqual(252, subject.NextId("test"));
        }
示例#31
0
        public void UniqueIdGeneratorWithMongoDBClientConstructorDataSource()
        {
            MongoClientSettings settings = new MongoClientSettings();
            settings.Server = new MongoServerAddress("localhost", 27017);

            UniqueIdGenerator generator = new UniqueIdGenerator(new MongoOptimisticDataStore(settings, "SnowMaker", "IntegrationTests"));
            string blockName = Guid.NewGuid().ToString();

            generator.BatchSize = 5;

            Assert.AreEqual(1, generator.NextId(blockName));
            Assert.AreEqual(2, generator.NextId(blockName));
            Assert.AreEqual(3, generator.NextId(blockName));
            Assert.AreEqual(4, generator.NextId(blockName));
            Assert.AreEqual(5, generator.NextId(blockName));
            Assert.AreEqual(6, generator.NextId(blockName));
            Assert.AreEqual(7, generator.NextId(blockName));
        }
示例#32
0
        public void UniqueIdGeneratorWithMongoDBDataSource()
        {
            UniqueIdGenerator generator = new UniqueIdGenerator(new MongoOptimisticDataStore());
            string blockName = Guid.NewGuid().ToString();

            generator.BatchSize = 5;

            Assert.AreEqual(1, generator.NextId(blockName));
            Assert.AreEqual(2, generator.NextId(blockName));
            Assert.AreEqual(3, generator.NextId(blockName));
            Assert.AreEqual(4, generator.NextId(blockName));
            Assert.AreEqual(5, generator.NextId(blockName));
            Assert.AreEqual(6, generator.NextId(blockName));
            Assert.AreEqual(7, generator.NextId(blockName));
        }
示例#33
0
        public void UniqueIdGeneratorWithMongoDBDataSourceMany()
        {
            UniqueIdGenerator gen1 = new UniqueIdGenerator(new MongoOptimisticDataStore());
            string blockName = Guid.NewGuid().ToString();

            gen1.BatchSize = 5;

            Assert.AreEqual(1, gen1.NextId(blockName));
            Assert.AreEqual(2, gen1.NextId(blockName));
            Assert.AreEqual(3, gen1.NextId(blockName));

            UniqueIdGenerator gen2 = new UniqueIdGenerator(new MongoOptimisticDataStore());
            gen2.BatchSize = 5;

            Assert.AreEqual(6, gen2.NextId(blockName));
            Assert.AreEqual(7, gen2.NextId(blockName));
            Assert.AreEqual(8, gen2.NextId(blockName));
            Assert.AreEqual(9, gen2.NextId(blockName));

            UniqueIdGenerator gen3 = new UniqueIdGenerator(new MongoOptimisticDataStore());
            gen3.BatchSize = 50;
            Assert.AreEqual(11, gen3.NextId(blockName));

            UniqueIdGenerator gen4 = new UniqueIdGenerator(new MongoOptimisticDataStore());
            gen4.BatchSize = 50;
            Assert.AreEqual(61, gen4.NextId(blockName));
        }
示例#34
0
        public uint Generate(string scope)
        {
            long nextId = generator.NextId(scope);

            return((uint)nextId);
        }