示例#1
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testNextId()
        public virtual void TestNextId()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                for (long i = 0; i < 7; i++)
                {
                    assertEquals(i, idGenerator.NextId());
                }
                idGenerator.FreeId(1);
                idGenerator.FreeId(3);
                idGenerator.FreeId(5);
                assertEquals(7L, idGenerator.NextId());
                idGenerator.FreeId(6);
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 5, 1000, false, IdType.NODE, () => 0L);
                idGenerator.FreeId(2);
                idGenerator.FreeId(4);
                assertEquals(1L, idGenerator.NextId());
                idGenerator.FreeId(1);
                assertEquals(3L, idGenerator.NextId());
                idGenerator.FreeId(3);
                assertEquals(5L, idGenerator.NextId());
                idGenerator.FreeId(5);
                assertEquals(6L, idGenerator.NextId());
                idGenerator.FreeId(6);
                assertEquals(8L, idGenerator.NextId());
                idGenerator.FreeId(8);
                assertEquals(9L, idGenerator.NextId());
                idGenerator.FreeId(9);
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                assertEquals(6L, idGenerator.NextId());
                assertEquals(8L, idGenerator.NextId());
                assertEquals(9L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(3L, idGenerator.NextId());
                assertEquals(5L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                assertEquals(4L, idGenerator.NextId());
                assertEquals(10L, idGenerator.NextId());
                assertEquals(11L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#2
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testFreeId()
        public virtual void TestFreeId()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 3, 1000, false, IdType.NODE, () => 0L);
                for (long i = 0; i < 7; i++)
                {
                    assertEquals(i, idGenerator.NextId());
                }
                try
                {
                    idGenerator.FreeId(-1);
                    fail("Negative id should throw exception");
                }
                catch (System.ArgumentException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(7);
                    fail("Greater id than ever returned should throw exception");
                }
                catch (System.ArgumentException)
                {                         // good
                }
                for (int i = 0; i < 7; i++)
                {
                    idGenerator.FreeId(i);
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                assertEquals(5L, idGenerator.NextId());
                assertEquals(6L, idGenerator.NextId());
                assertEquals(3L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 30, 1000, false, IdType.NODE, () => 0L);

                assertEquals(0L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                assertEquals(4L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#3
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void makeSureMagicMinusOneCannotBeReturnedEvenIfFreed()
        public virtual void MakeSureMagicMinusOneCannotBeReturnedEvenIfFreed()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator idGenerator   = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, (new NodeRecordFormat()).MaxId, false, IdType.NODE, () => 0L);
            long        magicMinusOne = ( long )Math.Pow(2, 32) - 1;

            idGenerator.HighId = magicMinusOne;
            assertEquals(magicMinusOne + 1, idGenerator.NextId());
            idGenerator.FreeId(magicMinusOne - 1);
            idGenerator.FreeId(magicMinusOne);
            CloseIdGenerator(idGenerator);

            idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, (new NodeRecordFormat()).MaxId, false, IdType.NODE, () => 0L);
            assertEquals(magicMinusOne - 1, idGenerator.NextId());
            assertEquals(magicMinusOne + 2, idGenerator.NextId());
            CloseIdGenerator(idGenerator);
        }
示例#4
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testClose()
        public virtual void TestClose()
        {
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                CloseIdGenerator(idGenerator);
                try
                {
                    idGenerator.NextId();
                    fail("nextId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(0);
                    fail("freeId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2, 1000, false, IdType.NODE, () => 0L);
                assertEquals(0L, idGenerator.NextId());
                assertEquals(1L, idGenerator.NextId());
                assertEquals(2L, idGenerator.NextId());
                CloseIdGenerator(idGenerator);
                try
                {
                    idGenerator.NextId();
                    fail("nextId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
                try
                {
                    idGenerator.FreeId(0);
                    fail("freeId after close should throw exception");
                }
                catch (System.InvalidOperationException)
                {                         // good
                }
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#5
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void delete()
        public virtual void Delete()
        {
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGeneratorImpl idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 10, 1000, false, IdType.NODE, () => 0L);
            long            id          = idGenerator.NextId();

            idGenerator.NextId();
            idGenerator.FreeId(id);
            idGenerator.Dispose();
            idGenerator.Delete();
            assertFalse(IdGeneratorFile().exists());
            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 10, 1000, false, IdType.NODE, () => 0L);
            assertEquals(id, idGenerator.NextId());
            idGenerator.Dispose();
        }
示例#6
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testRandomTest()
        public virtual void TestRandomTest()
        {
            Random random   = new Random(DateTimeHelper.CurrentUnixTimeMillis());
            int    capacity = random.Next(1024) + 1024;
            int    grabSize = random.Next(128) + 128;

            IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
            IdGenerator  idGenerator    = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, capacity * 2, false, IdType.NODE, () => 0L);
            IList <long> idsTaken       = new List <long>();
            float        releaseIndex   = 0.25f;
            float        closeIndex     = 0.05f;
            int          currentIdCount = 0;

            try
            {
                while (currentIdCount < capacity)
                {
                    float rIndex = random.nextFloat();
                    if (rIndex < releaseIndex && currentIdCount > 0)
                    {
                        idGenerator.FreeId(idsTaken.Remove(random.Next(currentIdCount)).intValue());
                        currentIdCount--;
                    }
                    else
                    {
                        idsTaken.Add(idGenerator.NextId());
                        currentIdCount++;
                    }
                    if (rIndex > (1.0f - closeIndex) || rIndex < closeIndex)
                    {
                        CloseIdGenerator(idGenerator);
                        grabSize    = random.Next(128) + 128;
                        idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, capacity * 2, false, IdType.NODE, () => 0L);
                    }
                }
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#7
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testUnsignedId()
        public virtual void TestUnsignedId()
        {
            try
            {
                PropertyKeyTokenRecordFormat recordFormat = new PropertyKeyTokenRecordFormat();
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, recordFormat.MaxId, false, IdType.NODE, () => 0L);
                idGenerator.HighId = recordFormat.MaxId;
                long id = idGenerator.NextId();
                assertEquals(recordFormat.MaxId, id);
                idGenerator.FreeId(id);
                try
                {
                    idGenerator.NextId();
                    fail("Shouldn't be able to get next ID");
                }
                catch (StoreFailureException)
                {                         // good, capacity exceeded
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 1, recordFormat.MaxId, false, IdType.NODE, () => 0L);
                assertEquals(recordFormat.MaxId + 1, idGenerator.HighId);
                id = idGenerator.NextId();
                assertEquals(recordFormat.MaxId, id);
                try
                {
                    idGenerator.NextId();
                }
                catch (StoreFailureException)
                {                         // good, capacity exceeded
                }
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#8
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testChurnIdBatchAtGrabSize()
        public virtual void TestChurnIdBatchAtGrabSize()
        {
            IdGenerator idGenerator = null;

            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                const int grabSize = 10;
                int       rounds   = 10;
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), grabSize, 1000, true, IdType.NODE, () => 0L);

                for (int i = 0; i < rounds; i++)
                {
                    ISet <long> ids = new HashSet <long>();
                    for (int j = 0; j < grabSize; j++)
                    {
                        ids.Add(idGenerator.NextId());
                    }
                    foreach (long?id in ids)
                    {
                        idGenerator.FreeId(id.Value);
                    }
                }
                long newId = idGenerator.NextId();
                assertTrue("Expected IDs to be reused (" + grabSize + " at a time). high ID was: " + newId, newId < grabSize * rounds);
            }
            finally
            {
                if (idGenerator != null)
                {
                    CloseIdGenerator(idGenerator);
                }
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }
示例#9
0
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes:
//ORIGINAL LINE: @Test public void testOddAndEvenWorstCase()
        public virtual void TestOddAndEvenWorstCase()
        {
            int capacity = 1024 * 8 + 1;

            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 128, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i++)
                {
                    idGenerator.NextId();
                }
                IDictionary <long, object> freedIds = new Dictionary <long, object>();
                for (long i = 1; i < capacity; i += 2)
                {
                    idGenerator.FreeId(i);
                    freedIds[i] = this;
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2000, capacity * 2, false, IdType.NODE, () => 0L);
                long oldId = -1;
                for (int i = 0; i < capacity - 1; i += 2)
                {
                    long id = idGenerator.NextId();
                    if (freedIds.Remove(id) == null)
                    {
                        throw new Exception("Id=" + id + " prevId=" + oldId + " list.size()=" + freedIds.Count);
                    }
                    oldId = id;
                }
                assertEquals(0, freedIds.Values.Count);
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (_fs.fileExists(file))
                {
                    assertTrue(_fs.deleteFile(file));
                }
            }
            try
            {
                IdGeneratorImpl.createGenerator(_fs, IdGeneratorFile(), 0, false);
                IdGenerator idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 128, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i++)
                {
                    idGenerator.NextId();
                }
                IDictionary <long, object> freedIds = new Dictionary <long, object>();
                for (long i = 0; i < capacity; i += 2)
                {
                    idGenerator.FreeId(i);
                    freedIds[i] = this;
                }
                CloseIdGenerator(idGenerator);
                idGenerator = new IdGeneratorImpl(_fs, IdGeneratorFile(), 2000, capacity * 2, false, IdType.NODE, () => 0L);
                for (int i = 0; i < capacity; i += 2)
                {
                    assertEquals(this, freedIds.Remove(idGenerator.NextId()));
                }
                assertEquals(0, freedIds.Values.Count);
                CloseIdGenerator(idGenerator);
            }
            finally
            {
                File file = IdGeneratorFile();
                if (file.exists())
                {
                    assertTrue(file.delete());
                }
            }
        }