public void FirstCreateOfEntityInSessionFactoryAlsoIncrementsGeneratorId()
        {
            ResetDatabase();

            var person = new PersonWithBlockSizeDefault {FirstName = "Tim", LastName = "Howard"};

            var sessionFactory = new ConfigurationFactory().CreateConfiguration().BuildSessionFactory();
            using (var session = sessionFactory.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                session.Save(person);
                tx.Commit();
            }

            Assert.Equal(1, person.Id);
            Assert.Equal(1 + Int16.MaxValue, GetCurrentGeneratorValue(sessionFactory));
        }
        public void InterleavedEntitiesWithDifferentBlockSizes()
        {
            ResetDatabase();

            var sessionFactory = new ConfigurationFactory().CreateConfiguration().BuildSessionFactory();
            const long smallBlockSize = 2;
            const long defaultBlockSize = Int16.MaxValue;

            long expectedDatabaseGeneratorValue = 1;
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));

            // ** First save of entity with small block size

            var personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);

            // Generator should be incremented by the small block size
            long expectedSmallBlockSizeGeneratorValue = expectedDatabaseGeneratorValue;
            expectedDatabaseGeneratorValue += smallBlockSize;
            Assert.Equal(expectedSmallBlockSizeGeneratorValue, personWithSmallBlockSize.Id);
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));

            // ** First save of entity with default block size

            var personWithDefaultBlockSize = new PersonWithBlockSizeDefault { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithDefaultBlockSize);

            // This entity should be allocated a default block
            // Database Generator should be incremented by default block size
            long expectedDefaultBlockSizeGeneratorValue = expectedDatabaseGeneratorValue;
            expectedDatabaseGeneratorValue += defaultBlockSize;
            Assert.Equal(expectedDefaultBlockSizeGeneratorValue, personWithDefaultBlockSize.Id);
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));

            // ** Second save of entity with small block size

            personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Lionel", LastName = "Messi" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);

            // Database Generator should not be incremented
            expectedSmallBlockSizeGeneratorValue++;
            Assert.Equal(expectedSmallBlockSizeGeneratorValue, personWithSmallBlockSize.Id);
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));

            // ** Second save of entity with default block size

            personWithDefaultBlockSize = new PersonWithBlockSizeDefault { FirstName = "Tim", LastName = "Howard" };
            SavePersonInNewSession(sessionFactory, personWithDefaultBlockSize);

            // Database Generator should not be incremented
            expectedDefaultBlockSizeGeneratorValue++;
            Assert.Equal(expectedDefaultBlockSizeGeneratorValue, personWithDefaultBlockSize.Id);
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));

            // Third save of entity with small block size

            personWithSmallBlockSize = new PersonWithBlockSizeTwo { FirstName = "Wayne", LastName = "Rooney" };
            SavePersonInNewSession(sessionFactory, personWithSmallBlockSize);

            // Database Generator should be incremented again
            expectedSmallBlockSizeGeneratorValue = expectedDatabaseGeneratorValue;
            expectedDatabaseGeneratorValue += smallBlockSize;
            Assert.Equal(expectedSmallBlockSizeGeneratorValue, personWithSmallBlockSize.Id);
            Assert.Equal(expectedDatabaseGeneratorValue, GetCurrentGeneratorValue(sessionFactory));
        }