|
| 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.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | +using System.Reflection; |
| 14 | +using NHibernate.Cache; |
| 15 | +using NHibernate.Cfg; |
| 16 | +using NHibernate.Impl; |
| 17 | +using NHibernate.Linq; |
| 18 | +using NHibernate.Test.SecondLevelCacheTests; |
| 19 | +using NSubstitute; |
| 20 | +using NUnit.Framework; |
| 21 | + |
| 22 | +namespace NHibernate.Test.SecondLevelCacheTest |
| 23 | +{ |
| 24 | + using System.Threading.Tasks; |
| 25 | + using System.Threading; |
| 26 | + [TestFixture] |
| 27 | + public class NeverCachedEntityTestsAsync : TestCase |
| 28 | + { |
| 29 | + protected override string CacheConcurrencyStrategy => null; |
| 30 | + protected override string MappingsAssembly => "NHibernate.Test"; |
| 31 | + |
| 32 | + protected override string[] Mappings => new[] { "SecondLevelCacheTest.Item.hbm.xml" }; |
| 33 | + |
| 34 | + protected override void Configure(Configuration configuration) |
| 35 | + { |
| 36 | + configuration.SetProperty(Environment.CacheProvider, typeof(HashtableCacheProvider).AssemblyQualifiedName); |
| 37 | + configuration.SetProperty(Environment.UseQueryCache, "true"); |
| 38 | + } |
| 39 | + |
| 40 | + [Test] |
| 41 | + public async Task NeverInvalidateEntitiesAsync() |
| 42 | + { |
| 43 | + var debugSessionFactory = (DebugSessionFactory) Sfi; |
| 44 | + |
| 45 | + var cache = Substitute.For<UpdateTimestampsCache>(Sfi.Settings, new Dictionary<string, string>()); |
| 46 | + |
| 47 | + var updateTimestampsCacheField = typeof(SessionFactoryImpl).GetField( |
| 48 | + "updateTimestampsCache", |
| 49 | + BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); |
| 50 | + |
| 51 | + updateTimestampsCacheField.SetValue(debugSessionFactory.ActualFactory, cache); |
| 52 | + |
| 53 | + //"Received" assertions can not be used since the collection is reused and cleared between calls. |
| 54 | + //The received args are cloned and stored |
| 55 | + var preInvalidations = new List<IReadOnlyCollection<string>>(); |
| 56 | + var invalidations = new List<IReadOnlyCollection<string>>(); |
| 57 | + |
| 58 | + await (cache.PreInvalidateAsync(Arg.Do<IReadOnlyCollection<string>>(x => preInvalidations.Add(x.ToList())), CancellationToken.None)); |
| 59 | + await (cache.InvalidateAsync(Arg.Do<IReadOnlyCollection<string>>(x => invalidations.Add(x.ToList())), CancellationToken.None)); |
| 60 | + |
| 61 | + using (var session = OpenSession()) |
| 62 | + { |
| 63 | + List<int> ids = new List<int>(); |
| 64 | + //Add NeverItem |
| 65 | + using (var tx = session.BeginTransaction()) |
| 66 | + { |
| 67 | + foreach (var i in Enumerable.Range(1, 10)) |
| 68 | + { |
| 69 | + var item = new NeverItem { Name = "Abatay" }; |
| 70 | + item.Childrens.Add(new NeverChildItem() |
| 71 | + { |
| 72 | + Name = "Child", |
| 73 | + Parent = item |
| 74 | + }); |
| 75 | + await (session.SaveAsync(item)); |
| 76 | + ids.Add(item.Id); |
| 77 | + } |
| 78 | + |
| 79 | + await (tx.CommitAsync()); |
| 80 | + } |
| 81 | + |
| 82 | + //Update NeverItem |
| 83 | + using (var tx = session.BeginTransaction()) |
| 84 | + { |
| 85 | + foreach (var i in ids) |
| 86 | + { |
| 87 | + var item = await (session.GetAsync<NeverItem>(i)); |
| 88 | + item.Name = item.Id.ToString(); |
| 89 | + } |
| 90 | + |
| 91 | + await (tx.CommitAsync()); |
| 92 | + } |
| 93 | + |
| 94 | + //Delete NeverItem |
| 95 | + using (var tx = session.BeginTransaction()) |
| 96 | + { |
| 97 | + foreach (var i in ids) |
| 98 | + { |
| 99 | + var item = await (session.GetAsync<NeverItem>(i)); |
| 100 | + await (session.DeleteAsync(item)); |
| 101 | + } |
| 102 | + |
| 103 | + await (tx.CommitAsync()); |
| 104 | + } |
| 105 | + |
| 106 | + //Update NeverItem using HQL |
| 107 | + using (var tx = session.BeginTransaction()) |
| 108 | + { |
| 109 | + await (session.CreateQuery("UPDATE NeverItem SET Name='Test'").ExecuteUpdateAsync()); |
| 110 | + |
| 111 | + await (tx.CommitAsync()); |
| 112 | + } |
| 113 | + |
| 114 | + //Update NeverItem using LINQ |
| 115 | + using (var tx = session.BeginTransaction()) |
| 116 | + { |
| 117 | + await (session.Query<NeverItem>() |
| 118 | + .UpdateBuilder() |
| 119 | + .Set(x => x.Name, "Test") |
| 120 | + .UpdateAsync()); |
| 121 | + |
| 122 | + await (tx.CommitAsync()); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + //Should receive none preinvalidation when Cache is configured as never |
| 127 | + Assert.That(preInvalidations, Has.Count.EqualTo(0)); |
| 128 | + |
| 129 | + //Should receive none invalidation when Cache is configured as never |
| 130 | + Assert.That(invalidations, Has.Count.EqualTo(0)); |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public async Task QueryCache_ThrowsExceptionAsync() |
| 135 | + { |
| 136 | + using (var session = OpenSession()) |
| 137 | + { |
| 138 | + //Linq |
| 139 | + using (var tx = session.BeginTransaction()) |
| 140 | + { |
| 141 | + Assert.ThrowsAsync<QueryException>(() => session |
| 142 | + .Query<NeverItem>().WithOptions(x => x.SetCacheable(true)).ToListAsync()); |
| 143 | + |
| 144 | + await (tx.CommitAsync()); |
| 145 | + } |
| 146 | + |
| 147 | + //Linq Multiple with error message we will quarantied that gets 2 class in error message |
| 148 | + using (var tx = session.BeginTransaction()) |
| 149 | + { |
| 150 | + Assert.ThrowsAsync<QueryException>(() => session |
| 151 | + .Query<NeverItem>().Where(x => x.Childrens.Any()) |
| 152 | + .WithOptions(x => x.SetCacheable(true)) |
| 153 | + .ToListAsync(), |
| 154 | + $"Never cached entity:{string.Join(", ", typeof(NeverItem).FullName, typeof(NeverChildItem).FullName)} cannot be used in cacheable query"); |
| 155 | + |
| 156 | + await (tx.CommitAsync()); |
| 157 | + } |
| 158 | + |
| 159 | + //Hql |
| 160 | + using (var tx = session.BeginTransaction()) |
| 161 | + { |
| 162 | + Assert.ThrowsAsync<QueryException>(() => session |
| 163 | + .CreateQuery("from NeverItem").SetCacheable(true).ListAsync<NeverItem>()); |
| 164 | + |
| 165 | + await (tx.CommitAsync()); |
| 166 | + } |
| 167 | + |
| 168 | + //ICriteria |
| 169 | + using (var tx = session.BeginTransaction()) |
| 170 | + { |
| 171 | + Assert.ThrowsAsync<QueryException>(() => session |
| 172 | + .CreateCriteria<NeverItem>() |
| 173 | + .SetCacheable(true) |
| 174 | + .ListAsync<NeverItem>()); |
| 175 | + |
| 176 | + await (tx.CommitAsync()); |
| 177 | + } |
| 178 | + |
| 179 | + //Native Sql |
| 180 | + using (var tx = session.BeginTransaction()) |
| 181 | + { |
| 182 | + Assert.ThrowsAsync<QueryException>(() => session |
| 183 | + .CreateSQLQuery("select * from NeverItem") |
| 184 | + .AddSynchronizedQuerySpace("NeverItem") |
| 185 | + .SetCacheable(true) |
| 186 | + .ListAsync<NeverItem>()); |
| 187 | + |
| 188 | + await (tx.CommitAsync()); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + [Test] |
| 194 | + public async Task ShouldAutoFlushAsync() |
| 195 | + { |
| 196 | + using (var session = OpenSession()) |
| 197 | + using (session.BeginTransaction()) |
| 198 | + { |
| 199 | + var e1 = new NeverItem { Name = "Abatay" }; |
| 200 | + e1.Childrens.Add(new NeverChildItem() |
| 201 | + { |
| 202 | + Name = "Child", |
| 203 | + Parent = e1 |
| 204 | + }); |
| 205 | + await (session.SaveAsync(e1)); |
| 206 | + |
| 207 | + var result = await ((from e in session.Query<NeverItem>() |
| 208 | + where e.Name == "Abatay" |
| 209 | + select e).ToListAsync()); |
| 210 | + |
| 211 | + Assert.That(result.Count, Is.EqualTo(1)); |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + protected override void OnTearDown() |
| 216 | + { |
| 217 | + using (var s = OpenSession()) |
| 218 | + using (var tx = s.BeginTransaction()) |
| 219 | + { |
| 220 | + s.Delete("from NeverItem"); |
| 221 | + tx.Commit(); |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +} |
0 commit comments