コード例 #1
0
        private ISet <long> CollectGeneratedIds(ReplicatedIdGenerator idGenerator, long expectedIds)
        {
            ISet <long> idsGenerated = new HashSet <long>();

            long nextId;

            for (int i = 0; i < expectedIds; i++)
            {
                nextId = idGenerator.NextId();
                assertThat(nextId, greaterThanOrEqualTo(0L));
                idsGenerated.Add(nextId);
            }

            try
            {
                idGenerator.NextId();
                fail("Too many ids produced, expected " + expectedIds);
            }
            catch (NoMoreIds)
            {
                // rock and roll!
            }

            return(idsGenerated);
        }
コード例 #2
0
        private void ConsecutiveAllocationFromSeparateIdGeneratorsForSameIdTypeShouldNotDuplicateForGivenInitialHighId(long initialHighId)
        {
            ISet <long> idAllocations = new HashSet <long>();
            int         idRangeLength = 8;

            FileSystemAbstraction fs = DefaultFileSystemRule.get();
            File generatorFile1      = TestDirectory.file("gen1");
            File generatorFile2      = TestDirectory.file("gen2");

            using (ReplicatedIdGenerator generatorOne = CreateForMemberWithInitialIdAndRangeLength(_memberA, initialHighId, idRangeLength, fs, generatorFile1), ReplicatedIdGenerator generatorTwo = CreateForMemberWithInitialIdAndRangeLength(_memberB, initialHighId, idRangeLength, fs, generatorFile2), )
            {
                // First iteration is bootstrapping the set, so we do it outside the loop to avoid an if check in there
                long newId = generatorOne.NextId();
                idAllocations.Add(newId);

                for (int i = 1; i < idRangeLength - initialHighId; i++)
                {
                    newId = generatorOne.NextId();
                    bool wasNew = idAllocations.Add(newId);
                    assertTrue("Id " + newId + " has already been returned", wasNew);
                    assertTrue("Detected gap in id generation, missing " + (newId - 1), idAllocations.Contains(newId - 1));
                }

                for (int i = 0; i < idRangeLength; i++)
                {
                    newId = generatorTwo.NextId();
                    bool wasNew = idAllocations.Add(newId);
                    assertTrue("Id " + newId + " has already been returned", wasNew);
                    assertTrue("Detected gap in id generation, missing " + (newId - 1), idAllocations.Contains(newId - 1));
                }
            }
        }