|
| 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.Data; |
| 13 | +using System.Data.SqlClient; |
| 14 | +using System.Globalization; |
| 15 | +using System.Linq; |
| 16 | +using System.Threading; |
| 17 | +using System.Threading.Tasks; |
| 18 | +using NHibernate.Linq; |
| 19 | +using NUnit.Framework; |
| 20 | + |
| 21 | +namespace NHibernate.Test.Stateless |
| 22 | +{ |
| 23 | + [TestFixture] |
| 24 | + public class StatelessSessionCancelQueryFixtureAsync : TestCase |
| 25 | + { |
| 26 | + protected override string MappingsAssembly |
| 27 | + { |
| 28 | + get { return "NHibernate.Test"; } |
| 29 | + } |
| 30 | + |
| 31 | + protected override string[] Mappings |
| 32 | + { |
| 33 | + get { return new[] { "Stateless.Document.hbm.xml" }; } |
| 34 | + } |
| 35 | + |
| 36 | + private const string _documentName = "SomeDocument"; |
| 37 | + private CultureInfo _backupCulture; |
| 38 | + private CultureInfo _backupUICulture; |
| 39 | + |
| 40 | + protected override void OnSetUp() |
| 41 | + { |
| 42 | + if (CultureInfo.CurrentCulture.TwoLetterISOLanguageName != CultureInfo.InvariantCulture.TwoLetterISOLanguageName) |
| 43 | + { |
| 44 | + // This test needs to run in English |
| 45 | + _backupCulture = CultureInfo.CurrentCulture; |
| 46 | + _backupUICulture = CultureInfo.CurrentUICulture; |
| 47 | + CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; |
| 48 | + CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture; |
| 49 | + } |
| 50 | + |
| 51 | + using (var s = Sfi.OpenStatelessSession()) |
| 52 | + using (var t = s.BeginTransaction()) |
| 53 | + { |
| 54 | + s.Insert(new Document("Some text", _documentName)); |
| 55 | + t.Commit(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + protected override void OnTearDown() |
| 60 | + { |
| 61 | + using (var s = Sfi.OpenStatelessSession()) |
| 62 | + using (var t = s.BeginTransaction()) |
| 63 | + { |
| 64 | + s.CreateQuery("delete Document").ExecuteUpdate(); |
| 65 | + t.Commit(); |
| 66 | + } |
| 67 | + |
| 68 | + if (_backupCulture != null) |
| 69 | + { |
| 70 | + CultureInfo.CurrentCulture = _backupCulture; |
| 71 | + CultureInfo.CurrentUICulture = _backupUICulture; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + protected override bool AppliesTo(Dialect.Dialect dialect) |
| 76 | + { |
| 77 | + return TestDialect.SupportsCancelQuery && |
| 78 | + TestDialect.SupportsSelectForUpdate; |
| 79 | + } |
| 80 | + |
| 81 | + private async Task CancelQueryTestAsync(Action<IStatelessSession> queryAction, CancellationToken cancellationToken = default(CancellationToken)) |
| 82 | + { |
| 83 | + using (var s1 = Sfi.OpenStatelessSession()) |
| 84 | + using (var t1 = s1.BeginTransaction()) |
| 85 | + { |
| 86 | + await (s1.GetAsync<Document>(_documentName, LockMode.Upgrade, cancellationToken)); |
| 87 | + |
| 88 | + using (var s2 = Sfi.OpenStatelessSession()) |
| 89 | + using (var t2 = s2.BeginTransaction()) |
| 90 | + { |
| 91 | + var queryTask = Task.Factory.StartNew(() => queryAction(s2)); |
| 92 | + |
| 93 | + await (Task.Delay(200, cancellationToken)); |
| 94 | + s2.CancelQuery(); |
| 95 | + Assert.That(() => queryTask, |
| 96 | + Throws.InnerException.TypeOf(typeof(OperationCanceledException)) |
| 97 | + .Or.InnerException.Message.Contains("cancel")); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + [Test] |
| 103 | + public async Task CancelHqlQueryAsync() |
| 104 | + { |
| 105 | + await (CancelQueryTestAsync(s => s.CreateQuery("from Document d").SetLockMode("d", LockMode.Upgrade).List<Document>())); |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task CancelLinqQueryAsync() |
| 110 | + { |
| 111 | + await (CancelQueryTestAsync(s => s.Query<Document>().WithLock(LockMode.Upgrade).ToList())); |
| 112 | + } |
| 113 | + |
| 114 | + [Test] |
| 115 | + public async Task CancelQueryOverQueryAsync() |
| 116 | + { |
| 117 | + await (CancelQueryTestAsync(s => s.QueryOver<Document>().Lock().Upgrade.List())); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments