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 static PromiseActorRef Apply(IActorRefProvider provider, TimeSpan timeout, object targetName, string messageClassName, IActorRef sender = null) { sender = sender ?? ActorRefs.NoSender; var result = new TaskCompletionSource <object>(); var a = new PromiseActorRef(provider, result, messageClassName); var scheduler = provider.Guardian.Underlying.System.Scheduler.Advanced; var c = new Cancelable(scheduler, timeout); scheduler.ScheduleOnce(timeout, () => result.TrySetResult(new Status.Failure(new AskTimeoutException( string.Format("Ask timed out on [{0}] after [{1} ms]. Sender[{2}] sent message of type {3}.", targetName, timeout.TotalMilliseconds, sender, messageClassName)))), c); result.Task.ContinueWith(r => { try { a.Stop(); } finally { c.Cancel(false); } }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously); return(a); }
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(); }
/// <summary>Schedules to send a message repeatedly. The first message will be sent after the specified initial delay and there after at the rate specified.</summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialMillisecondsDelay">The time in milliseconds that has to pass before the first message is sent.</param> /// <param name="millisecondsInterval">The interval, i.e. the time in milliseconds that has to pass between messages are sent.</param> /// <param name="receiver">The receiver.</param> /// <param name="message">The message.</param> /// <param name="sender">The sender.</param> /// <returns>An <see cref="ICancelable"/> that can be used to cancel sending of the message. Once the message already has been sent, it cannot be cancelled.</returns> public static ICancelable ScheduleTellRepeatedlyCancelable(this IScheduler scheduler, int initialMillisecondsDelay, int millisecondsInterval, ICanTell receiver, object message, IActorRef sender) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleTellRepeatedly(initialMillisecondsDelay, millisecondsInterval, receiver, message, sender, cancelable); return(cancelable); }
public static void ScheduleOnce(this IScheduler scheduler, TimeSpan initialDelay, Action action, CancellationToken cancellationToken) { var source = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); var cancelable = new Cancelable(scheduler.Advanced, source); scheduler.Advanced.ScheduleOnce(initialDelay, action, cancelable); }
/// <summary> /// Schedules an action to be invoked after an delay. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="millisecondsDelay">The time in milliseconds that has to pass before the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleOnceCancelable(this IActionScheduler scheduler, int millisecondsDelay, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleOnce(millisecondsDelay, action, cancelable); return(cancelable); }
/// <summary> /// Schedules an action to be invoked after an delay. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="delay">The time period that has to pass before the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleOnceCancelable(this IActionScheduler scheduler, TimeSpan delay, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleOnce(delay, action, cancelable); return(cancelable); }
/// <summary> /// Schedules an action to be invoked after an initial delay and then repeatedly. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialDelay">The time period that has to pass before first invocation.</param> /// <param name="interval">The interval, i.e. the time period that has to pass between the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleRepeatedlyCancelable(this IActionScheduler scheduler, TimeSpan initialDelay, TimeSpan interval, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleRepeatedly(initialDelay, interval, action, cancelable); return(cancelable); }
public PerformanceCounterActor(string seriesName, Func<PerformanceCounter> performanceCounterGenerator) { _seriesName = seriesName; _performanceCounterGenerator = performanceCounterGenerator; _subscriptions = new HashSet<IActorRef>(); _cancelPublishing = new Cancelable(Context.System.Scheduler); }
/// <summary> /// Schedules an action to be invoked after an initial delay and then repeatedly. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialMillisecondsDelay">The time in milliseconds that has to pass before first invocation.</param> /// <param name="millisecondsInterval">The interval, i.e. the time in milliseconds that has to pass between the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleRepeatedlyCancelable(this IActionScheduler scheduler, int initialMillisecondsDelay, int millisecondsInterval, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleRepeatedly(initialMillisecondsDelay, millisecondsInterval, action, cancelable); return(cancelable); }
/// <summary>Schedules to send a message once after a specified period of time.</summary> /// <param name="scheduler">The scheduler</param> /// <param name="millisecondsDelay">The time in milliseconds that has to pass before the message is sent.</param> /// <param name="receiver">The receiver.</param> /// <param name="message">The message.</param> /// <param name="sender">The sender.</param> /// <returns>An <see cref="ICancelable"/> that can be used to cancel sending of the message. Once the message already has been sent, it cannot be cancelled.</returns> public static ICancelable ScheduleTellOnceCancelable(this IScheduler scheduler, int millisecondsDelay, ICanTell receiver, object message, IActorRef sender) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleTellOnce(millisecondsDelay, receiver, message, sender, cancelable); return(cancelable); }
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); }
private void Off() { Receive<TurnOffMessage>(m => { }); Receive<TurnOnMessage>(m => { Become(On); _cancel = new Cancelable(_scheduler); _scheduler.ScheduleTellRepeatedly(_interval, _interval, Self, new TickMessage(), Self, _cancel); Console.WriteLine("Starting scheduler"); }); Receive<TickMessage>(m => Console.WriteLine("Recieved tick when actor state was Off") /* Theoritically possible */ ); }
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_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 Timer(string name, object message, bool repeat, int generation, IActorContext context, ILoggingAdapter debugLog) { _debugLog = debugLog; Context = context; Generation = generation; Repeat = repeat; Message = message; Name = name; var scheduler = context.System.Scheduler; _scheduler = scheduler; _ref = new Cancelable(scheduler); }
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_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(); } }
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_canceling_existing_running_repeaters_by_scheduling_the_cancellation_ahead_of_time_Then_their_future_actions_should_not_be_invoked() { // Prepare, set up actions to be fired IScheduler scheduler = new DedicatedThreadScheduler(Sys); var cancelableOdd = new Cancelable(scheduler); scheduler.ScheduleTellRepeatedly(1, 150, TestActor, "Test", ActorRefs.NoSender, cancelableOdd); cancelableOdd.CancelAfter(50); //Expect one message ExpectMsg("Test"); //Validate that no messages were sent ExpectNoMsg(200); }
public TestReceiveActor() { Receive<ScheduleOnceMessage>(x => { Context.System.Scheduler.ScheduleTellOnce(x.ScheduleOffset, Sender, x, Self); }); Receive<RescheduleMessage>(x => { Context.System.Scheduler.ScheduleTellRepeatedly(x.InitialOffset, x.ScheduleOffset, Sender, x, Self); }); Receive<CancelableMessage>(x => { _cancelable = new Cancelable(Context.System.Scheduler); Context.System.Scheduler.ScheduleTellRepeatedly(x.ScheduleOffset, x.ScheduleOffset, Sender, x, Self, _cancelable); }); Receive<CancelMessage>(x => { _cancelable.Cancel(); }); }
/// <summary> /// Schedules an action to be invoked after an initial delay and then repeatedly. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialMillisecondsDelay">The time in milliseconds that has to pass before first invocation.</param> /// <param name="millisecondsInterval">The interval, i.e. the time in milliseconds that has to pass between the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleRepeatedlyCancelable(this IActionScheduler scheduler, int initialMillisecondsDelay, int millisecondsInterval, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleRepeatedly(initialMillisecondsDelay, millisecondsInterval, action, cancelable); return cancelable; }
/// <summary> /// Schedules an action to be invoked after an initial delay and then repeatedly. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialDelay">The time period that has to pass before first invocation.</param> /// <param name="interval">The interval, i.e. the time period that has to pass between the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleRepeatedlyCancelable(this IActionScheduler scheduler, TimeSpan initialDelay, TimeSpan interval, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleRepeatedly(initialDelay, interval, action, cancelable); return cancelable; }
/// <summary> /// Schedules an action to be invoked after an delay. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="millisecondsDelay">The time in milliseconds that has to pass before the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleOnceCancelable(this IActionScheduler scheduler, int millisecondsDelay, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleOnce(millisecondsDelay, action, cancelable); return cancelable; }
/// <summary> /// Schedules an action to be invoked after an delay. /// </summary> /// <param name="scheduler">The scheduler</param> /// <param name="delay">The time period that has to pass before the action is invoked.</param> /// <param name="action">The action to perform.</param> /// <returns>A cancelable that can be used to cancel the action from being executed</returns> public static ICancelable ScheduleOnceCancelable(this IActionScheduler scheduler, TimeSpan delay, Action action) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleOnce(delay, action, cancelable); return cancelable; }
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(); }
/// <summary>Schedules to send a message once after a specified period of time.</summary> /// <param name="scheduler">The scheduler</param> /// <param name="millisecondsDelay">The time in milliseconds that has to pass before the message is sent.</param> /// <param name="receiver">The receiver.</param> /// <param name="message">The message.</param> /// <param name="sender">The sender.</param> /// <returns>An <see cref="ICancelable"/> that can be used to cancel sending of the message. Once the message already has been sent, it cannot be cancelled.</returns> public static ICancelable ScheduleTellOnceCancelable(this IScheduler scheduler, int millisecondsDelay, ICanTell receiver, object message, IActorRef sender) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleTellOnce(millisecondsDelay, receiver, message, sender, cancelable); return cancelable; }
public void When_canceling_existing_running_repeaters_by_scheduling_the_cancellation_ahead_of_time_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 cancelableOdd = new Cancelable(scheduler); scheduler.ScheduleTellRepeatedly(1, 150, TestActor, "Test", ActorRefs.NoSender, cancelableOdd); cancelableOdd.CancelAfter(50); //Expect one message ExpectMsg("Test"); //Validate that no messages were sent ExpectNoMsg(200); } finally { scheduler.AsInstanceOf<IDisposable>().Dispose(); } }
public void Should_be_possible_to_call_CancelAfterTimespan() { var c = new Cancelable(Sys.Scheduler); var latch = CreateTestLatch(); c.Token.Register(() => latch.CountDown()); c.CancelAfter(TimeSpan.FromMilliseconds(50)); c.IsCancellationRequested.ShouldBeFalse(); latch.Ready(); c.IsCancellationRequested.ShouldBeTrue(); c.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 IActionScheduler scheduler = new DedicatedThreadScheduler(Sys.Settings.Config, Log); try { var cancelable = new Cancelable(scheduler); scheduler.ScheduleRepeatedly(0, 150, () => TestActor.Tell("Test"), cancelable); ExpectMsg("Test"); cancelable.Cancel(); //Validate that no more messages were sent ExpectNoMsg(200); } finally { scheduler.AsInstanceOf<IDisposable>().Dispose(); } }
/// <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); }
/// <summary>Schedules to send a message repeatedly. The first message will be sent after the specified initial delay and there after at the rate specified.</summary> /// <param name="scheduler">The scheduler</param> /// <param name="initialMillisecondsDelay">The time in milliseconds that has to pass before the first message is sent.</param> /// <param name="millisecondsInterval">The interval, i.e. the time in milliseconds that has to pass between messages are sent.</param> /// <param name="receiver">The receiver.</param> /// <param name="message">The message.</param> /// <param name="sender">The sender.</param> /// <returns>An <see cref="ICancelable"/> that can be used to cancel sending of the message. Once the message already has been sent, it cannot be cancelled.</returns> public static ICancelable ScheduleTellRepeatedlyCancelable(this IScheduler scheduler, int initialMillisecondsDelay, int millisecondsInterval, ICanTell receiver, object message, IActorRef sender) { var cancelable = new Cancelable(scheduler); scheduler.ScheduleTellRepeatedly(initialMillisecondsDelay, millisecondsInterval, receiver, message, sender, cancelable); return cancelable; }
public void Should_not_be_cancelled_initially() { var c = new Cancelable(Sys.Scheduler); c.IsCancellationRequested.ShouldBeFalse(); c.Token.IsCancellationRequested.ShouldBeFalse(); }