コード例 #1
0
        public void GetServiceTest()
        {
            BaseTest.RegisterServiceCollection();
            BaseTest.RegisterServiceStart(true);

            var memcache = SenparcDI.GetService <IMemoryCache>();

            Assert.IsNotNull(memcache);
            Console.WriteLine($"memcache HashCode:{memcache.GetHashCode()}");

            var key = Guid.NewGuid().ToString();
            var dt  = SystemTime.Now;

            memcache.Set(key, dt);//直接使用缓存

            var memcache2 = SenparcDI.GetService <IMemoryCache>();

            Console.WriteLine($"memcache 2 HashCode:{memcache2.GetHashCode()}");

            Assert.AreEqual(memcache.GetHashCode(), memcache2.GetHashCode());//同一个全局对象

            var obj0 = memcache2.Get(key);

            Assert.IsNotNull(obj0);
            Assert.AreEqual(dt, obj0);

            //使用本地缓存测试
            CacheStrategyFactory.RegisterObjectCacheStrategy(() => LocalObjectCacheStrategy.Instance);
            var cache = CacheStrategyFactory.GetObjectCacheStrategyInstance();

            var obj1 = cache.Get(key, true);//使用缓存策略获取

            Assert.IsNotNull(obj1);
            Assert.AreEqual(dt, obj1);

            var obj2 = cache.Get <DateTimeOffset>(key, true);//获取明确类型对象

            Assert.IsNotNull(obj2);
            Assert.AreEqual(dt, obj2);

            Assert.AreEqual(obj0.GetHashCode(), obj2.GetHashCode());
        }
コード例 #2
0
        public void ThreadAndGlobalServiceTest()
        {
            BaseTest.RegisterServiceCollection();
            BaseTest.RegisterServiceStart(true);

            SenparcDI.GlobalServiceCollection.AddScoped <SenparcSetting>();
            SenparcDI.GlobalIServiceProvider = null;

            //测试跨线程唯一
            var s = SenparcDI.GetService <SenparcSetting>(true);

            Console.WriteLine($"s:{s.GetHashCode()}");

            var threadsCount = 10;

            Console.WriteLine("======= 开始全局唯一测试 =======");
            var finishedThread = 0;

            for (int i = 0; i < threadsCount; i++)
            {
                var index  = i;
                var thread = new Thread(() =>
                {
                    var s1 = SenparcDI.GetService <SenparcSetting>(true);
                    var s2 = SenparcDI.GetService <SenparcSetting>(true);
                    Console.WriteLine("ServiceProcider:" + SenparcDI.GlobalIServiceProvider?.GetHashCode());
                    Console.WriteLine($"{index}:{s1.GetHashCode()}");
                    Console.WriteLine($"{index}:{s2.GetHashCode()}");
                    Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode());
                    finishedThread++;
                });
                thread.Start();
            }

            while (finishedThread != threadsCount)
            {
            }
            //所有HashCode相同

            //测试通线程唯一
            Console.WriteLine("======= 开始线程内唯一测试 =======");
            finishedThread = 0;
            for (int i = 0; i < threadsCount; i++)
            {
                var thread = new Thread(() =>
                {
                    var index = i;
                    Console.WriteLine($"-------{index}----------");

                    var threadScope = Thread.GetData(Thread.GetNamedDataSlot(CO2NET.SenparcDI.SENPARC_DI_THREAD_SERVICE_Scope)) as IServiceScope;
                    Console.WriteLine("ServiceScope:" + threadScope?.GetHashCode());
                    Console.WriteLine("ServiceProcider:" + threadScope?.ServiceProvider.GetHashCode());

                    var s1 = SenparcDI.GetService <SenparcSetting>(false);
                    var s2 = SenparcDI.GetService <SenparcSetting>(false);
                    Console.WriteLine($"{index}:{s1.GetHashCode()}");
                    Console.WriteLine($"{index}:{s2.GetHashCode()}");
                    Assert.AreEqual(s1.GetHashCode(), s2.GetHashCode());

                    threadScope = Thread.GetData(Thread.GetNamedDataSlot(CO2NET.SenparcDI.SENPARC_DI_THREAD_SERVICE_Scope)) as IServiceScope;
                    Console.WriteLine("ServiceScope:" + threadScope.GetHashCode());
                    Console.WriteLine("ServiceProcider:" + threadScope.ServiceProvider.GetHashCode());
                    Console.WriteLine("-----------------");
                    finishedThread++;
                });
                thread.Start();
            }

            while (finishedThread != threadsCount)
            {
            }
            //单线程内HashCode相同
        }