示例#1
0
        public void multiple_actor_references_should_run_sequentially()
        {
            // arrange
            var random = new Random();

            var target = new ActorPool();

            int count = 0;

            async Task Work()
            {
                int temp = count;

                temp++;
                await Task.Yield();

                count = temp;
            }

            var id = Guid.NewGuid().ToString();

            // act
            Parallel.For(0, 100, _ =>
            {
                using (IActor actor = target.GetOrCreate(id))
                {
                    Thread.Sleep(random.Next(10));
                    actor.Enqueue(Work);
                }
            });

            // assert
            Thread.Sleep(5);
            count.Should().Be(100);
        }
        public void ParallelTest()
        {
            var random = new Random();

            var target = new ActorPool();

            int count = 0;

            async Task Work()
            {
                int temp = count;

                temp++;
                await Task.Yield();

                count = temp;
            }

            Parallel.For(0, 100, _ =>
            {
                using (var worker = target.GetOrCreate("a"))
                {
                    Thread.Sleep(random.Next(50));
                    worker.Enqueue(Work);
                }
            });

            Thread.Sleep(5);
            count.Should().Be(100);
        }
        public void GetOrCreateTest()
        {
            var target = new ActorPool();

            var countdownEvent = new CountdownEvent(3);

            using (IActor actor = target.GetOrCreate("a"))
            {
                Task Func()
                {
                    countdownEvent.Signal();
                    return(Task.CompletedTask);
                }

                actor.Enqueue(Func);
                actor.Enqueue(Func);
                actor.Enqueue(Func);
            }

            countdownEvent.Wait(TimeSpan.FromSeconds(1)).Should().Be(true);

            Thread.Sleep(100);

            target.EstimatedCount.Should().Be(0);
        }
示例#4
0
        public void active_actor_should_have_multiple_reference()
        {
            // arrange
            var target = new ActorPool();

            var id = Guid.NewGuid().ToString();

            // act
            using var actor  = target.GetOrCreate(id);
            using var actor2 = target.GetOrCreate(id);

            // assert
            ActorPoolSnapshot snapshot = target.TakeSnapshot();

            snapshot.Count.Should().Be(1);
            snapshot[id].QueuedWork.Should().Be(0);
            snapshot[id].References.Should().Be(2);
        }
示例#5
0
        public void active_actor_should_have_reference()
        {
            // arrange
            var target = new ActorPool();

            // act
            using var actor = target.GetOrCreate(Guid.NewGuid().ToString());

            // assert
            target.TakeSnapshot().Count.Should().Be(1);
        }
示例#6
0
        public void GetOrCreateShouldReturnWorkingActorWithNoHandles()
        {
            var target = new ActorPool();

            var e = new ManualResetEventSlim(false);

            Task Work()
            {
                e.Wait();
                return(Task.CompletedTask);
            }

            using (var actor = target.GetOrCreate("a"))
            {
                actor.Enqueue(Work);
            }

            // this should not block
            using var temp = target.GetOrCreate("a");
        }