public async Task Generates_sequential_values_async() { var configMock = new Mock <DbContextConfiguration>(); configMock.Setup(m => m.Connection).Returns(new Mock <RelationalConnection>().Object); var entryMock = new Mock <StateEntry>(); entryMock.Setup(m => m.Configuration).Returns(configMock.Object); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); for (var i = 0; i < 15; i++) { Assert.Equal((long)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(long)))); } for (var i = 15; i < 30; i++) { Assert.Equal(i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(int)))); } for (var i = 30; i < 45; i++) { Assert.Equal((short)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(short)))); } for (var i = 45; i < 60; i++) { Assert.Equal((byte)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(byte)))); } }
public async Task Generates_sequential_values_async() { var configMock = new Mock<DbContextConfiguration>(); configMock.Setup(m => m.Connection).Returns(new Mock<RelationalConnection>().Object); var entryMock = new Mock<StateEntry>(); entryMock.Setup(m => m.Configuration).Returns(configMock.Object); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); for (var i = 0; i < 15; i++) { Assert.Equal((long)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(long)))); } for (var i = 15; i < 30; i++) { Assert.Equal(i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(int)))); } for (var i = 30; i < 45; i++) { Assert.Equal((short)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(short)))); } for (var i = 45; i < 60; i++) { Assert.Equal((byte)i, await generator.NextAsync(entryMock.Object, CreateProperty(typeof(byte)))); } }
private IList <long>[] GenerateValuesInMultipleThreads(int poolSize, int threadCount, int valueCount) { const int blockSize = 10; var serviceProvider = SqlServerTestHelpers.Instance.CreateServiceProvider(); var state = new SqlServerSequenceValueGeneratorState("Foo", blockSize, poolSize); var executor = new FakeSqlStatementExecutor(blockSize); var sqlGenerator = new SqlServerUpdateSqlGenerator(); var tests = new Action[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] = () => { for (var j = 0; j < valueCount; j++) { var connection = CreateConnection(serviceProvider); var generator = new SqlServerSequenceValueGenerator <long>(executor, sqlGenerator, state, connection); generatedValues[testNumber].Add(generator.Next()); } }; } Parallel.Invoke(tests); return(generatedValues); }
public void Does_not_generate_temp_values() { var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); Assert.False(generator.GeneratesTemporaryValues); }
public async Task Generates_sequential_values_async() { var storeServices = CreateStoreServices(); var entityType = _model.GetEntityType(typeof(AnEntity)); var intProperty = entityType.GetProperty("Id"); var longProperty = entityType.GetProperty("Long"); var shortProperty = entityType.GetProperty("Short"); var byteProperty = entityType.GetProperty("Byte"); var nullableIntProperty = entityType.GetProperty("NullableId"); var nullableLongProperty = entityType.GetProperty("NullableLong"); var nullableShortProperty = entityType.GetProperty("NullableShort"); var nullableByteProperty = entityType.GetProperty("NullableByte"); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); for (var i = 0; i < 15; i++) { Assert.Equal(i, await generator.NextAsync(intProperty, storeServices)); } for (var i = 15; i < 30; i++) { Assert.Equal((long)i, await generator.NextAsync(longProperty, storeServices)); } for (var i = 30; i < 45; i++) { Assert.Equal((short)i, await generator.NextAsync(shortProperty, storeServices)); } for (var i = 45; i < 60; i++) { Assert.Equal((byte)i, await generator.NextAsync(byteProperty, storeServices)); } for (var i = 60; i < 75; i++) { Assert.Equal((int?)i, await generator.NextAsync(nullableIntProperty, storeServices)); } for (var i = 75; i < 90; i++) { Assert.Equal((long?)i, await generator.NextAsync(nullableLongProperty, storeServices)); } for (var i = 90; i < 105; i++) { Assert.Equal((short?)i, await generator.NextAsync(nullableShortProperty, storeServices)); } for (var i = 105; i < 120; i++) { Assert.Equal((byte?)i, await generator.NextAsync(nullableByteProperty, storeServices)); } }
public async Task Multiple_threads_can_use_the_same_generator_async() { var configMock = new Mock <DbContextConfiguration>(); configMock.Setup(m => m.Connection).Returns(new Mock <RelationalConnection>().Object); var entryMock = new Mock <StateEntry>(); entryMock.Setup(m => m.Configuration).Returns(configMock.Object); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); const int threadCount = 50; const int valueCount = 35; 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++) { generatedValues[testNumber].Add((long)await generator.NextAsync(entryMock.Object, CreateProperty(typeof(long)))); } }; } var tasks = tests.Select(Task.Run).ToArray(); foreach (var t in tasks) { await t; } // Check that each value was generated once and only once var checks = new bool[threadCount * valueCount]; foreach (var values in generatedValues) { Assert.Equal(valueCount, values.Count); foreach (var value in values) { checks[value] = true; } } Assert.True(checks.All(c => c)); }
public async Task Multiple_threads_can_use_the_same_generator_async() { var serviceProvider = TestHelpers.CreateServiceProvider(); var property = _model.GetEntityType(typeof(AnEntity)).GetProperty("Long"); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); const int threadCount = 50; const int valueCount = 35; 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 storeServices = CreateStoreServices(serviceProvider); var generatedValue = await generator.NextAsync(property, storeServices); generatedValues[testNumber].Add((long)generatedValue.Value); } }; } var tasks = tests.Select(Task.Run).ToArray(); foreach (var t in tasks) { await t; } // Check that each value was generated once and only once var checks = new bool[threadCount * valueCount]; foreach (var values in generatedValues) { Assert.Equal(valueCount, values.Count); foreach (var value in values) { checks[value] = true; } } Assert.True(checks.All(c => c)); }
public void Multiple_threads_can_use_the_same_generator() { var property = _model.GetEntityType(typeof(AnEntity)).GetProperty("Long"); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); const int threadCount = 50; const int valueCount = 35; var tests = new Action[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] = () => { var stateEntry = TestHelpers.CreateStateEntry <AnEntity>(_model); for (var j = 0; j < valueCount; j++) { generator.Next(stateEntry, property); generatedValues[testNumber].Add((long)stateEntry[property]); } }; } Parallel.Invoke(tests); // Check that each value was generated once and only once var checks = new bool[threadCount * valueCount]; foreach (var values in generatedValues) { Assert.Equal(valueCount, values.Count); foreach (var value in values) { checks[value] = true; } } Assert.True(checks.All(c => c)); }
public void Multiple_threads_can_use_the_same_generator() { var configMock = new Mock <DbContextConfiguration>(); configMock.Setup(m => m.Connection).Returns(new Mock <RelationalConnection>().Object); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); const int threadCount = 50; const int valueCount = 35; var tests = new Action[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] = () => { for (var j = 0; j < valueCount; j++) { generatedValues[testNumber].Add((long)generator.Next(configMock.Object, CreateProperty(typeof(long)))); } }; } Parallel.Invoke(tests); // Check that each value was generated once and only once var checks = new bool[threadCount * valueCount]; foreach (var values in generatedValues) { Assert.Equal(valueCount, values.Count); foreach (var value in values) { checks[value] = true; } } Assert.True(checks.All(c => c)); }
public async Task Generates_sequential_values_async() { var stateEntry = TestHelpers.CreateStateEntry <AnEntity>(_model); var intProperty = stateEntry.EntityType.GetProperty("Id"); var longProperty = stateEntry.EntityType.GetProperty("Long"); var shortProperty = stateEntry.EntityType.GetProperty("Short"); var byteProperty = stateEntry.EntityType.GetProperty("Byte"); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); for (var i = 0; i < 15; i++) { await generator.NextAsync(stateEntry, intProperty); Assert.Equal(i, stateEntry[intProperty]); Assert.False(stateEntry.HasTemporaryValue(intProperty)); } for (var i = 15; i < 30; i++) { await generator.NextAsync(stateEntry, longProperty); Assert.Equal((long)i, stateEntry[longProperty]); Assert.False(stateEntry.HasTemporaryValue(longProperty)); } for (var i = 30; i < 45; i++) { await generator.NextAsync(stateEntry, shortProperty); Assert.Equal((short)i, stateEntry[shortProperty]); Assert.False(stateEntry.HasTemporaryValue(shortProperty)); } for (var i = 45; i < 60; i++) { await generator.NextAsync(stateEntry, byteProperty); Assert.Equal((byte)i, stateEntry[byteProperty]); Assert.False(stateEntry.HasTemporaryValue(byteProperty)); } }
private IEnumerable <List <long> > GenerateValuesInMultipleThreads(int threadCount, int valueCount) { const int blockSize = 10; var serviceProvider = SqlServerTestHelpers.Instance.CreateServiceProvider(); var state = new SqlServerSequenceValueGeneratorState( new Sequence( new Model(), RelationalAnnotationNames.Prefix, "Foo") { IncrementBy = blockSize }); var executor = new FakeSqlStatementExecutor(blockSize); var sqlGenerator = new SqlServerUpdateSqlGenerator(new SqlServerSqlGenerator()); var tests = new Action[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] = () => { for (var j = 0; j < valueCount; j++) { var connection = CreateConnection(serviceProvider); var generator = new SqlServerSequenceHiLoValueGenerator <long>(executor, sqlGenerator, state, connection); generatedValues[testNumber].Add(generator.Next()); } }; } Parallel.Invoke(tests); return(generatedValues); }
public void Multiple_threads_can_use_the_same_generator() { var configMock = new Mock<DbContextConfiguration>(); configMock.Setup(m => m.Connection).Returns(new Mock<RelationalConnection>().Object); var entryMock = new Mock<StateEntry>(); entryMock.Setup(m => m.Configuration).Returns(configMock.Object); var executor = new FakeSqlStatementExecutor(10); var generator = new SqlServerSequenceValueGenerator(executor, "Foo", 10); const int threadCount = 50; const int valueCount = 35; var tests = new Action[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] = () => { for (var j = 0; j < valueCount; j++) { generatedValues[testNumber].Add((long)generator.Next(entryMock.Object, CreateProperty(typeof(long)))); } }; } Parallel.Invoke(tests); // Check that each value was generated once and only once var checks = new bool[threadCount * valueCount]; foreach (var values in generatedValues) { Assert.Equal(valueCount, values.Count); foreach (var value in values) { checks[value] = true; } } Assert.True(checks.All(c => c)); }