Пример #1
0
        public void TestCache()
        {
            var testsAssembly = Assembly.Load(new AssemblyName("Estellaris.Tests"));
            var connection    = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var factory = DependenciesProvider.Create().AddScopedFromAssemblies(new [] { testsAssembly })
                          .AddCache(dbOpts => dbOpts.UseSqlite(connection))
                          .Build();

            var dbContext    = factory.GetService <CacheDbContext>();
            var dbCache      = factory.GetService <IDistributedCache>();
            var cacheOptions = new DistributedCacheEntryOptions()
                               .SetAbsoluteExpiration(TimeSpan.FromSeconds(5));

            dbContext.Database.EnsureCreated();

            // test get/set and expiration
            var cacheStr  = "My cached value.";
            var cachedStr = "";

            dbCache.SetString("testEntry", cacheStr, cacheOptions);
            cachedStr = dbCache.GetString("testEntry");
            Assert.IsFalse(string.IsNullOrWhiteSpace(cachedStr));
            Assert.AreEqual(cacheStr, cachedStr);
            Thread.Sleep(TimeSpan.FromSeconds(6));
            cachedStr = dbCache.GetString("testEntry");
            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedStr));
            Assert.AreNotEqual(cacheStr, cachedStr);

            // test async
            Task.Factory.StartNew(async() => {
                var asyncCacheStr  = "My async cached value.";
                var asyncCachedStr = "";
                await dbCache.SetStringAsync("asyncEntry", asyncCacheStr, cacheOptions);
                asyncCachedStr = await dbCache.GetStringAsync("asyncEntry");
                Assert.IsFalse(string.IsNullOrWhiteSpace(asyncCachedStr));
                Assert.AreEqual(asyncCacheStr, asyncCachedStr);
            }).Wait();

            // test multi-threaded
            var tasks = new Collection <Task>();

            for (var i = 0; i < 100; i++)
            {
                tasks.Add(Task.Factory.StartNew(async() => {
                    var key = DateTime.Now.Ticks.ToString();
                    await dbCache.SetAsync(key, key.ToBytes());
                    await dbCache.GetAsync(key);
                }));
            }
            Task.WaitAll(tasks.ToArray());

            connection.Close();
            factory.Dispose();
        }
Пример #2
0
        public void InitContainer()
        {
            var testsAssembly = Assembly.Load(new AssemblyName("Estellaris.Tests"));

            _factory = DependenciesProvider.Create().AddScopedFromAssemblies(new [] { testsAssembly })
                       .AddTransient <ITransient, ClassTransient>()
                       .AddSingleton <ISingleton, ClassSingleton>()
                       .AddSingleton <InjectedClass>()
                       .AddConfig <MyOptions>("MyOptions")
                       .Build();
        }