Пример #1
0
        public async Task Given2Actors_WhenCreatedAndDeletedDifferentTask_ShouldPass()
        {
            int       count   = 0;
            int       count2  = 0;
            const int max     = 10;
            const int maxLoop = 10;

            IActorManager manager = new ActorManager();

            manager.Register <ICache>(_ => new StringCache(y => CountControl(ref count, y)));
            manager.Register <ICache2>(_ => new StringCache2(y => CountControl(ref count2, y)));

            for (int loop = 0; loop < maxLoop; loop++)
            {
                _output.WriteLine($"Loop: {loop}");

                await Enumerable.Range(0, max)
                .Select(x => new Task[]
                {
                    Task.Run(async() => {
                        ActorKey key = new ActorKey($"cache/test/{x}");
                        ICache cache = await manager.CreateProxy <ICache>(key);
                        cache.GetActorKey().Should().Be(key);
                        cache.GetActorManager().Should().Be(manager);
                    }),
                    Task.Run(async() => {
                        ActorKey key2  = new ActorKey($"cache/test/{x}");
                        ICache2 cache2 = await manager.CreateProxy <ICache2>(key2);
                        cache2.GetActorKey().Should().Be(key2);
                        cache2.GetActorManager().Should().Be(manager);
                    }),
                })
                .SelectMany(x => x)
                .WhenAll();

                count.Should().Be(max);
                count2.Should().Be(max);

                await Enumerable.Range(0, max)
                .Select(x => new Task <bool>[]
                {
                    Task.Run(async() => await manager.Deactivate <ICache>(new ActorKey($"cache/test/{x}"))),
                    Task.Run(async() => await manager.Deactivate <ICache2>(new ActorKey($"cache/test/{x}"))),
                })
                .SelectMany(x => x)
                .WhenAll();

                count.Should().Be(0);
                count2.Should().Be(0);

                await manager.DeactivateAll();

                count.Should().Be(0);
                count2.Should().Be(0);
            }
        }
Пример #2
0
 /// <summary>
 /// 使用外部插件
 /// </summary>
 /// <param name="classNameAndAssembly">命名空间.类名,程序集名称</param>
 public static void Use(string classNameAndAssembly)
 {
     if (classNameAndAssembly.IsNullEmpty())
     {
         _cache = (ICache2)"Pub.Class.WebCache,Pub.Class.WebCache".LoadClass();
     }
     else
     {
         _cache = (ICache2)classNameAndAssembly.LoadClass();
     }
 }
Пример #3
0
        public async Task Given2Actors_WhenCreatedAndDeleted_ShouldPass()
        {
            int       count  = 0;
            int       count2 = 0;
            const int max    = 10;

            IActorManager manager = new ActorManager();

            manager.Register <ICache>(_ => new StringCache(y => CountControl(ref count, y)));
            manager.Register <ICache2>(_ => new StringCache2(y => CountControl(ref count2, y)));

            await Enumerable.Range(0, max)
            .Select(async x =>
            {
                ActorKey key = new ActorKey($"cache/test/{x}");
                ICache cache = await manager.CreateProxy <ICache>(key);
                cache.GetActorKey().Should().Be(key);
                cache.GetActorManager().Should().Be(manager);

                ActorKey key2  = new ActorKey($"cache/test/{x}");
                ICache2 cache2 = await manager.CreateProxy <ICache2>(key2);
                cache2.GetActorKey().Should().Be(key2);
                cache2.GetActorManager().Should().Be(manager);
            })
            .WhenAll();

            count.Should().Be(max);
            count2.Should().Be(max);

            await Enumerable.Range(0, max)
            .Select(async x =>
            {
                ActorKey key = new ActorKey($"cache/test/{x}");
                (await manager.Deactivate <ICache>(key)).Should().BeTrue();

                ActorKey key2 = new ActorKey($"cache/test/{x}");
                (await manager.Deactivate <ICache2>(key2)).Should().BeTrue();
            })
            .WhenAll();

            count.Should().Be(0);
            count2.Should().Be(0);

            await manager.DeactivateAll();

            count.Should().Be(0);
            count2.Should().Be(0);
        }
Пример #4
0
        public async Task Given2Actors_WhenCreatedAndDeleted_ShouldPass()
        {
            int       count  = 0;
            int       count2 = 0;
            const int max    = 10;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => CountControl(ref count, y)))
                                         .Register <ICache2>(() => new StringCache2(y => CountControl(ref count2, y)));

            Enumerable.Range(0, max)
            .ForEach(x =>
            {
                ActorKey key = new ActorKey($"cache/test/{x}");
                ICache cache = actorHost.GetActor <ICache>(key);
                cache.GetActorKey().Should().Be(key);
                cache.GetActorHost().Should().Be(actorHost);

                ActorKey key2  = new ActorKey($"cache/test/{x}");
                ICache2 cache2 = actorHost.GetActor <ICache2>(key2);
                cache2.GetActorKey().Should().Be(key2);
                cache2.GetActorHost().Should().Be(actorHost);
            });

            count.Should().Be(max);
            count2.Should().Be(max);

            await Enumerable.Range(0, max)
            .Select(async x =>
            {
                ActorKey key = new ActorKey($"cache/test/{x}");
                (await actorHost.Deactivate <ICache>(key)).Should().BeTrue();

                ActorKey key2 = new ActorKey($"cache/test/{x}");
                (await actorHost.Deactivate <ICache2>(key2)).Should().BeTrue();
            })
            .WhenAll();

            count.Should().Be(0);
            count2.Should().Be(0);

            await actorHost.DeactivateAll();

            count.Should().Be(0);
            count2.Should().Be(0);
        }
Пример #5
0
 /// <summary>
 /// 使用外部插件
 /// </summary>
 public static void Use <T>(T t) where T : ICache2, new()
 {
     _cache = Singleton <T> .Instance();
 }
Пример #6
0
 /// <summary>
 /// 使用外部插件
 /// </summary>
 /// <param name="dllFileName">dll文件名</param>
 /// <param name="className">命名空间.类名</param>
 public static void Use(string dllFileName, string className)
 {
     _cache = (ICache2)dllFileName.LoadClass(className);
 }