Beispiel #1
0
        public async Task ActorMultipleTest()
        {
            int count = 0;

            ActorManager manager = new ActorManager();

            manager.Register <ICache>(_context, (c, x, m) => new StringCache(x, m, y => count += y));

            ActorKey key1   = new ActorKey("Cache/Test1");
            ICache   cache1 = await manager.CreateProxyAsync <ICache>(_context, key1);

            count.Should().Be(1);

            ActorKey key2   = new ActorKey("Cache/Test2");
            ICache   cache2 = await manager.CreateProxyAsync <ICache>(_context, key2);

            count.Should().Be(2);

            await manager.DeactivateAsync <ICache>(_context, key1);

            count.Should().Be(1);

            await manager.DeactivateAsync <ICache>(_context, key2);

            count.Should().Be(0);

            await manager.DeactivateAllAsync(_context);
        }
Beispiel #2
0
        public async Task ActorSameInstanceTest()
        {
            int count = 0;

            var manager = new ActorManager();

            manager.Register <ICache>(_context, (c, x, m) => new StringCache(x, manager, y => count += y));

            ActorKey key1   = new ActorKey("Cache/Test1");
            ICache   cache1 = await manager.CreateProxyAsync <ICache>(_context, key1);

            count.Should().Be(1);

            ActorKey key2   = new ActorKey("Cache/Test2");
            ICache   cache2 = await manager.CreateProxyAsync <ICache>(_context, key2);

            count.Should().Be(2);

            const string firstText  = "first";
            const string secondText = "secondFirst";

            await cache1.Add(firstText);

            bool test = await cache1.IsCached(firstText);

            test.Should().BeTrue();

            await cache2.Add(secondText);

            bool test2 = await cache2.IsCached(secondText);

            test2.Should().BeTrue();

            ICache cache1Dup = await manager.CreateProxyAsync <ICache>(_context, key1);

            test = await cache1Dup.IsCached(firstText);

            test.Should().BeTrue();
            test = await cache1Dup.IsCached(secondText);

            test.Should().BeFalse();

            await manager.DeactivateAsync <ICache>(_context, key1);

            await manager.DeactivateAsync <ICache>(_context, key2);

            count.Should().Be(0);

            await manager.DeactivateAllAsync(_context);
        }
Beispiel #3
0
        public async Task ActorMethodTest()
        {
            int count = 0;

            ActorManager manager = new ActorManager();

            manager.Register <ICache>(_context, (c, x, m) => new StringCache(x, m, y => count += y));

            ActorKey key1   = new ActorKey("Cache/Test1");
            ICache   cache1 = await manager.CreateProxyAsync <ICache>(_context, 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 manager.DeactivateAsync <ICache>(_context, key1);

            count.Should().Be(0);

            await manager.DeactivateAllAsync(_context);
        }
Beispiel #4
0
        public async Task ActorAutoFacActionSimpleTest()
        {
            IActorManager manager = new ActorManager();

            var builder = new ContainerBuilder();

            builder.RegisterType <StringCache>().As <ICache>();
            builder.RegisterInstance(manager).As <IActorManager>();
            IContainer container = builder.Build();

            using (container.BeginLifetimeScope())
            {
                manager.Register <ICache>(_context,
                                          (c, k, m) =>
                {
                    return(container.Resolve <ICache>(new TypedParameter(typeof(ActorKey), k)));
                });

                ActorKey key   = new ActorKey("cache/test");
                ICache   cache = await manager.CreateProxyAsync <ICache>(_context, key);

                (await cache.GetCount()).Should().Be(1);
                await manager.DeactivateAsync <ICache>(_context, key);

                (await cache.GetCount()).Should().Be(2);
            }

            await manager.DeactivateAllAsync(_context);
        }
Beispiel #5
0
        public async Task ActorDeactivateAllTest()
        {
            int count = 0;

            var manager = new ActorManager();

            manager.Register <ICache>(_context, (c, x, m) => new StringCache(x, m, y => count += y));

            ActorKey key   = new ActorKey("cache/test");
            ICache   cache = await manager.CreateProxyAsync <ICache>(_context, key);

            count.Should().Be(1);
            await manager.DeactivateAllAsync(_context);

            count.Should().Be(0);
        }
Beispiel #6
0
        public async Task ActorSimpleTimerTest()
        {
            IActorManager manager = new ActorManager();

            manager.Register <ITimerActor>(_context, (context, k, m) => new TimeActor(k, m));

            ActorKey    key        = new ActorKey("timer/test");
            ITimerActor timerActor = await manager.CreateProxyAsync <ITimerActor>(_context, key);

            foreach (var index in Enumerable.Range(0, 20))
            {
                await Task.Delay(TimeSpan.FromSeconds(1));

                int count = await timerActor.GetCount();

                if (count > 2)
                {
                    break;
                }
            }

            (await timerActor.GetCount()).Should().BeGreaterThan(2);
            await manager.DeactivateAsync <ITimerActor>(_context, key);
        }
Beispiel #7
0
 public async Task Add(IWorkContext context, int value)
 {
     _actorSum = _actorSum ?? (await ActorManager.CreateProxyAsync <IActorSum>(context, new ActorKey(sumActorName)));
     await _actorSum.Add(value);
 }