Esempio n. 1
0
        public async Task ActorMultipleTest()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            ActorKey key1   = new ActorKey("Cache/Test1");
            ICache   cache1 = actorHost.GetActor <ICache>(key1);

            count.Should().Be(1);

            ActorKey key2   = new ActorKey("Cache/Test2");
            ICache   cache2 = actorHost.GetActor <ICache>(key2);

            count.Should().Be(2);

            (await actorHost.Deactivate <ICache>(key1)).Should().BeTrue();
            count.Should().Be(1);

            (await actorHost.Deactivate <ICache>(key2)).Should().BeTrue();
            count.Should().Be(0);

            await actorHost.DeactivateAll();
        }
Esempio n. 2
0
        public async Task ActorMethodTest()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            ActorKey key1   = new ActorKey("Cache/Test1");
            ICache   cache1 = actorHost.GetActor <ICache>(key1);

            count.Should().Be(1);

            const string firstText = "first";

            bool test = await cache1.IsCached(firstText);

            test.Should().BeFalse();
            await cache1.Add(firstText);

            test = await cache1.IsCached(firstText);

            test.Should().BeTrue();

            (await actorHost.Deactivate <ICache>(key1)).Should().BeTrue();;
            count.Should().Be(0);

            await actorHost.DeactivateAll();
        }
Esempio n. 3
0
        public async Task Given2Actors_WhenCreatedAndDeletedDifferentTask_ShouldPass()
        {
            int       count   = 0;
            int       count2  = 0;
            const int max     = 10;
            const int maxLoop = 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)));

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

                await Enumerable.Range(0, max)
                .Select(x => new Task[]
                {
                    Task.Run(() => {
                        ActorKey key = new ActorKey($"cache/test/{x}");
                        ICache cache = actorHost.GetActor <ICache>(key);
                        cache.GetActorKey().Should().Be(key);
                        cache.GetActorHost().Should().Be(actorHost);
                    }),
                    Task.Run(() => {
                        ActorKey key2  = new ActorKey($"cache/test/{x}");
                        ICache2 cache2 = actorHost.GetActor <ICache2>(key2);
                        cache2.GetActorKey().Should().Be(key2);
                        cache2.GetActorHost().Should().Be(actorHost);
                    }),
                })
                .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 actorHost.Deactivate <ICache>(new ActorKey($"cache/test/{x}"))),
                    Task.Run(async() => await actorHost.Deactivate <ICache2>(new ActorKey($"cache/test/{x}"))),
                })
                .SelectMany(x => x)
                .WhenAll();

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

                await actorHost.DeactivateAll();

                count.Should().Be(0);
                count2.Should().Be(0);
            }
        }
Esempio n. 4
0
        public async Task GivenActor_WhenDeactivatedAll_ActorCountShouldBeZero()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            ActorKey key   = new ActorKey("cache/test");
            ICache   cache = actorHost.GetActor <ICache>(key);

            count.Should().Be(1);
            await actorHost.DeactivateAll();

            count.Should().Be(0);
        }
Esempio n. 5
0
        public async Task Given2Actors_WhenCreatedAndDeletedDifferentKeyRange_ShouldPass()
        {
            int       count   = 0;
            int       count2  = 0;
            const int max     = 1000;
            const int maxLoop = 10;

            using IActorHost actorHost = new ActorHost(100000, _loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => CountControl(ref count, y)))
                                         .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, i) => new Task[]
                {
                    Task.Run(() => actorHost.GetActor <ICache>(new ActorKey($"cache/test/{i}"))),
                    Task.Run(() => actorHost.GetActor <ICache2>(new ActorKey($"cache/test/{i+100}"))),
                })
                .SelectMany(x => x)
                .WhenAll();

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

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

                results.All(x => x == true).Should().BeTrue();

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

                await actorHost.DeactivateAll();

                count.Should().Be(0);
                count2.Should().Be(0);
            }
        }
Esempio n. 6
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);
        }
Esempio n. 7
0
        public async Task GivenActor_WhenMultipleCreated_KeyAndManagerShouldBeSet()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            const int max     = 10;
            var       keyList = new List <ActorKey>();

            Enumerable.Range(0, max)
            .ForEach((x, index) =>
            {
                ActorKey key = new ActorKey($"cache/test_{index}");
                keyList.Add(key);

                ICache cache = actorHost.GetActor <ICache>(key);
                cache.GetActorKey().Should().Be(key);
                cache.GetActorManager().Should().Be(actorHost);
            });

            count.Should().Be(max);

            keyList
            .ForEach(x =>
            {
                ICache cache = actorHost.GetActor <ICache>(x);
                cache.GetActorKey().Should().Be(x);
                cache.GetActorManager().Should().Be(actorHost);
            });

            count.Should().Be(max);

            await keyList
            .ForEachAsync(async x =>
            {
                (await actorHost.Deactivate <ICache>(x)).Should().BeTrue();
            });

            count.Should().Be(0);

            await actorHost.DeactivateAll();

            count.Should().Be(0);
        }
Esempio n. 8
0
        public async Task GivenActor_WhenCreatedDeactivated_CountsShouldFollowLifecycle()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            ActorKey key   = new ActorKey("cache/test");
            ICache   cache = actorHost.GetActor <ICache>(key);

            count.Should().Be(1);
            (await actorHost.Deactivate <ICache>(key)).Should().BeTrue();
            count.Should().Be(0);

            await actorHost.DeactivateAll();

            count.Should().Be(0);
        }
Esempio n. 9
0
        public async Task GivenActor_WhenCreated_KeyAndManagerShouldBeSet()
        {
            int count = 0;

            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <ICache>(() => new StringCache(y => count += y));

            ActorKey key   = new ActorKey("cache/test");
            ICache   cache = actorHost.GetActor <ICache>(key);

            cache.GetActorKey().Should().Be(key);
            cache.GetActorManager().Should().Be(actorHost);

            count.Should().Be(1);
            (await actorHost.Deactivate <ICache>(key)).Should().BeTrue();
            count.Should().Be(0);

            await actorHost.DeactivateAll();

            count.Should().Be(0);
        }
        public async Task ActorSingleChainTest()
        {
            using IActorHost actorHost = new ActorHost(_loggerFactory)
                                         .Register <IActorNode>(() => new ActorNode())
                                         .Register <IActorSum>(() => new ActorSum());

            ActorKey   key  = new ActorKey("node/test");
            IActorNode node = actorHost.GetActor <IActorNode>(key);

            int sum = 0;

            for (int i = 0; i < 10; i++)
            {
                await node.Add(i);

                sum += i;
            }

            IActorSum sumActor = actorHost.GetActor <IActorSum>(new ActorKey(sumActorName));

            (await sumActor.GetSum()).Should().Be(sum);

            await actorHost.DeactivateAll();
        }