Exemple #1
0
        public async Task Generates_sequential_values <TValue>(bool async)
        {
            const int blockSize = 4;

            var sequence = new Model().MySql().GetOrAddSequence("Foo");

            sequence.IncrementBy = blockSize;
            var state = new MySqlSequenceValueGeneratorState(sequence);

            var generator = new MySqlSequenceHiLoValueGenerator <TValue>(
                new FakeRawSqlCommandBuilder(blockSize),
                new MySqlUpdateSqlGenerator(
                    new UpdateSqlGeneratorDependencies(
                        new MySqlSqlGenerationHelper(
                            new RelationalSqlGenerationHelperDependencies()),
                        new MySqlTypeMappingSource(
                            TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                            TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>()))),
                state,
                CreateConnection());

            for (var i = 1; i <= 27; i++)
            {
                var value = async
                    ? await generator.NextAsync(null)
                    : generator.Next(null);

                Assert.Equal(i, (int)Convert.ChangeType(value, typeof(int), CultureInfo.InvariantCulture));
            }
        }
Exemple #2
0
        private async Task <IEnumerable <List <long> > > GenerateValuesInMultipleThreads(int threadCount, int valueCount)
        {
            const int blockSize = 10;

            var serviceProvider = MySqlTestHelpers.Instance.CreateServiceProvider();

            var sequence = ((IMutableModel) new Model()).AddSequence("Foo");

            sequence.IncrementBy = blockSize;
            var state = new MySqlSequenceValueGeneratorState(sequence);

            var executor     = new FakeRawSqlCommandBuilder(blockSize);
            var sqlGenerator = new MySqlUpdateSqlGenerator(
                new UpdateSqlGeneratorDependencies(
                    new MySqlSqlGenerationHelper(
                        new RelationalSqlGenerationHelperDependencies()),
                    new MySqlTypeMappingSource(
                        TestServiceFactory.Instance.Create <TypeMappingSourceDependencies>(),
                        TestServiceFactory.Instance.Create <RelationalTypeMappingSourceDependencies>())));

            var logger = new FakeDiagnosticsLogger <DbLoggerCategory.Database.Command>();

            var tests           = new Func <Task> [threadCount];
            var generatedValues = new List <long> [threadCount];

            for (var i = 0; i < tests.Length; i++)
            {
                var testNumber = i;
                generatedValues[testNumber] = new List <long>();
                tests[testNumber]           = async() =>
                {
                    for (var j = 0; j < valueCount; j++)
                    {
                        var connection = CreateConnection(serviceProvider);
                        var generator  = new MySqlSequenceHiLoValueGenerator <long>(executor, sqlGenerator, state, connection, logger);

                        var value = j % 2 == 0
                            ? await generator.NextAsync(null)
                            : generator.Next(null);

                        generatedValues[testNumber].Add(value);
                    }
                };
            }

            var tasks = tests.Select(Task.Run).ToArray();

            foreach (var t in tasks)
            {
                await t;
            }

            return(generatedValues);
        }