public void CacheManagerExtensions_SetIfNotExists() { var cm = new Mock <ICacheManager>(); var key = "key"; var value = new CacheTestObject(); var cacheTime = 123U; //object is cached cm.Setup(c => c.Get <CacheTestObject>(It.IsAny <string>())).Returns(null as CacheTestObject); CacheManagerExtensions.SetIfNotExists(cm.Object, key, value, cacheTime); cm.Verify(c => c.Set( It.Is <string>(k => k == key), It.Is <CacheTestObject>(cto => cto == value), It.IsAny <uint>()), Times.Once); //object is not cached cm.Reset(); cm.Setup(c => c.Get <CacheTestObject>(It.IsAny <string>())).Returns(new CacheTestObject()); CacheManagerExtensions.SetIfNotExists(cm.Object, key, value, cacheTime); cm.Verify(c => c.Set(It.IsAny <string>(), It.IsAny <CacheTestObject>(), It.IsAny <uint>()), Times.Never); }
public async Task ToCachedCollection_Async_EmptyKeyResolvesQuery(string ck) { var a = new[] { "a", "b", "c" }; var q = Task.FromResult(a.AsQueryable()); var cm = new Mock <ICacheManager>(); var res = await CacheManagerExtensions.ToCachedCollection(cm.Object, q, ck, TimeSpan.FromDays(1)); res.ShouldBe(a); }
public async Task ToCachedEnumerable_Synced_EmptyKeyResolvesQuery(string ck) { var a = new[] { "a", "b", "c" }; var q = a.AsQueryable(); var cm = new Mock <ICacheManager>(); var res = await CacheManagerExtensions.ToCachedEnumerable(cm.Object, q, ck, TimeSpan.FromDays(1)); res.ShouldBe(a); }
public void CacheManagerExtensions_Set() { var cm = new Mock <ICacheManager>(); var key = "key"; var value = new CacheTestObject(); CacheManagerExtensions.Set(cm.Object, key, value); cm.Verify(c => c.Set( It.Is <string>(k => k == key), It.Is <CacheTestObject>(cto => cto == value), It.Is <uint>(i => i == CacheManagerExtensions.DefaultCacheTime)), Times.Once); }
public async Task ToCachedEnumerable_Synced_GetsFromCache() { var ck = "cKey"; var a = new[] { "a", "b", "c" }; var cm = new Mock <ICacheManager>(); cm.Setup(c => c.Get(It.IsAny <string>(), It.IsAny <Func <Task <IEnumerable <string> > > >(), It.IsAny <TimeSpan>())) .ReturnsAsync(a); var q = a.AsQueryable(); var res = await CacheManagerExtensions.ToCachedEnumerable(cm.Object, q, ck, TimeSpan.FromDays(1)); res.ShouldBe(a); }
public async Task ToCachedCollection_Async_GetsFromCache() { var ck = "cKey"; var a = new List <string> { "a", "b", "c" }; var cm = new Mock <ICacheManager>(); cm.Setup(c => c.Get(It.IsAny <string>(), It.IsAny <Func <Task <ICollection <string> > > >(), It.IsAny <TimeSpan>())) .ReturnsAsync(a); var q = Task.FromResult(a.AsQueryable()); var res = await CacheManagerExtensions.ToCachedCollection(cm.Object, q, ck, TimeSpan.FromDays(1)); res.ShouldBe(a); }
public void CacheManagerExtensions_GetOrAquire() { var cm = new Mock <ICacheManager>(); var key = "key"; var cacheTime = 123U; var wasAquired = false; Func <CacheTestObject> aquireFunc = () => { wasAquired = true; return(new CacheTestObject()); }; //does not call aquire func cm.Setup(c => c.Get <CacheTestObject>(It.IsAny <string>())).Returns(new CacheTestObject()); CacheManagerExtensions.Get(cm.Object, key, aquireFunc, cacheTime); wasAquired.ShouldBeFalse(); //should call call aquire func cm.Setup(c => c.Get <CacheTestObject>(It.IsAny <string>())).Returns(null as CacheTestObject); CacheManagerExtensions.Get(cm.Object, key, aquireFunc, cacheTime); wasAquired.ShouldBeTrue(); }
/// <summary> /// Seed database (applies migrations, runs bootstrappers/seeders) /// </summary> private void SeedDatabase() { var ioc = StaticContext.IocManager; var lockFactory = ioc.Resolve <ILockFactory>(); var cacheManager = ioc.Resolve <ICacheManager>(); // Note: the application may work in the multiple instance environment (e.g. on Azure) // We are trying to acquire a lock and initialize the application, possible cases: // 1. lock successfully acquired - initialize the application // 2. initialization is locked by another process - wait for completion and check the result of initialization. If the initialization failed - we can't continue the process and just throw exception Logger.Warn("Try to initialize database..."); var expiry = TimeSpan.FromSeconds(30); var wait = TimeSpan.FromSeconds(10); var retry = TimeSpan.FromSeconds(1); const string seedDbKey = "AppStart:SeedDb"; const string seedDbFinishedOnKey = "SeedDbFinishedOn"; var cache = CacheManagerExtensions.GetCache <string, DateTime>(cacheManager, seedDbKey); var initializationStart = DateTime.Now.ToUniversalTime(); var initializedByCurrentInstance = false; lockFactory.DoExclusive(seedDbKey, expiry, wait, retry, () => { // get last initialization time var seedDbTime = cache.Get(seedDbFinishedOnKey, key => DateTime.MinValue); if (seedDbTime != DateTime.MinValue && ( seedDbTime.Ticks > initializationStart.Ticks || /* DB was initialized while we were waiting for the lock */ (initializationStart - seedDbTime).TotalSeconds <= 30 /* DB was initialized less than 30 seconds ago */ )) { Logger.Warn("Database initialized by another application instance"); return; } initializedByCurrentInstance = true; Logger.Warn("Database initialization started"); Logger.Warn("Apply migrations..."); var dbMigrator = ioc.Resolve <IAbpZeroDbMigrator>(); dbMigrator?.CreateOrMigrateForHost(); Logger.Warn("Apply migrations - finished"); // find all seeders/bootstrappers and run them var bootstrapperTypes = _typeFinder.Find(t => typeof(IBootstrapper).IsAssignableFrom(t) && t.IsClass).ToList(); bootstrapperTypes = SortByDependencies(bootstrapperTypes); foreach (var bootstrapperType in bootstrapperTypes) { AsyncHelper.RunSync(async() => { if (ioc.Resolve(bootstrapperType) is IBootstrapper bootstrapper) { Logger.Warn($"Run bootstrapper: {bootstrapperType.Name}..."); var uowManager = ioc.Resolve <IUnitOfWorkManager>(); using (var unitOfWork = uowManager.Begin()) { await bootstrapper.Process(); await unitOfWork.CompleteAsync(); } Logger.Warn($"Run bootstrapper: {bootstrapperType.Name} - finished"); } }); } // update the DB seeding time cache.Set(seedDbFinishedOnKey, initializationStart, TimeSpan.FromMinutes(10)); }); Logger.Warn(initializedByCurrentInstance ? "Database initialization finished" : "Database initialization skipped (locked by another instance)"); //SeedHelper.SeedHostDb(IocManager); }