예제 #1
0
        public void CoordinatedShutdown_must_run_from_given_phase()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase },
                { "b", Phase("a") },
                { "c", Phase("b", "a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                return(TaskEx.Completed);
            });

            co.AddTask("b", "b1", () =>
            {
                TestActor.Tell("B");
                return(TaskEx.Completed);
            });

            co.AddTask("c", "c1", () =>
            {
                TestActor.Tell("C");
                return(TaskEx.Completed);
            });

            co.Run(customReason, "b").Wait(RemainingOrDefault);
            ReceiveN(2).Should().Equal(new object[] { "B", "C" });
            co.ShutdownReason.ShouldBeEquivalentTo(customReason);
        }
예제 #2
0
        public void CoordinatedShutdown_must_be_possible_to_add_tasks_in_later_phase_from_earlier_phase()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase },
                { "b", Phase("a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                co.AddTask("b", "b1", () =>
                {
                    TestActor.Tell("B");
                    return(TaskEx.Completed);
                });
                return(TaskEx.Completed);
            });

            co.Run(CoordinatedShutdown.UnknownReason.Instance).Wait(RemainingOrDefault);
            ExpectMsg("A");
            ExpectMsg("B");
        }
예제 #3
0
        public void CoordinatedShutdown_must_abort_if_recover_is_off()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "b", new Phase(ImmutableHashSet <string> .Empty.Add("a"), TimeSpan.FromMilliseconds(100), false) },
                { "c", Phase("b", "a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("b", "b1", () =>
            {
                TestActor.Tell("B");
                return(new TaskCompletionSource <Done>().Task); // never completed
            });

            co.AddTask("c", "c1", () =>
            {
                TestActor.Tell("C");
                return(TaskEx.Completed);
            });

            var result = co.Run(CoordinatedShutdown.UnknownReason.Instance);

            ExpectMsg("B");
            Intercept <TimeoutException>(() => result.Wait(RemainingOrDefault));
            ExpectNoMsg(TimeSpan.FromMilliseconds(200)); // C not run
        }
        public void CoordinatedShutdown_must_only_run_once()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                return(TaskEx.Completed);
            });

            co.Run().Wait(RemainingOrDefault);
            ExpectMsg("A");
            co.Run().Wait(RemainingOrDefault);
            TestActor.Tell("done");
            ExpectMsg("done"); // no additional A
        }
예제 #5
0
        public void CoordinatedShutdown_must_only_run_once()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                return(TaskEx.Completed);
            });

            co.ShutdownReason.Should().BeNull();
            co.Run(customReason).Wait(RemainingOrDefault);
            co.ShutdownReason.ShouldBeEquivalentTo(customReason);
            ExpectMsg("A");
            co.Run(CoordinatedShutdown.UnknownReason.Instance).Wait(RemainingOrDefault);
            TestActor.Tell("done");
            ExpectMsg("done"); // no additional A
            co.ShutdownReason.ShouldBeEquivalentTo(customReason);
        }
예제 #6
0
        public void CoordinatedShutdown_must_continue_after_timeout_or_failure()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase },
                { "b", new Phase(ImmutableHashSet <string> .Empty.Add("a"), TimeSpan.FromMilliseconds(100), true) },
                { "c", Phase("b", "a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                return(TaskEx.FromException <Done>(new Exception("boom")));
            });

            co.AddTask("a", "a2", () =>
            {
                Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
                TestActor.Tell("A");
                return(TaskEx.Completed);
            });

            co.AddTask("b", "b1", () =>
            {
                TestActor.Tell("B");
                return(new TaskCompletionSource <Done>().Task); // never completed
            });

            co.AddTask("c", "c1", () =>
            {
                TestActor.Tell("C");
                return(TaskEx.Completed);
            });

            co.Run(CoordinatedShutdown.UnknownReason.Instance).Wait(RemainingOrDefault);
            ExpectMsg("A");
            ExpectMsg("A");
            ExpectMsg("B");
            ExpectMsg("C");
        }
예제 #7
0
        static async Task Main(string[] args)
        {
            string parsedConfig = File
                                  .ReadAllText("Config.ini")
                                  .Replace("__CURRENT_MACHINE_NAME__", Environment.MachineName);

            Config config = ConfigurationFactory.ParseString(parsedConfig);

            using (ActorSystem actorSystem = ActorSystem.Create("myActor", config))
            {
                // Need to give it a second or 2 to full startup and print everything to console
                await Task.Delay(TimeSpan.FromSeconds(2)).ConfigureAwait(false);

                Console.WriteLine("Press any key to shutdown...");
                Console.ReadKey(true);
                CoordinatedShutdown coordinatedShutdown = CoordinatedShutdown.Get(actorSystem);
                await coordinatedShutdown.Run(ClrExitReason.Instance).ConfigureAwait(false);

                await actorSystem.WhenTerminated.ConfigureAwait(false);
            }
        }
예제 #8
0
        public void CoordinatedShutdown_must_run_ordered_phases()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "a", EmptyPhase },
                { "b", Phase("a") },
                { "c", Phase("b", "a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("a", "a1", () =>
            {
                TestActor.Tell("A");
                return(TaskEx.Completed);
            });

            co.AddTask("b", "b1", () =>
            {
                TestActor.Tell("B");
                return(TaskEx.Completed);
            });

            co.AddTask("b", "b2", () =>
            {
                // to verify that c is not performed before b
                Task.Delay(TimeSpan.FromMilliseconds(100)).Wait();
                TestActor.Tell("B");
                return(TaskEx.Completed);
            });

            co.AddTask("c", "c1", () =>
            {
                TestActor.Tell("C");
                return(TaskEx.Completed);
            });

            co.Run(CoordinatedShutdown.UnknownReason.Instance).Wait(RemainingOrDefault);
            ReceiveN(4).Should().Equal(new object[] { "A", "B", "B", "C" });
        }
        public void CoordinatedShutdown_must_abort_if_recover_is_off()
        {
            var phases = new Dictionary <string, Phase>()
            {
                { "b", new Phase(ImmutableHashSet <string> .Empty.Add("a"), TimeSpan.FromMilliseconds(100), false) },
                { "c", Phase("b", "a") }
            };

            var co = new CoordinatedShutdown(ExtSys, phases);

            co.AddTask("b", "b1", () =>
            {
                TestActor.Tell("B");
                return(new TaskCompletionSource <Done>().Task); // never completed
            });

            co.AddTask("c", "c1", () =>
            {
                TestActor.Tell("C");
                return(TaskEx.Completed);
            });

            var result = co.Run();

            ExpectMsg("B");
            Intercept <AggregateException>(() =>
            {
                if (result.Wait(RemainingOrDefault))
                {
                    result.Exception?.Flatten().InnerException.Should().BeOfType <TimeoutException>();
                }
                else
                {
                    throw new Exception("CoordinatedShutdown task did not complete");
                }
            });

            ExpectNoMsg(TimeSpan.FromMilliseconds(200)); // C not run
        }