public async Task ActorMultipleTest() { int count = 0; ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxy <ICache>(key1); count.Should().Be(1); ActorKey key2 = new ActorKey("Cache/Test2"); ICache cache2 = await manager.CreateProxy <ICache>(key2); count.Should().Be(2); (await manager.Deactivate <ICache>(key1)).Should().BeTrue(); count.Should().Be(1); (await manager.Deactivate <ICache>(key2)).Should().BeTrue(); count.Should().Be(0); await manager.DeactivateAll(); }
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); } }
public async Task ActorSameInstanceTest() { int count = 0; var manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxy <ICache>(key1); count.Should().Be(1); ActorKey key2 = new ActorKey("Cache/Test2"); ICache cache2 = await manager.CreateProxy <ICache>(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.CreateProxy <ICache>(key1); test = await cache1Dup.IsCached(firstText); test.Should().BeTrue(); test = await cache1Dup.IsCached(secondText); test.Should().BeFalse(); (await manager.Deactivate <ICache>(key1)).Should().BeTrue(); (await manager.Deactivate <ICache>(key2)).Should().BeTrue(); count.Should().Be(0); await manager.DeactivateAll(); }
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); }
public async Task Given2Actors_WhenCreatedAndDeletedDifferentKeyRange_ShouldPass() { int count = 0; int count2 = 0; const int max = 1000; 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, i) => new Task[] { Task.Run(async() => await manager.CreateProxy <ICache>(new ActorKey($"cache/test/{i}"))), Task.Run(async() => await manager.CreateProxy <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 manager.Deactivate <ICache>(new ActorKey($"cache/test/{i}"))), Task.Run(async() => await manager.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 manager.DeactivateAll(); count.Should().Be(0); count2.Should().Be(0); } }
public async Task GivenAutofac_WhenProxyCreated_ShouldPass() { 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>(_ => container.Resolve <ICache>()); ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxy <ICache>(key); (await cache.GetCount()).Should().Be(1); await manager.Deactivate <ICache>(key); (await cache.GetCount()).Should().Be(2); } await manager.DeactivateAll(); }
public async Task ActorMethodTest() { int count = 0; ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key1 = new ActorKey("Cache/Test1"); ICache cache1 = await manager.CreateProxy <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 manager.Deactivate <ICache>(key1)).Should().BeTrue();; count.Should().Be(0); await manager.DeactivateAll(); }
public async Task ActorProxyMultiTaskTest() { const int taskCount = 10; ActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache()); var tasks = new List <Task>(); ActorKey key1 = new ActorKey("Cache/Test1"); CancellationTokenSource tokenSource = new CancellationTokenSource(); for (int i = 0; i < taskCount; i++) { Task t = Task.Run(() => TestAccess(manager, key1, tokenSource.Token)); tasks.Add(t); } await Task.Delay(TimeSpan.FromSeconds(10)); tokenSource.Cancel(); Task.WaitAll(tasks.ToArray()); (await manager.Deactivate <ICache>(key1)).Should().BeTrue(); }
public async Task GivenActor_WhenMultipleCreated_KeyAndManagerShouldBeSet() { int count = 0; IActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); const int max = 10; var keyList = new List <ActorKey>(); await Enumerable.Range(0, max) .ForEachAsync(async(x, index) => { ActorKey key = new ActorKey($"cache/test_{index}"); keyList.Add(key); ICache cache = await manager.CreateProxy <ICache>(key); cache.GetActorKey().Should().Be(key); cache.GetActorManager().Should().Be(manager); }); count.Should().Be(max); await keyList .ForEachAsync(async x => { ICache cache = await manager.CreateProxy <ICache>(x); cache.GetActorKey().Should().Be(x); cache.GetActorManager().Should().Be(manager); }); count.Should().Be(max); await keyList .ForEachAsync(async x => { (await manager.Deactivate <ICache>(x)).Should().BeTrue(); }); count.Should().Be(0); await manager.DeactivateAll(); count.Should().Be(0); }
public async Task GivenActor_WhenCreatedDeactivated_CountsShouldFollowLifecycle() { int count = 0; IActorManager manager = new ActorManager(); manager.Register <ICache>(_ => new StringCache(y => count += y)); ActorKey key = new ActorKey("cache/test"); ICache cache = await manager.CreateProxy <ICache>(key); count.Should().Be(1); (await manager.Deactivate <ICache>(key)).Should().BeTrue(); count.Should().Be(0); await manager.DeactivateAll(); count.Should().Be(0); }
public async Task ActorSimpleTimerTest() { IActorManager manager = new ActorManager(); manager.Register <ITimerActor>(_ => new TimeActor()); ActorKey key = new ActorKey("timer/test"); ITimerActor timerActor = await manager.CreateProxy <ITimerActor>(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.Deactivate <ITimerActor>(key); }