Cancel() public method

public Cancel ( ) : void
return void
Example #1
0
        public void Given_a_canceled_Cancelable_with_callback_Then_Cancel_should_be_possible_to_call_again_but_callbacks_should_not_be_called_again()
        {
            var c = new Cancelable(Sys.Scheduler);
            var callbacks = new AtomicCounter(0);
            c.Token.Register(() => callbacks.IncrementAndGet());
            c.Cancel();

            c.Cancel();

            //HACK: Using the fact that when Cancel is called, callbacks are executed synchronously
            callbacks.Current.ShouldBe(1);
        }
Example #2
0
        public void When_CancelFalse_is_called_Then_first_exception_should_not_prevent_rest_from_being_called()
        {
            var c = new Cancelable(Sys.Scheduler);
            var callbacks = new AtomicCounter(0);
            c.Token.Register(() => { throw new Exception("Something wonderful has happened."); });
            c.Token.Register(() => callbacks.IncrementAndGet());
            c.Token.Register(() => { throw new Exception("Your AKKA is alive!!!"); });
            try
            {
                //First callback should prevent the second one from being called
                c.Cancel(throwOnFirstException: false);
            }
            catch (AggregateException aggregateException)
            {
                aggregateException = aggregateException.Flatten();
                foreach (var e in aggregateException.InnerExceptions)
                {
                    if (!e.Message.StartsWith("Your") && !e.Message.StartsWith("Something"))
                        throw new Exception("Invalid exception received: " + e, e);

                }
            }
            //HACK: Using the fact that when Cancel is called, callbacks are executed synchronously
            callbacks.Current.ShouldBe(1);
            c.IsCancellationRequested.ShouldBeTrue();
            c.Token.IsCancellationRequested.ShouldBeTrue();
        }
Example #3
0
 public void When_cancel_has_been_called_Should_be_cancelled()
 {
     var c = new Cancelable(Sys.Scheduler);
     c.Cancel();
     c.IsCancellationRequested.ShouldBeTrue();
     c.Token.IsCancellationRequested.ShouldBeTrue();
 }
        public void When_ScheduleTellRepeatedly_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleTellRepeatedly(100, 2, TestActor, "Test", ActorRefs.NoSender, cancelable);
            cancelable.Cancel();

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        public void When_ScheduleOnce_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleOnce(100, () => TestActor.Tell("Test"), cancelable);
            cancelable.Cancel();

            //Validate that no messages were sent
            ExpectNoMsg(150);
        }
        public void When_canceling_existing_running_repeaters_Then_their_future_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new DedicatedThreadScheduler(Sys);

            var cancelable = new Cancelable(scheduler);
            scheduler.ScheduleTellRepeatedly(0, 150, TestActor, "Test", ActorRefs.NoSender, cancelable);
            ExpectMsg("Test");
            cancelable.Cancel();

            //Validate that no more messages were sent
            ExpectNoMsg(200);
        }
        public void ScheduleRepeatedly_in_milliseconds_Tests_and_verify_the_interval(int initialDelay, int interval)
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new HashedWheelTimerScheduler(Sys.Settings.Config, Log);

            try
            {
                var cancelable = new Cancelable(Sys.Scheduler);
                var receiver = ActorOf(dsl =>
                {
                    //Receive three messages, and store the time when these were received
                    //after three messages stop the actor and send the times to TestActor
                    var messages = new List<DateTimeOffset>();
                    dsl.Receive<string>((s, context) =>
                    {
                        messages.Add(context.System.Scheduler.Now);
                        if (messages.Count == 3)
                        {
                            TestActor.Tell(messages);
                            cancelable.Cancel();
                            context.Stop(context.Self);
                        }
                    });
                });
                scheduler.ScheduleRepeatedly(initialDelay, interval, () => receiver.Tell(""), cancelable);

                //Expect to get a list from receiver after it has received three messages
                var dateTimeOffsets = ExpectMsg<List<DateTimeOffset>>();
                dateTimeOffsets.ShouldHaveCount(3);
                Action<int, int> validate = (a, b) =>
                {
                    var valA = dateTimeOffsets[a];
                    var valB = dateTimeOffsets[b];
                    var diffBetweenMessages = Math.Abs((valB - valA).TotalMilliseconds);
                    var diffInMs = Math.Abs(diffBetweenMessages - interval);
                    var deviate = (diffInMs/interval);
                    deviate.Should(val => val < 0.1,
                        string.Format(
                            "Expected the interval between message {1} and {2} to deviate maximum 10% from {0}. It was {3} ms between the messages. It deviated {4}%",
                            interval, a + 1, b + 1, diffBetweenMessages, deviate*100));
                };
                validate(0, 1);
                validate(1, 2);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }

        }
        public void When_ScheduleTellOnce_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new HashedWheelTimerScheduler(Sys.Settings.Config, Log);

            try
            {
                var cancelable = new Cancelable(scheduler);
                scheduler.ScheduleTellOnce(100, TestActor, "Test", ActorRefs.NoSender, cancelable);
                cancelable.Cancel();

                //Validate that no messages were sent
                ExpectNoMsg(150);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
        }
        public void When_ScheduleOnce_and_then_canceling_before_they_occur_Then_their_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IActionScheduler scheduler = new DedicatedThreadScheduler(Sys.Settings.Config, Log);

            try
            {
                var cancelable = new Cancelable(scheduler);
                scheduler.ScheduleOnce(100, () => TestActor.Tell("Test"), cancelable);
                cancelable.Cancel();

                //Validate that no messages were sent
                ExpectNoMsg(150);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
        }
Example #10
0
        /// <summary>
        /// Sends a message to the collection of routees.
        /// </summary>
        /// <param name="message">The message that is being sent.</param>
        /// <param name="sender">The actor sending the message.</param>
        public override void Send(object message, IActorRef sender)
        {
            _routees.Shuffle();
            var routeeIndex = new AtomicCounter(0);

            var completion = new TaskCompletionSource<object>();
            var cancelable = new Cancelable(_scheduler);

            completion.Task
                .ContinueWith(task => cancelable.Cancel(false));

            if (_routees.Length == 0)
            {
                completion.TrySetResult(NoRoutee);
            }
            else
            {
                _scheduler.Advanced.ScheduleRepeatedly(TimeSpan.Zero, _interval, async () =>
                {
                    var currentIndex = routeeIndex.GetAndIncrement();
                    if (currentIndex >= _routees.Length) 
                        return;

                    try
                    {

                        completion.TrySetResult(await ((Task<object>)_routees[currentIndex].Ask(message, _within)));
                    }
                    catch (TaskCanceledException)
                    {
                        completion.TrySetResult(
                            new Status.Failure(
                                new AskTimeoutException(String.Format("Ask timed out on {0} after {1}", sender, _within))));
                    }
                }, cancelable);
            }

            completion.Task.PipeTo(sender);
        }
Example #11
0
        public void Given_linked_Cancelable_When_canceling_underlying_Then_linked_should_be_canceled()
        {
            var underlying = new Cancelable(Sys.Scheduler);
            var linked = Cancelable.CreateLinkedCancelable(Sys.Scheduler, underlying);
            var latch = CreateTestLatch();
            linked.Token.Register(() => latch.CountDown());

            underlying.Cancel();
            underlying.IsCancellationRequested.ShouldBeTrue();
            underlying.Token.IsCancellationRequested.ShouldBeTrue();
            latch.Ready();

            linked.IsCancellationRequested.ShouldBeTrue();
            linked.Token.IsCancellationRequested.ShouldBeTrue();
        }
        public void When_canceling_existing_running_repeaters_Then_their_future_actions_should_not_be_invoked()
        {
            // Prepare, set up actions to be fired
            IScheduler scheduler = new HashedWheelTimerScheduler(Sys.Settings.Config, Log);

            try
            {
                var cancelable = new Cancelable(scheduler);
                scheduler.ScheduleTellRepeatedly(0, 150, TestActor, "Test", ActorRefs.NoSender, cancelable);
                ExpectMsg("Test");
                cancelable.Cancel();

                //Validate that no more messages were sent
                ExpectNoMsg(200);
            }
            finally
            {
                scheduler.AsInstanceOf<IDisposable>().Dispose();
            }
            
        }