|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.Collections; |
| 13 | +using System.Linq; |
| 14 | +using log4net; |
| 15 | +using NHibernate.AdoNet; |
| 16 | +using NHibernate.Cfg; |
| 17 | +using NHibernate.Dialect; |
| 18 | +using NUnit.Framework; |
| 19 | +using Environment = NHibernate.Cfg.Environment; |
| 20 | +using NHibernate.Linq; |
| 21 | + |
| 22 | +namespace NHibernate.Test.Ado |
| 23 | +{ |
| 24 | + using System.Threading.Tasks; |
| 25 | + using System.Threading; |
| 26 | + [TestFixture] |
| 27 | + public class GenericBatchingBatcherFixtureAsync : TestCase |
| 28 | + { |
| 29 | + protected override string MappingsAssembly => "NHibernate.Test"; |
| 30 | + |
| 31 | + protected override IList Mappings => new[] {"Ado.VerySimple.hbm.xml"}; |
| 32 | + |
| 33 | + protected override void Configure(Configuration configuration) |
| 34 | + { |
| 35 | + configuration.SetProperty(Environment.BatchStrategy, typeof(GenericBatchingBatcherFactory).AssemblyQualifiedName); |
| 36 | + configuration.SetProperty(Environment.GenerateStatistics, "true"); |
| 37 | + configuration.SetProperty(Environment.BatchSize, "1000"); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public async Task MassiveInsertUpdateDeleteTestAsync() |
| 42 | + { |
| 43 | + var totalRecords = 1000; |
| 44 | + await (BatchInsertAsync(totalRecords)); |
| 45 | + await (BatchUpdateAsync(totalRecords)); |
| 46 | + await (BatchDeleteAsync(totalRecords)); |
| 47 | + |
| 48 | + await (DbShoudBeEmptyAsync()); |
| 49 | + } |
| 50 | + |
| 51 | + [Test] |
| 52 | + public async Task BatchSizeTestAsync() |
| 53 | + { |
| 54 | + using (var sqlLog = new SqlLogSpy()) |
| 55 | + using (var s = Sfi.OpenSession()) |
| 56 | + using (var tx = s.BeginTransaction()) |
| 57 | + { |
| 58 | + s.SetBatchSize(5); |
| 59 | + for (var i = 0; i < 20; i++) |
| 60 | + { |
| 61 | + await (s.SaveAsync(new VerySimple { Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i })); |
| 62 | + } |
| 63 | + await (tx.CommitAsync()); |
| 64 | + |
| 65 | + var log = sqlLog.GetWholeLog(); |
| 66 | + Assert.That(4, Is.EqualTo(FindAllOccurrences(log, "Batch commands:"))); |
| 67 | + } |
| 68 | + await (CleanupAsync()); |
| 69 | + } |
| 70 | + |
| 71 | + private async Task BatchInsertAsync(int totalRecords, CancellationToken cancellationToken = default(CancellationToken)) |
| 72 | + { |
| 73 | + Sfi.Statistics.Clear(); |
| 74 | + using (var s = Sfi.OpenSession()) |
| 75 | + using (var tx = s.BeginTransaction()) |
| 76 | + { |
| 77 | + for (var i = 0; i < totalRecords; i++) |
| 78 | + { |
| 79 | + await (s.SaveAsync(new VerySimple {Id = 1 + i, Name = $"Fabio{i}", Weight = 1.45 + i}, cancellationToken)); |
| 80 | + } |
| 81 | + await (tx.CommitAsync(cancellationToken)); |
| 82 | + } |
| 83 | + Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1)); |
| 84 | + } |
| 85 | + |
| 86 | + public async Task BatchUpdateAsync(int totalRecords, CancellationToken cancellationToken = default(CancellationToken)) |
| 87 | + { |
| 88 | + using (var s = Sfi.OpenSession()) |
| 89 | + using (var tx = s.BeginTransaction()) |
| 90 | + { |
| 91 | + var items = await (s.Query<VerySimple>().ToListAsync(cancellationToken)); |
| 92 | + Assert.That(items.Count, Is.EqualTo(totalRecords)); |
| 93 | + |
| 94 | + foreach (var item in items) |
| 95 | + { |
| 96 | + item.Weight += 5; |
| 97 | + await (s.UpdateAsync(item, cancellationToken)); |
| 98 | + } |
| 99 | + |
| 100 | + Sfi.Statistics.Clear(); |
| 101 | + await (tx.CommitAsync(cancellationToken)); |
| 102 | + } |
| 103 | + Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1)); |
| 104 | + } |
| 105 | + |
| 106 | + public async Task BatchDeleteAsync(int totalRecords, CancellationToken cancellationToken = default(CancellationToken)) |
| 107 | + { |
| 108 | + using (var s = Sfi.OpenSession()) |
| 109 | + using (var tx = s.BeginTransaction()) |
| 110 | + { |
| 111 | + var items = await (s.Query<VerySimple>().ToListAsync(cancellationToken)); |
| 112 | + Assert.That(items.Count, Is.EqualTo(totalRecords)); |
| 113 | + |
| 114 | + foreach (var item in items) |
| 115 | + { |
| 116 | + await (s.DeleteAsync(item, cancellationToken)); |
| 117 | + } |
| 118 | + |
| 119 | + Sfi.Statistics.Clear(); |
| 120 | + await (tx.CommitAsync(cancellationToken)); |
| 121 | + } |
| 122 | + Assert.That(Sfi.Statistics.PrepareStatementCount, Is.EqualTo(1)); |
| 123 | + } |
| 124 | + |
| 125 | + private async Task DbShoudBeEmptyAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 126 | + { |
| 127 | + using (var s = Sfi.OpenSession()) |
| 128 | + using (var tx = s.BeginTransaction()) |
| 129 | + { |
| 130 | + var items = await (s.Query<VerySimple>().ToListAsync(cancellationToken)); |
| 131 | + Assert.That(items.Count, Is.EqualTo(0)); |
| 132 | + |
| 133 | + await (tx.CommitAsync(cancellationToken)); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private async Task CleanupAsync(CancellationToken cancellationToken = default(CancellationToken)) |
| 138 | + { |
| 139 | + using (var s = Sfi.OpenSession()) |
| 140 | + using (s.BeginTransaction()) |
| 141 | + { |
| 142 | + await (s.CreateQuery("delete from VerySimple").ExecuteUpdateAsync(cancellationToken)); |
| 143 | + await (s.Transaction.CommitAsync(cancellationToken)); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private int FindAllOccurrences(string source, string substring) |
| 148 | + { |
| 149 | + if (source == null) |
| 150 | + { |
| 151 | + return 0; |
| 152 | + } |
| 153 | + int n = 0, count = 0; |
| 154 | + while ((n = source.IndexOf(substring, n, StringComparison.InvariantCulture)) != -1) |
| 155 | + { |
| 156 | + n += substring.Length; |
| 157 | + ++count; |
| 158 | + } |
| 159 | + return count; |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments