How to use Assert method of Microsoft.Coyote.Actors.Mocks.MockEventQueue class

Best Coyote code snippet using Microsoft.Coyote.Actors.Mocks.MockEventQueue.Assert

ActorExecutionContext.cs

Source:ActorExecutionContext.cs Github

copy

Full Screen

...187 internal virtual Actor CreateActor(ActorId id, Type type, string name, Actor creator, EventGroup eventGroup)188 {189 if (!type.IsSubclassOf(typeof(Actor)))190 {191 this.Assert(false, "Type '{0}' is not an actor.", type.FullName);192 }193 if (id is null)194 {195 id = this.CreateActorId(type, name);196 }197 else if (id.Runtime != null && id.Runtime != this)198 {199 this.Assert(false, "Unbound actor id '{0}' was created by another runtime.", id.Value);200 }201 else if (id.Type != type.FullName)202 {203 this.Assert(false, "Cannot bind actor id '{0}' of type '{1}' to an actor of type '{2}'.",204 id.Value, id.Type, type.FullName);205 }206 else207 {208 id.Bind(this);209 }210 // If no event group is provided then inherit the current group from the creator.211 if (eventGroup is null && creator != null)212 {213 eventGroup = creator.EventGroup;214 }215 Actor actor = ActorFactory.Create(type);216 ActorOperation op = this.Runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing ?217 this.GetOrCreateActorOperation(id, actor) : null;218 IEventQueue eventQueue = new EventQueue(actor);219 actor.Configure(this, id, op, eventQueue, eventGroup);220 actor.SetupEventHandlers();221 if (!this.ActorMap.TryAdd(id, actor))222 {223 throw new InvalidOperationException($"An actor with id '{id.Value}' already exists.");224 }225 return actor;226 }227 /// <summary>228 /// Returns the operation for the specified actor id, or creates a new229 /// operation if it does not exist yet.230 /// </summary>231 protected ActorOperation GetOrCreateActorOperation(ActorId id, Actor actor)232 {233 var op = this.Runtime.GetOperationWithId<ActorOperation>(id.Value);234 return op ?? new ActorOperation(id.Value, id.Name, actor, this.Runtime);235 }236 /// <inheritdoc/>237 public virtual void SendEvent(ActorId targetId, Event initialEvent, EventGroup eventGroup = default, SendOptions options = null) =>238 this.SendEvent(targetId, initialEvent, null, eventGroup, options);239 /// <inheritdoc/>240 public virtual Task<bool> SendEventAndExecuteAsync(ActorId targetId, Event initialEvent,241 EventGroup eventGroup = null, SendOptions options = null) =>242 this.SendEventAndExecuteAsync(targetId, initialEvent, null, eventGroup, options);243 /// <summary>244 /// Sends an asynchronous <see cref="Event"/> to an actor.245 /// </summary>246 internal virtual void SendEvent(ActorId targetId, Event e, Actor sender, EventGroup eventGroup, SendOptions options)247 {248 EnqueueStatus enqueueStatus = this.EnqueueEvent(targetId, e, sender, eventGroup, out Actor target);249 if (enqueueStatus is EnqueueStatus.EventHandlerNotRunning)250 {251 this.OnActorEventHandlerStarted(target.Id);252 this.RunActorEventHandler(target, null, false);253 }254 }255 /// <summary>256 /// Sends an asynchronous <see cref="Event"/> to an actor. Returns immediately if the target was257 /// already running. Otherwise blocks until the target handles the event and reaches quiescence.258 /// </summary>259 internal virtual async Task<bool> SendEventAndExecuteAsync(ActorId targetId, Event e, Actor sender,260 EventGroup eventGroup, SendOptions options)261 {262 EnqueueStatus enqueueStatus = this.EnqueueEvent(targetId, e, sender, eventGroup, out Actor target);263 if (enqueueStatus is EnqueueStatus.EventHandlerNotRunning)264 {265 this.OnActorEventHandlerStarted(target.Id);266 await this.RunActorEventHandlerAsync(target, null, false);267 return true;268 }269 return enqueueStatus is EnqueueStatus.Dropped;270 }271 /// <summary>272 /// Enqueues an event to the actor with the specified id.273 /// </summary>274 private EnqueueStatus EnqueueEvent(ActorId targetId, Event e, Actor sender, EventGroup eventGroup, out Actor target)275 {276 if (e is null)277 {278 string message = sender != null ?279 string.Format("{0} is sending a null event.", sender.Id.ToString()) :280 "Cannot send a null event.";281 this.Assert(false, message);282 }283 if (targetId is null)284 {285 string message = (sender != null) ?286 string.Format("{0} is sending event {1} to a null actor.", sender.Id.ToString(), e.ToString())287 : string.Format("Cannot send event {0} to a null actor.", e.ToString());288 this.Assert(false, message);289 }290 if (this.Runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing &&291 this.Runtime.TryGetExecutingOperation(out ControlledOperation current))292 {293 this.Runtime.DelayOperation(current);294 }295 target = this.GetActorWithId<Actor>(targetId);296 // If no group is provided we default to passing along the group from the sender.297 if (eventGroup is null && sender != null)298 {299 eventGroup = sender.EventGroup;300 }301 Guid opId = eventGroup is null ? Guid.Empty : eventGroup.Id;302 if (target is null || target.IsHalted)303 {304 this.LogWriter.LogSendEvent(targetId, sender?.Id.Name, sender?.Id.Type,305 (sender as StateMachine)?.CurrentStateName ?? default, e, opId, isTargetHalted: true);306 this.HandleDroppedEvent(e, targetId);307 return EnqueueStatus.Dropped;308 }309 this.LogWriter.LogSendEvent(targetId, sender?.Id.Name, sender?.Id.Type,310 (sender as StateMachine)?.CurrentStateName ?? default, e, opId, isTargetHalted: false);311 EnqueueStatus enqueueStatus = target.Enqueue(e, eventGroup, null);312 if (enqueueStatus == EnqueueStatus.Dropped)313 {314 this.HandleDroppedEvent(e, targetId);315 }316 return enqueueStatus;317 }318 /// <summary>319 /// Runs a new asynchronous actor event handler.320 /// This is a fire and forget invocation.321 /// </summary>322 private void RunActorEventHandler(Actor actor, Event initialEvent, bool isFresh)323 {324 if (this.Runtime.SchedulingPolicy is SchedulingPolicy.Fuzzing)325 {326 this.Runtime.TaskFactory.StartNew(async state =>327 {328 await this.RunActorEventHandlerAsync(actor, initialEvent, isFresh);329 },330 actor.Operation,331 default,332 this.Runtime.TaskFactory.CreationOptions | TaskCreationOptions.DenyChildAttach,333 this.Runtime.TaskFactory.Scheduler);334 }335 else336 {337 Task.Run(async () => await this.RunActorEventHandlerAsync(actor, initialEvent, isFresh));338 }339 }340 /// <summary>341 /// Runs a new asynchronous actor event handler.342 /// </summary>343 private async Task RunActorEventHandlerAsync(Actor actor, Event initialEvent, bool isFresh)344 {345 try346 {347 if (isFresh)348 {349 await actor.InitializeAsync(initialEvent);350 }351 await actor.RunEventHandlerAsync();352 }353 catch (Exception ex)354 {355 this.Runtime.IsRunning = false;356 this.RaiseOnFailureEvent(ex);357 }358 finally359 {360 if (actor.IsHalted)361 {362 this.ActorMap.TryRemove(actor.Id, out Actor _);363 }364 this.OnActorEventHandlerCompleted(actor.Id);365 }366 }367 /// <summary>368 /// Invoked when the event handler of the specified actor starts.369 /// </summary>370 protected void OnActorEventHandlerStarted(ActorId actorId)371 {372 if (this.Runtime.SchedulingPolicy != SchedulingPolicy.None)373 {374 lock (this.QuiescenceSyncObject)375 {376 this.EnabledActors.Add(actorId);377 }378 }379 }380 /// <summary>381 /// Invoked when the event handler of the specified actor completes.382 /// </summary>383 protected void OnActorEventHandlerCompleted(ActorId actorId)384 {385 if (this.Runtime.SchedulingPolicy != SchedulingPolicy.None)386 {387 lock (this.QuiescenceSyncObject)388 {389 this.EnabledActors.Remove(actorId);390 if (this.IsActorQuiescenceAwaited && this.EnabledActors.Count is 0)391 {392 this.QuiescenceCompletionSource.TrySetResult(true);393 }394 }395 }396 }397 /// <summary>398 /// Creates a new timer that sends a <see cref="TimerElapsedEvent"/> to its owner actor.399 /// </summary>400 internal virtual IActorTimer CreateActorTimer(TimerInfo info, Actor owner) => new ActorTimer(info, owner);401 /// <inheritdoc/>402 public virtual EventGroup GetCurrentEventGroup(ActorId currentActorId)403 {404 Actor actor = this.GetActorWithId<Actor>(currentActorId);405 return actor?.CurrentEventGroup;406 }407 /// <summary>408 /// Gets the actor of type <typeparamref name="TActor"/> with the specified id,409 /// or null if no such actor exists.410 /// </summary>411 private TActor GetActorWithId<TActor>(ActorId id)412 where TActor : Actor =>413 id != null && this.ActorMap.TryGetValue(id, out Actor value) &&414 value is TActor actor ? actor : null;415 /// <summary>416 /// Returns the next available unique operation id.417 /// </summary>418 /// <returns>Value representing the next available unique operation id.</returns>419 private ulong GetNextOperationId() => this.Runtime.GetNextOperationId();420 /// <inheritdoc/>421 public bool RandomBoolean() => this.GetNondeterministicBooleanChoice(null, null);422 /// <summary>423 /// Returns a controlled nondeterministic boolean choice.424 /// </summary>425 internal virtual bool GetNondeterministicBooleanChoice(string callerName, string callerType) =>426 this.Runtime.GetNextNondeterministicBooleanChoice(callerName, callerType);427 /// <inheritdoc/>428 public int RandomInteger(int maxValue) => this.GetNondeterministicIntegerChoice(maxValue, null, null);429 /// <summary>430 /// Returns a controlled nondeterministic integer choice.431 /// </summary>432 internal virtual int GetNondeterministicIntegerChoice(int maxValue, string callerName, string callerType) =>433 this.Runtime.GetNextNondeterministicIntegerChoice(maxValue, callerName, callerType);434 /// <summary>435 /// Logs that the specified actor invoked an action.436 /// </summary>437 internal virtual void LogInvokedAction(Actor actor, MethodInfo action, string handlingStateName, string currentStateName)438 {439 if (this.Configuration.IsVerbose)440 {441 this.LogWriter.LogExecuteAction(actor.Id, handlingStateName, currentStateName, action.Name);442 }443 }444 /// <summary>445 /// Logs that the specified actor enqueued an <see cref="Event"/>.446 /// </summary>447 internal virtual void LogEnqueuedEvent(Actor actor, Event e, EventGroup eventGroup, EventInfo eventInfo)448 {449 if (this.Configuration.IsVerbose)450 {451 this.LogWriter.LogEnqueueEvent(actor.Id, e);452 }453 }454 /// <summary>455 /// Logs that the specified actor dequeued an <see cref="Event"/>.456 /// </summary>457 internal virtual void LogDequeuedEvent(Actor actor, Event e, EventInfo eventInfo, bool isFreshDequeue)458 {459 if (this.Configuration.IsVerbose)460 {461 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;462 this.LogWriter.LogDequeueEvent(actor.Id, stateName, e);463 }464 }465 /// <summary>466 /// Logs that the specified actor dequeued the default <see cref="Event"/>.467 /// </summary>468 [MethodImpl(MethodImplOptions.AggressiveInlining)]469 internal virtual void LogDefaultEventDequeued(Actor actor)470 {471 }472 /// <summary>473 /// Logs that the specified actor raised an <see cref="Event"/>.474 /// </summary>475 internal virtual void LogRaisedEvent(Actor actor, Event e, EventGroup eventGroup, EventInfo eventInfo)476 {477 if (this.Configuration.IsVerbose)478 {479 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;480 this.LogWriter.LogRaiseEvent(actor.Id, stateName, e);481 }482 }483 /// <summary>484 /// Logs that the specified actor is handling a raised <see cref="Event"/>.485 /// </summary>486 internal virtual void LogHandleRaisedEvent(Actor actor, Event e)487 {488 if (this.Configuration.IsVerbose)489 {490 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;491 this.LogWriter.LogHandleRaisedEvent(actor.Id, stateName, e);492 }493 }494 /// <summary>495 /// Logs that the specified actor is handling a raised <see cref="HaltEvent"/>.496 /// </summary>497 internal virtual void LogHandleHaltEvent(Actor actor, int inboxSize)498 {499 if (this.Configuration.IsVerbose)500 {501 this.LogWriter.LogHalt(actor.Id, inboxSize);502 }503 }504 /// <summary>505 /// Logs that the specified actor called <see cref="Actor.ReceiveEventAsync(Type[])"/>506 /// or one of its overloaded methods.507 /// </summary>508 [MethodImpl(MethodImplOptions.AggressiveInlining)]509 internal virtual void LogReceiveCalled(Actor actor)510 {511 }512 /// <summary>513 /// Logs that the specified actor enqueued an event that it was waiting to receive.514 /// </summary>515 internal virtual void LogReceivedEvent(Actor actor, Event e, EventInfo eventInfo)516 {517 if (this.Configuration.IsVerbose)518 {519 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;520 this.LogWriter.LogReceiveEvent(actor.Id, stateName, e, wasBlocked: true);521 }522 }523 /// <summary>524 /// Logs that the specified actor received an event without waiting because the event525 /// was already in the inbox when the actor invoked the receive statement.526 /// </summary>527 internal virtual void LogReceivedEventWithoutWaiting(Actor actor, Event e, EventInfo eventInfo)528 {529 if (this.Configuration.IsVerbose)530 {531 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;532 this.LogWriter.LogReceiveEvent(actor.Id, stateName, e, wasBlocked: false);533 }534 }535 /// <summary>536 /// Logs that the specified actor is waiting to receive an event of one of the specified types.537 /// </summary>538 internal virtual void LogWaitEvent(Actor actor, IEnumerable<Type> eventTypes)539 {540 if (this.Configuration.IsVerbose)541 {542 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;543 var eventWaitTypesArray = eventTypes.ToArray();544 if (eventWaitTypesArray.Length is 1)545 {546 this.LogWriter.LogWaitEvent(actor.Id, stateName, eventWaitTypesArray[0]);547 }548 else549 {550 this.LogWriter.LogWaitEvent(actor.Id, stateName, eventWaitTypesArray);551 }552 }553 }554 /// <summary>555 /// Logs that the event handler of the specified actor terminated.556 /// </summary>557 internal virtual void LogEventHandlerTerminated(Actor actor, DequeueStatus dequeueStatus)558 {559 if (this.Configuration.IsVerbose)560 {561 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : default;562 this.LogWriter.LogEventHandlerTerminated(actor.Id, stateName, dequeueStatus);563 }564 }565 /// <summary>566 /// Logs that the specified state machine entered a state.567 /// </summary>568 internal virtual void LogEnteredState(StateMachine stateMachine)569 {570 if (this.Configuration.IsVerbose)571 {572 this.LogWriter.LogStateTransition(stateMachine.Id, stateMachine.CurrentStateName, isEntry: true);573 }574 }575 /// <summary>576 /// Logs that the specified state machine exited a state.577 /// </summary>578 internal virtual void LogExitedState(StateMachine stateMachine)579 {580 if (this.Configuration.IsVerbose)581 {582 this.LogWriter.LogStateTransition(stateMachine.Id, stateMachine.CurrentStateName, isEntry: false);583 }584 }585 /// <summary>586 /// Logs that the specified state machine invoked pop.587 /// </summary>588 [MethodImpl(MethodImplOptions.AggressiveInlining)]589 internal virtual void LogPopState(StateMachine stateMachine)590 {591 }592 /// <summary>593 /// Logs that the specified state machine invoked an action.594 /// </summary>595 internal virtual void LogInvokedOnEntryAction(StateMachine stateMachine, MethodInfo action)596 {597 if (this.Configuration.IsVerbose)598 {599 this.LogWriter.LogExecuteAction(stateMachine.Id, stateMachine.CurrentStateName,600 stateMachine.CurrentStateName, action.Name);601 }602 }603 /// <summary>604 /// Logs that the specified state machine invoked an action.605 /// </summary>606 internal virtual void LogInvokedOnExitAction(StateMachine stateMachine, MethodInfo action)607 {608 if (this.Configuration.IsVerbose)609 {610 this.LogWriter.LogExecuteAction(stateMachine.Id, stateMachine.CurrentStateName,611 stateMachine.CurrentStateName, action.Name);612 }613 }614 /// <summary>615 /// Builds the coverage graph information, if any. This information is only available616 /// when <see cref="Configuration.IsActivityCoverageReported"/> is enabled.617 /// </summary>618 internal CoverageInfo BuildCoverageInfo()619 {620 var result = this.CoverageInfo;621 if (result != null)622 {623 var builder = this.LogWriter.GetLogsOfType<ActorRuntimeLogGraphBuilder>()624 .FirstOrDefault(builder => builder.CollapseInstances);625 if (builder != null)626 {627 result.CoverageGraph = builder.SnapshotGraph(false);628 }629 var eventCoverage = this.LogWriter.GetLogsOfType<ActorRuntimeLogEventCoverage>().FirstOrDefault();630 if (eventCoverage != null)631 {632 result.EventInfo = eventCoverage.EventCoverage;633 }634 }635 return result;636 }637 /// <summary>638 /// Returns the DGML graph of the current execution, if there is any.639 /// </summary>640 internal Graph GetExecutionGraph()641 {642 Graph result = null;643 var builder = this.LogWriter.GetLogsOfType<ActorRuntimeLogGraphBuilder>()644 .FirstOrDefault(builder => !builder.CollapseInstances);645 if (builder != null)646 {647 result = builder.SnapshotGraph(true);648 }649 return result;650 }651 /// <summary>652 /// Returns the program counter of the specified actor.653 /// </summary>654 [MethodImpl(MethodImplOptions.AggressiveInlining)]655 internal virtual int GetActorProgramCounter(ActorId actorId) => 0;656 /// <inheritdoc/>657 public void RegisterMonitor<T>()658 where T : Monitor =>659 this.Runtime.RegisterMonitor<T>();660 /// <inheritdoc/>661 public void Monitor<T>(Event e)662 where T : Monitor =>663 this.Runtime.Monitor<T>(e);664 /// <summary>665 /// Invokes the specified <see cref="Specifications.Monitor"/> with the specified <see cref="Event"/>.666 /// </summary>667 internal void InvokeMonitor(Type type, Event e, string senderName, string senderType, string senderStateName) =>668 this.Runtime.InvokeMonitor(type, e, senderName, senderType, senderStateName);669 /// <inheritdoc/>670 [MethodImpl(MethodImplOptions.AggressiveInlining)]671 public void Assert(bool predicate) => this.Runtime.Assert(predicate);672 /// <inheritdoc/>673 [MethodImpl(MethodImplOptions.AggressiveInlining)]674 public void Assert(bool predicate, string s, object arg0) => this.Runtime.Assert(predicate, s, arg0);675 /// <inheritdoc/>676 [MethodImpl(MethodImplOptions.AggressiveInlining)]677 public void Assert(bool predicate, string s, object arg0, object arg1) => this.Runtime.Assert(predicate, s, arg0, arg1);678 /// <inheritdoc/>679 [MethodImpl(MethodImplOptions.AggressiveInlining)]680 public void Assert(bool predicate, string s, object arg0, object arg1, object arg2) =>681 this.Runtime.Assert(predicate, s, arg0, arg1, arg2);682 /// <inheritdoc/>683 [MethodImpl(MethodImplOptions.AggressiveInlining)]684 public void Assert(bool predicate, string s, params object[] args) => this.Runtime.Assert(predicate, s, args);685 /// <summary>686 /// Asserts that the actor calling an actor method is also the actor that is currently executing.687 /// </summary>688 [MethodImpl(MethodImplOptions.AggressiveInlining)]689 internal virtual void AssertExpectedCallerActor(Actor caller, string calledAPI)690 {691 }692 /// <summary>693 /// Raises the <see cref="OnFailure"/> event with the specified <see cref="Exception"/>.694 /// </summary>695 internal void RaiseOnFailureEvent(Exception exception) => this.Runtime.RaiseOnFailureEvent(exception);696 /// <summary>697 /// Handle the specified dropped <see cref="Event"/>.698 /// </summary>699 internal void HandleDroppedEvent(Event e, ActorId id) => this.OnEventDropped?.Invoke(e, id);700 /// <summary>701 /// Throws an <see cref="AssertionFailureException"/> exception containing the specified exception.702 /// </summary>703#if !DEBUG704 [DebuggerHidden]705#endif706 internal void WrapAndThrowException(Exception exception, string s, params object[] args) =>707 this.Runtime.WrapAndThrowException(exception, s, args);708 /// <inheritdoc/>709 [Obsolete("Please set the Logger property directory instead of calling this method.")]710 public TextWriter SetLogger(TextWriter logger)711 {712 var result = this.LogWriter.SetLogger(new TextWriterLogger(logger));713 if (result != null)714 {715 return result.TextWriter;716 }717 return null;718 }719 /// <inheritdoc/>720 public void RegisterLog(IRuntimeLog log) => this.Runtime.RegisterLog(log);721 /// <inheritdoc/>722 public void RemoveLog(IRuntimeLog log) => this.Runtime.RemoveLog(log);723 /// <summary>724 /// Returns a task that completes once all actors reach quiescence.725 /// </summary>726 internal Task WaitUntilQuiescenceAsync()727 {728 lock (this.QuiescenceSyncObject)729 {730 if (this.EnabledActors.Count > 0)731 {732 this.IsActorQuiescenceAwaited = true;733 return this.QuiescenceCompletionSource.Task;734 }735 else736 {737 return Task.CompletedTask;738 }739 }740 }741 /// <inheritdoc/>742 public void Stop() => this.Runtime.Stop();743 /// <summary>744 /// Disposes runtime resources.745 /// </summary>746 protected virtual void Dispose(bool disposing)747 {748 if (disposing)749 {750 this.ActorMap.Clear();751 this.EnabledActors.Clear();752 }753 }754 /// <inheritdoc/>755 public void Dispose()756 {757 this.Dispose(true);758 GC.SuppressFinalize(this);759 }760 /// <summary>761 /// The mocked execution context of an actor program.762 /// </summary>763 internal sealed class Mock : ActorExecutionContext764 {765 /// <summary>766 /// Set of all created actor ids.767 /// </summary>768 private readonly ConcurrentDictionary<ActorId, byte> ActorIds;769 /// <summary>770 /// Map that stores all unique names and their corresponding actor ids.771 /// </summary>772 private readonly ConcurrentDictionary<string, ActorId> NameValueToActorId;773 /// <summary>774 /// Map of program counters used for state-caching to distinguish775 /// scheduling from non-deterministic choices.776 /// </summary>777 private readonly ConcurrentDictionary<ActorId, int> ProgramCounterMap;778 /// <summary>779 /// If true, the actor execution is controlled, else false.780 /// </summary>781 internal override bool IsExecutionControlled => true;782 /// <summary>783 /// Initializes a new instance of the <see cref="Mock"/> class.784 /// </summary>785 internal Mock(Configuration configuration, CoyoteRuntime runtime)786 : base(configuration, runtime)787 {788 this.ActorIds = new ConcurrentDictionary<ActorId, byte>();789 this.NameValueToActorId = new ConcurrentDictionary<string, ActorId>();790 this.ProgramCounterMap = new ConcurrentDictionary<ActorId, int>();791 }792 /// <inheritdoc/>793 public override ActorId CreateActorIdFromName(Type type, string name)794 {795 // It is important that all actor ids use the monotonically incrementing796 // value as the id during testing, and not the unique name.797 var id = this.NameValueToActorId.GetOrAdd(name, key => this.CreateActorId(type, key));798 this.ActorIds.TryAdd(id, 0);799 return id;800 }801 /// <inheritdoc/>802 public override ActorId CreateActor(Type type, Event initialEvent = null, EventGroup eventGroup = null) =>803 this.CreateActor(null, type, null, initialEvent, eventGroup);804 /// <inheritdoc/>805 public override ActorId CreateActor(Type type, string name, Event initialEvent = null, EventGroup eventGroup = null) =>806 this.CreateActor(null, type, name, initialEvent, eventGroup);807 /// <inheritdoc/>808 public override ActorId CreateActor(ActorId id, Type type, Event initialEvent = null, EventGroup eventGroup = null)809 {810 this.Assert(id != null, "Cannot create an actor using a null actor id.");811 return this.CreateActor(id, type, null, initialEvent, eventGroup);812 }813 /// <inheritdoc/>814 public override Task<ActorId> CreateActorAndExecuteAsync(Type type, Event initialEvent = null, EventGroup eventGroup = null) =>815 this.CreateActorAndExecuteAsync(null, type, null, initialEvent, eventGroup);816 /// <inheritdoc/>817 public override Task<ActorId> CreateActorAndExecuteAsync(Type type, string name, Event initialEvent = null, EventGroup eventGroup = null) =>818 this.CreateActorAndExecuteAsync(null, type, name, initialEvent, eventGroup);819 /// <inheritdoc/>820 public override Task<ActorId> CreateActorAndExecuteAsync(ActorId id, Type type, Event initialEvent = null, EventGroup eventGroup = null)821 {822 this.Assert(id != null, "Cannot create an actor using a null actor id.");823 return this.CreateActorAndExecuteAsync(id, type, null, initialEvent, eventGroup);824 }825 /// <summary>826 /// Creates a new actor of the specified <see cref="Type"/> and name, using the specified827 /// unbound actor id, and passes the specified optional <see cref="Event"/>. This event828 /// can only be used to access its payload, and cannot be handled.829 /// </summary>830 internal ActorId CreateActor(ActorId id, Type type, string name, Event initialEvent = null, EventGroup eventGroup = null)831 {832 var creatorOp = this.Runtime.GetExecutingOperation<ActorOperation>();833 return this.CreateActor(id, type, name, initialEvent, creatorOp?.Actor, eventGroup);834 }835 /// <summary>836 /// Creates a new <see cref="Actor"/> of the specified <see cref="Type"/>.837 /// </summary>838 internal override ActorId CreateActor(ActorId id, Type type, string name, Event initialEvent, Actor creator, EventGroup eventGroup)839 {840 this.AssertExpectedCallerActor(creator, "CreateActor");841 Actor actor = this.CreateActor(id, type, name, creator, eventGroup);842 this.OnActorEventHandlerStarted(actor.Id);843 this.RunActorEventHandler(actor, initialEvent, true, null);844 return actor.Id;845 }846 /// <summary>847 /// Creates a new actor of the specified <see cref="Type"/> and name, using the specified848 /// unbound actor id, and passes the specified optional <see cref="Event"/>. This event849 /// can only be used to access its payload, and cannot be handled. The method returns only850 /// when the actor is initialized and the <see cref="Event"/> (if any) is handled.851 /// </summary>852 internal Task<ActorId> CreateActorAndExecuteAsync(ActorId id, Type type, string name, Event initialEvent = null,853 EventGroup eventGroup = null)854 {855 var creatorOp = this.Runtime.GetExecutingOperation<ActorOperation>();856 return this.CreateActorAndExecuteAsync(id, type, name, initialEvent, creatorOp?.Actor, eventGroup);857 }858 /// <summary>859 /// Creates a new <see cref="Actor"/> of the specified <see cref="Type"/>. The method860 /// returns only when the actor is initialized and the <see cref="Event"/> (if any)861 /// is handled.862 /// </summary>863 internal override async Task<ActorId> CreateActorAndExecuteAsync(ActorId id, Type type, string name, Event initialEvent,864 Actor creator, EventGroup eventGroup)865 {866 this.AssertExpectedCallerActor(creator, "CreateActorAndExecuteAsync");867 this.Assert(creator != null, "Only an actor can call 'CreateActorAndExecuteAsync': avoid calling " +868 "it directly from the test method; instead call it through a test driver actor.");869 Actor actor = this.CreateActor(id, type, name, creator, eventGroup);870 this.OnActorEventHandlerStarted(actor.Id);871 this.RunActorEventHandler(actor, initialEvent, true, creator);872 // Wait until the actor reaches quiescence.873 await creator.ReceiveEventAsync(typeof(QuiescentEvent), rev => (rev as QuiescentEvent).ActorId == actor.Id);874 return await Task.FromResult(actor.Id);875 }876 /// <summary>877 /// Creates a new <see cref="Actor"/> of the specified <see cref="Type"/>.878 /// </summary>879 internal override Actor CreateActor(ActorId id, Type type, string name, Actor creator, EventGroup eventGroup)880 {881 this.Assert(type.IsSubclassOf(typeof(Actor)), "Type '{0}' is not an actor.", type.FullName);882 // Using ulong.MaxValue because a Create operation cannot specify883 // the id of its target, because the id does not exist yet.884 this.Runtime.ScheduleNextOperation(creator?.Operation, SchedulingPointType.Create);885 this.ResetProgramCounter(creator);886 if (id is null)887 {888 id = this.CreateActorId(type, name);889 this.ActorIds.TryAdd(id, 0);890 }891 else892 {893 if (this.ActorMap.ContainsKey(id))894 {895 throw new InvalidOperationException($"An actor with id '{id.Value}' already exists.");896 }897 this.Assert(id.Runtime is null || id.Runtime == this, "Unbound actor id '{0}' was created by another runtime.", id.Value);898 this.Assert(id.Type == type.FullName, "Cannot bind actor id '{0}' of type '{1}' to an actor of type '{2}'.",899 id.Value, id.Type, type.FullName);900 id.Bind(this);901 }902 // If a group was not provided, inherit the current event group from the creator (if any).903 if (eventGroup is null && creator != null)904 {905 eventGroup = creator.EventGroup;906 }907 Actor actor = ActorFactory.Create(type);908 ActorOperation op = this.GetOrCreateActorOperation(id, actor);909 IEventQueue eventQueue = new MockEventQueue(actor);910 actor.Configure(this, id, op, eventQueue, eventGroup);911 actor.SetupEventHandlers();912 // This should always succeed, because it is either a new id or it has already passed913 // the assertion check, which still holds due to the schedule serialization during914 // systematic testing, but we still do the check defensively.915 if (!this.ActorMap.TryAdd(id, actor))916 {917 throw new InvalidOperationException($"An actor with id '{id.Value}' already exists.");918 }919 if (this.Configuration.IsActivityCoverageReported)920 {921 actor.ReportActivityCoverage(this.CoverageInfo);922 }923 if (actor is StateMachine)924 {925 this.LogWriter.LogCreateStateMachine(id, creator?.Id.Name, creator?.Id.Type);926 }927 else928 {929 this.LogWriter.LogCreateActor(id, creator?.Id.Name, creator?.Id.Type);930 }931 return actor;932 }933 /// <inheritdoc/>934 public override void SendEvent(ActorId targetId, Event initialEvent, EventGroup eventGroup = default, SendOptions options = null)935 {936 var senderOp = this.Runtime.GetExecutingOperation<ActorOperation>();937 this.SendEvent(targetId, initialEvent, senderOp?.Actor, eventGroup, options);938 }939 /// <inheritdoc/>940 public override Task<bool> SendEventAndExecuteAsync(ActorId targetId, Event initialEvent,941 EventGroup eventGroup = null, SendOptions options = null)942 {943 var senderOp = this.Runtime.GetExecutingOperation<ActorOperation>();944 return this.SendEventAndExecuteAsync(targetId, initialEvent, senderOp?.Actor, eventGroup, options);945 }946 /// <summary>947 /// Sends an asynchronous <see cref="Event"/> to an actor.948 /// </summary>949 internal override void SendEvent(ActorId targetId, Event e, Actor sender, EventGroup eventGroup, SendOptions options)950 {951 if (e is null)952 {953 string message = sender != null ?954 string.Format("{0} is sending a null event.", sender.Id.ToString()) :955 "Cannot send a null event.";956 this.Assert(false, message);957 }958 if (sender != null)959 {960 this.Assert(targetId != null, "{0} is sending event {1} to a null actor.", sender.Id, e);961 }962 else963 {964 this.Assert(targetId != null, "Cannot send event {1} to a null actor.", e);965 }966 this.AssertExpectedCallerActor(sender, "SendEvent");967 EnqueueStatus enqueueStatus = this.EnqueueEvent(targetId, e, sender, eventGroup, options, out Actor target);968 if (enqueueStatus is EnqueueStatus.EventHandlerNotRunning)969 {970 this.OnActorEventHandlerStarted(target.Id);971 this.RunActorEventHandler(target, null, false, null);972 }973 }974 /// <summary>975 /// Sends an asynchronous <see cref="Event"/> to an actor. Returns immediately if the target was976 /// already running. Otherwise blocks until the target handles the event and reaches quiescence.977 /// </summary>978 internal override async Task<bool> SendEventAndExecuteAsync(ActorId targetId, Event e, Actor sender,979 EventGroup eventGroup, SendOptions options)980 {981 this.Assert(sender is StateMachine, "Only an actor can call 'SendEventAndExecuteAsync': avoid " +982 "calling it directly from the test method; instead call it through a test driver actor.");983 this.Assert(e != null, "{0} is sending a null event.", sender.Id);984 this.Assert(targetId != null, "{0} is sending event {1} to a null actor.", sender.Id, e);985 this.AssertExpectedCallerActor(sender, "SendEventAndExecuteAsync");986 EnqueueStatus enqueueStatus = this.EnqueueEvent(targetId, e, sender, eventGroup, options, out Actor target);987 if (enqueueStatus is EnqueueStatus.EventHandlerNotRunning)988 {989 this.OnActorEventHandlerStarted(target.Id);990 this.RunActorEventHandler(target, null, false, sender as StateMachine);991 // Wait until the actor reaches quiescence.992 await (sender as StateMachine).ReceiveEventAsync(typeof(QuiescentEvent), rev => (rev as QuiescentEvent).ActorId == targetId);993 return true;994 }995 // EnqueueStatus.EventHandlerNotRunning is not returned by EnqueueEvent996 // (even when the actor was previously inactive) when the event e requires997 // no action by the actor (i.e., it implicitly handles the event).998 return enqueueStatus is EnqueueStatus.Dropped || enqueueStatus is EnqueueStatus.NextEventUnavailable;999 }1000 /// <summary>1001 /// Enqueues an event to the actor with the specified id.1002 /// </summary>1003 private EnqueueStatus EnqueueEvent(ActorId targetId, Event e, Actor sender, EventGroup eventGroup,1004 SendOptions options, out Actor target)1005 {1006 target = this.Runtime.GetOperationWithId<ActorOperation>(targetId.Value)?.Actor;1007 this.Assert(target != null,1008 "Cannot send event '{0}' to actor id '{1}' that is not bound to an actor instance.",1009 e.GetType().FullName, targetId.Value);1010 this.Runtime.ScheduleNextOperation(sender?.Operation, SchedulingPointType.Send);1011 this.ResetProgramCounter(sender as StateMachine);1012 // If no group is provided we default to passing along the group from the sender.1013 if (eventGroup is null && sender != null)1014 {1015 eventGroup = sender.EventGroup;1016 }1017 if (target.IsHalted)1018 {1019 Guid groupId = eventGroup is null ? Guid.Empty : eventGroup.Id;1020 this.LogWriter.LogSendEvent(targetId, sender?.Id.Name, sender?.Id.Type,1021 (sender as StateMachine)?.CurrentStateName ?? default, e, groupId, isTargetHalted: true);1022 this.Assert(options is null || !options.MustHandle,1023 "A must-handle event '{0}' was sent to {1} which has halted.", e.GetType().FullName, targetId);1024 this.HandleDroppedEvent(e, targetId);1025 return EnqueueStatus.Dropped;1026 }1027 EnqueueStatus enqueueStatus = this.EnqueueEvent(target, e, sender, eventGroup, options);1028 if (enqueueStatus == EnqueueStatus.Dropped)1029 {1030 this.HandleDroppedEvent(e, targetId);1031 }1032 return enqueueStatus;1033 }1034 /// <summary>1035 /// Enqueues an event to the actor with the specified id.1036 /// </summary>1037 private EnqueueStatus EnqueueEvent(Actor actor, Event e, Actor sender, EventGroup eventGroup, SendOptions options)1038 {1039 EventOriginInfo originInfo;1040 string stateName = null;1041 if (sender is StateMachine senderStateMachine)1042 {1043 originInfo = new EventOriginInfo(sender.Id, senderStateMachine.GetType().FullName,1044 NameResolver.GetStateNameForLogging(senderStateMachine.CurrentState));1045 stateName = senderStateMachine.CurrentStateName;1046 }1047 else if (sender is Actor senderActor)1048 {1049 originInfo = new EventOriginInfo(sender.Id, senderActor.GetType().FullName, string.Empty);1050 }1051 else1052 {1053 // Message comes from the environment.1054 originInfo = new EventOriginInfo(null, "Env", "Env");1055 }1056 EventInfo eventInfo = new EventInfo(e, originInfo)1057 {1058 MustHandle = options?.MustHandle ?? false,1059 Assert = options?.Assert ?? -11060 };1061 Guid opId = eventGroup is null ? Guid.Empty : eventGroup.Id;1062 this.LogWriter.LogSendEvent(actor.Id, sender?.Id.Name, sender?.Id.Type, stateName,1063 e, opId, isTargetHalted: false);1064 return actor.Enqueue(e, eventGroup, eventInfo);1065 }1066 /// <summary>1067 /// Runs a new asynchronous event handler for the specified actor.1068 /// This is a fire and forget invocation.1069 /// </summary>1070 /// <param name="actor">The actor that executes this event handler.</param>1071 /// <param name="initialEvent">Optional event for initializing the actor.</param>1072 /// <param name="isFresh">If true, then this is a new actor.</param>1073 /// <param name="syncCaller">Caller actor that is blocked for quiescence.</param>1074 private void RunActorEventHandler(Actor actor, Event initialEvent, bool isFresh, Actor syncCaller)1075 {1076 this.Runtime.TaskFactory.StartNew(async state =>1077 {1078 try1079 {1080 if (isFresh)1081 {1082 await actor.InitializeAsync(initialEvent);1083 }1084 await actor.RunEventHandlerAsync();1085 if (syncCaller != null)1086 {1087 this.EnqueueEvent(syncCaller, new QuiescentEvent(actor.Id), actor, actor.CurrentEventGroup, null);1088 }1089 if (!actor.IsHalted)1090 {1091 this.ResetProgramCounter(actor);1092 }1093 }1094 catch (Exception ex)1095 {1096 this.Runtime.ProcessUnhandledExceptionInOperation(actor.Operation, ex);1097 }1098 finally1099 {1100 if (actor.IsHalted)1101 {1102 this.ActorMap.TryRemove(actor.Id, out Actor _);1103 }1104 this.OnActorEventHandlerCompleted(actor.Id);1105 }1106 },1107 actor.Operation,1108 default,1109 this.Runtime.TaskFactory.CreationOptions | TaskCreationOptions.DenyChildAttach,1110 this.Runtime.TaskFactory.Scheduler);1111 }1112 /// <summary>1113 /// Creates a new timer that sends a <see cref="TimerElapsedEvent"/> to its owner actor.1114 /// </summary>1115 internal override IActorTimer CreateActorTimer(TimerInfo info, Actor owner)1116 {1117 var id = this.CreateActorId(typeof(MockStateMachineTimer));1118 this.CreateActor(id, typeof(MockStateMachineTimer), new TimerSetupEvent(info, owner, this.Configuration.TimeoutDelay));1119 return this.Runtime.GetOperationWithId<ActorOperation>(id.Value).Actor as MockStateMachineTimer;1120 }1121 /// <inheritdoc/>1122 public override EventGroup GetCurrentEventGroup(ActorId currentActorId)1123 {1124 var callerOp = this.Runtime.GetExecutingOperation<ActorOperation>();1125 this.Assert(callerOp != null && currentActorId == callerOp.Actor.Id,1126 "Trying to access the event group id of {0}, which is not the currently executing actor.",1127 currentActorId);1128 return callerOp.Actor.CurrentEventGroup;1129 }1130 /// <summary>1131 /// Returns a controlled nondeterministic boolean choice.1132 /// </summary>1133 internal override bool GetNondeterministicBooleanChoice(string callerName, string callerType)1134 {1135 var caller = this.Runtime.GetExecutingOperation<ActorOperation>()?.Actor;1136 if (caller is Actor callerActor)1137 {1138 this.IncrementActorProgramCounter(callerActor.Id);1139 }1140 return this.Runtime.GetNextNondeterministicBooleanChoice(callerName ?? caller?.Id.Name, callerType ?? caller?.Id.Type);1141 }1142 /// <summary>1143 /// Returns a controlled nondeterministic integer choice.1144 /// </summary>1145 internal override int GetNondeterministicIntegerChoice(int maxValue, string callerName, string callerType)1146 {1147 var caller = this.Runtime.GetExecutingOperation<ActorOperation>()?.Actor;1148 if (caller is Actor callerActor)1149 {1150 this.IncrementActorProgramCounter(callerActor.Id);1151 }1152 return this.Runtime.GetNextNondeterministicIntegerChoice(maxValue, callerName ?? caller?.Id.Name, callerType ?? caller?.Id.Type);1153 }1154 /// <inheritdoc/>1155 internal override void LogInvokedAction(Actor actor, MethodInfo action, string handlingStateName, string currentStateName) =>1156 this.LogWriter.LogExecuteAction(actor.Id, handlingStateName, currentStateName, action.Name);1157 /// <inheritdoc/>1158 [MethodImpl(MethodImplOptions.AggressiveInlining)]1159 internal override void LogEnqueuedEvent(Actor actor, Event e, EventGroup eventGroup, EventInfo eventInfo) =>1160 this.LogWriter.LogEnqueueEvent(actor.Id, e);1161 /// <inheritdoc/>1162 internal override void LogDequeuedEvent(Actor actor, Event e, EventInfo eventInfo, bool isFreshDequeue)1163 {1164 if (!isFreshDequeue)1165 {1166 // Skip the scheduling point, as this is the first dequeue of the event handler,1167 // to avoid unnecessary context switches.1168 this.Runtime.ScheduleNextOperation(actor.Operation, SchedulingPointType.Receive);1169 this.ResetProgramCounter(actor);1170 }1171 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1172 this.LogWriter.LogDequeueEvent(actor.Id, stateName, e);1173 }1174 /// <inheritdoc/>1175 internal override void LogDefaultEventDequeued(Actor actor)1176 {1177 this.Runtime.ScheduleNextOperation(actor.Operation, SchedulingPointType.Receive);1178 this.ResetProgramCounter(actor);1179 }1180 /// <inheritdoc/>1181 internal override void LogRaisedEvent(Actor actor, Event e, EventGroup eventGroup, EventInfo eventInfo)1182 {1183 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1184 this.LogWriter.LogRaiseEvent(actor.Id, stateName, e);1185 }1186 /// <inheritdoc/>1187 internal override void LogHandleRaisedEvent(Actor actor, Event e)1188 {1189 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1190 this.LogWriter.LogHandleRaisedEvent(actor.Id, stateName, e);1191 }1192 /// <inheritdoc/>1193 internal override void LogHandleHaltEvent(Actor actor, int inboxSize)1194 {1195 this.Runtime.ScheduleNextOperation(actor.Operation, SchedulingPointType.Halt);1196 this.LogWriter.LogHalt(actor.Id, inboxSize);1197 }1198 /// <inheritdoc/>1199 [MethodImpl(MethodImplOptions.AggressiveInlining)]1200 internal override void LogReceiveCalled(Actor actor) => this.AssertExpectedCallerActor(actor, "ReceiveEventAsync");1201 /// <inheritdoc/>1202 internal override void LogReceivedEvent(Actor actor, Event e, EventInfo eventInfo)1203 {1204 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1205 this.LogWriter.LogReceiveEvent(actor.Id, stateName, e, wasBlocked: true);1206 }1207 /// <inheritdoc/>1208 internal override void LogReceivedEventWithoutWaiting(Actor actor, Event e, EventInfo eventInfo)1209 {1210 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1211 this.LogWriter.LogReceiveEvent(actor.Id, stateName, e, wasBlocked: false);1212 this.Runtime.ScheduleNextOperation(actor.Operation, SchedulingPointType.Receive);1213 this.ResetProgramCounter(actor);1214 }1215 /// <inheritdoc/>1216 internal override void LogWaitEvent(Actor actor, IEnumerable<Type> eventTypes)1217 {1218 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1219 var eventWaitTypesArray = eventTypes.ToArray();1220 if (eventWaitTypesArray.Length is 1)1221 {1222 this.LogWriter.LogWaitEvent(actor.Id, stateName, eventWaitTypesArray[0]);1223 }1224 else1225 {1226 this.LogWriter.LogWaitEvent(actor.Id, stateName, eventWaitTypesArray);1227 }1228 this.Runtime.ScheduleNextOperation(actor.Operation, SchedulingPointType.Pause);1229 this.ResetProgramCounter(actor);1230 }1231 /// <inheritdoc/>1232 internal override void LogEventHandlerTerminated(Actor actor, DequeueStatus dequeueStatus)1233 {1234 string stateName = actor is StateMachine stateMachine ? stateMachine.CurrentStateName : null;1235 this.LogWriter.LogEventHandlerTerminated(actor.Id, stateName, dequeueStatus);1236 }1237 /// <inheritdoc/>1238 internal override void LogEnteredState(StateMachine stateMachine) =>1239 this.LogWriter.LogStateTransition(stateMachine.Id, stateMachine.CurrentStateName, isEntry: true);1240 /// <inheritdoc/>1241 internal override void LogExitedState(StateMachine stateMachine) =>1242 this.LogWriter.LogStateTransition(stateMachine.Id, stateMachine.CurrentStateName, isEntry: false);1243 /// <inheritdoc/>1244 internal override void LogPopState(StateMachine stateMachine)1245 {1246 this.AssertExpectedCallerActor(stateMachine, "Pop");1247 this.LogWriter.LogPopState(stateMachine.Id, default, stateMachine.CurrentStateName);1248 }1249 /// <inheritdoc/>1250 internal override void LogInvokedOnEntryAction(StateMachine stateMachine, MethodInfo action)1251 {1252 string stateName = stateMachine.CurrentStateName;1253 this.LogWriter.LogExecuteAction(stateMachine.Id, stateName, stateName, action.Name);1254 }1255 /// <inheritdoc/>1256 internal override void LogInvokedOnExitAction(StateMachine stateMachine, MethodInfo action)1257 {1258 string stateName = stateMachine.CurrentStateName;1259 this.LogWriter.LogExecuteAction(stateMachine.Id, stateName, stateName, action.Name);1260 }1261 /// <inheritdoc/>1262 [MethodImpl(MethodImplOptions.AggressiveInlining)]1263 internal override int GetActorProgramCounter(ActorId actorId) =>1264 this.ProgramCounterMap.GetOrAdd(actorId, 0);1265 /// <summary>1266 /// Increments the program counter of the specified actor.1267 /// </summary>1268 [MethodImpl(MethodImplOptions.AggressiveInlining)]1269 private void IncrementActorProgramCounter(ActorId actorId) =>1270 this.ProgramCounterMap.AddOrUpdate(actorId, 1, (id, value) => value + 1);1271 /// <summary>1272 /// Resets the program counter of the specified actor.1273 /// </summary>1274 private void ResetProgramCounter(Actor actor)1275 {1276 if (actor != null)1277 {1278 this.ProgramCounterMap.AddOrUpdate(actor.Id, 0, (id, value) => 0);1279 }1280 }1281 /// <inheritdoc/>1282#if !DEBUG1283 [DebuggerHidden]1284#endif1285 internal override void AssertExpectedCallerActor(Actor caller, string calledAPI)1286 {1287 if (caller is null)1288 {1289 return;1290 }1291 var op = this.Runtime.GetExecutingOperation<ActorOperation>();1292 if (op is null)1293 {1294 return;1295 }1296 this.Assert(op.Actor.Equals(caller), "{0} invoked {1} on behalf of {2}.",1297 op.Actor.Id, calledAPI, caller.Id);1298 }1299 /// <inheritdoc/>1300 protected override void Dispose(bool disposing)1301 {1302 if (disposing)1303 {1304 this.NameValueToActorId.Clear();1305 this.ProgramCounterMap.Clear();1306 foreach (var id in this.ActorIds)1307 {1308 // Unbind the runtime to avoid memory leaks if the user holds the id.1309 id.Key.Bind(null);1310 }...

Full Screen

Full Screen

MockEventQueue.cs

Source:MockEventQueue.cs Github

copy

Full Screen

...83 return EnqueueStatus.EventHandlerRunning;84 }85 this.OnEnqueueEvent(e, eventGroup, info);86 this.Queue.AddLast((e, eventGroup, info));87 if (info.Assert >= 0)88 {89 var eventCount = this.Queue.Count(val => val.e.GetType().Equals(e.GetType()));90 this.Assert(eventCount <= info.Assert,91 "There are more than {0} instances of '{1}' in the input queue of {2}.",92 info.Assert, info.EventName, this.Owner.Id);93 }94 if (!this.IsEventHandlerRunning)95 {96 if (this.TryDequeueEvent(true).e is null)97 {98 return EnqueueStatus.NextEventUnavailable;99 }100 else101 {102 this.IsEventHandlerRunning = true;103 return EnqueueStatus.EventHandlerNotRunning;104 }105 }106 return EnqueueStatus.EventHandlerRunning;107 }108 /// <inheritdoc/>109 public (DequeueStatus status, Event e, EventGroup eventGroup, EventInfo info) Dequeue()110 {111 // Try to get the raised event, if there is one. Raised events112 // have priority over the events in the inbox.113 if (this.RaisedEvent != default)114 {115 if (this.IsEventIgnored(this.RaisedEvent.e))116 {117 // TODO: should the user be able to raise an ignored event?118 // The raised event is ignored in the current state.119 this.OnIgnoreEvent(this.RaisedEvent.e, this.RaisedEvent.eventGroup, this.RaisedEvent.info);120 this.RaisedEvent = default;121 }122 else123 {124 (Event e, EventGroup eventGroup, EventInfo info) raisedEvent = this.RaisedEvent;125 this.RaisedEvent = default;126 return (DequeueStatus.Raised, raisedEvent.e, raisedEvent.eventGroup, raisedEvent.info);127 }128 }129 // Make sure this happens before a potential dequeue.130 var hasDefaultHandler = this.IsDefaultHandlerAvailable();131 // Try to dequeue the next event, if there is one.132 var (e, eventGroup, info) = this.TryDequeueEvent();133 if (e != null)134 {135 // Found next event that can be dequeued.136 return (DequeueStatus.Success, e, eventGroup, info);137 }138 // No event can be dequeued, so check if there is a default event handler.139 if (!hasDefaultHandler)140 {141 // There is no default event handler installed, so do not return an event.142 this.IsEventHandlerRunning = false;143 return (DequeueStatus.Unavailable, null, null, null);144 }145 // TODO: check op-id of default event.146 // A default event handler exists.147 string stateName = this.Owner is StateMachine stateMachine ?148 NameResolver.GetStateNameForLogging(stateMachine.CurrentState) : string.Empty;149 var eventOrigin = new EventOriginInfo(this.Owner.Id, this.Owner.GetType().FullName, stateName);150 return (DequeueStatus.Default, DefaultEvent.Instance, null, new EventInfo(DefaultEvent.Instance, eventOrigin));151 }152 /// <summary>153 /// Dequeues the next event and its metadata, if there is one available, else returns null.154 /// </summary>155 private (Event e, EventGroup eventGroup, EventInfo info) TryDequeueEvent(bool checkOnly = false)156 {157 // Try to dequeue the next event, if there is one.158 var node = this.Queue.First;159 while (node != null)160 {161 // Iterates through the events and metadata in the inbox.162 var nextNode = node.Next;163 var currentEvent = node.Value;164 if (this.IsEventIgnored(currentEvent.e))165 {166 if (!checkOnly)167 {168 // Removes an ignored event.169 this.Queue.Remove(node);170 this.OnIgnoreEvent(currentEvent.e, currentEvent.eventGroup, currentEvent.info);171 }172 node = nextNode;173 continue;174 }175 else if (this.IsEventDeferred(currentEvent.e))176 {177 // Skips a deferred event.178 this.OnDeferEvent(currentEvent.e, currentEvent.eventGroup, currentEvent.info);179 node = nextNode;180 continue;181 }182 if (!checkOnly)183 {184 this.Queue.Remove(node);185 }186 return currentEvent;187 }188 return default;189 }190 /// <inheritdoc/>191 public void RaiseEvent(Event e, EventGroup eventGroup)192 {193 string stateName = this.Owner is StateMachine stateMachine ?194 NameResolver.GetStateNameForLogging(stateMachine.CurrentState) : string.Empty;195 var eventOrigin = new EventOriginInfo(this.Owner.Id, this.Owner.GetType().FullName, stateName);196 var info = new EventInfo(e, eventOrigin);197 this.RaisedEvent = (e, eventGroup, info);198 this.OnRaiseEvent(e, eventGroup, info);199 }200 /// <inheritdoc/>201 public Task<Event> ReceiveEventAsync(Type eventType, Func<Event, bool> predicate = null)202 {203 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>204 {205 { eventType, predicate }206 };207 return this.ReceiveEventAsync(eventWaitTypes);208 }209 /// <inheritdoc/>210 public Task<Event> ReceiveEventAsync(params Type[] eventTypes)211 {212 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>();213 foreach (var type in eventTypes)214 {215 eventWaitTypes.Add(type, null);216 }217 return this.ReceiveEventAsync(eventWaitTypes);218 }219 /// <inheritdoc/>220 public Task<Event> ReceiveEventAsync(params Tuple<Type, Func<Event, bool>>[] events)221 {222 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>();223 foreach (var e in events)224 {225 eventWaitTypes.Add(e.Item1, e.Item2);226 }227 return this.ReceiveEventAsync(eventWaitTypes);228 }229 /// <summary>230 /// Waits for an event to be enqueued.231 /// </summary>232 private Task<Event> ReceiveEventAsync(Dictionary<Type, Func<Event, bool>> eventWaitTypes)233 {234 this.OnReceiveInvoked();235 (Event e, EventGroup eventGroup, EventInfo info) receivedEvent = default;236 var node = this.Queue.First;237 while (node != null)238 {239 // Dequeue the first event that the caller waits to receive, if there is one in the queue.240 if (eventWaitTypes.TryGetValue(node.Value.e.GetType(), out Func<Event, bool> predicate) &&241 (predicate is null || predicate(node.Value.e)))242 {243 receivedEvent = node.Value;244 this.Queue.Remove(node);245 break;246 }247 node = node.Next;248 }249 if (receivedEvent == default)250 {251 this.ReceiveCompletionSource = new TaskCompletionSource<Event>();252 this.EventWaitTypes = eventWaitTypes;253 this.OnWaitEvent(this.EventWaitTypes.Keys);254 return this.ReceiveCompletionSource.Task;255 }256 this.OnReceiveEventWithoutWaiting(receivedEvent.e, receivedEvent.eventGroup, receivedEvent.info);257 return Task.FromResult(receivedEvent.e);258 }259 /// <summary>260 /// Checks if the specified event is currently ignored.261 /// </summary>262 [MethodImpl(MethodImplOptions.AggressiveInlining)]263 protected virtual bool IsEventIgnored(Event e) => this.Owner.IsEventIgnored(e);264 /// <summary>265 /// Checks if the specified event is currently deferred.266 /// </summary>267 [MethodImpl(MethodImplOptions.AggressiveInlining)]268 protected virtual bool IsEventDeferred(Event e) => this.Owner.IsEventDeferred(e);269 /// <summary>270 /// Checks if a default handler is currently available.271 /// </summary>272 protected virtual bool IsDefaultHandlerAvailable()273 {274 bool result = this.Owner.IsDefaultHandlerInstalled();275 if (result)276 {277 this.Owner.Context.Runtime.ScheduleNextOperation(this.Owner.Operation, Runtime.SchedulingPointType.Receive);278 }279 return result;280 }281 /// <summary>282 /// Notifies the actor that an event has been enqueued.283 /// </summary>284 [MethodImpl(MethodImplOptions.AggressiveInlining)]285 protected virtual void OnEnqueueEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>286 this.Owner.OnEnqueueEvent(e, eventGroup, eventInfo);287 /// <summary>288 /// Notifies the actor that an event has been raised.289 /// </summary>290 [MethodImpl(MethodImplOptions.AggressiveInlining)]291 protected virtual void OnRaiseEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>292 this.Owner.OnRaiseEvent(e, eventGroup, eventInfo);293 /// <summary>294 /// Notifies the actor that it is waiting to receive an event of one of the specified types.295 /// </summary>296 [MethodImpl(MethodImplOptions.AggressiveInlining)]297 protected virtual void OnWaitEvent(IEnumerable<Type> eventTypes) => this.Owner.OnWaitEvent(eventTypes);298 /// <summary>299 /// Notifies the actor that an event it was waiting to receive has been enqueued.300 /// </summary>301 [MethodImpl(MethodImplOptions.AggressiveInlining)]302 protected virtual void OnReceiveEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>303 this.Owner.OnReceiveEvent(e, eventGroup, eventInfo);304 /// <summary>305 /// Notifies the actor that an event it was waiting to receive was already in the306 /// event queue when the actor invoked the receive statement.307 /// </summary>308 [MethodImpl(MethodImplOptions.AggressiveInlining)]309 protected virtual void OnReceiveEventWithoutWaiting(Event e, EventGroup eventGroup, EventInfo eventInfo) =>310 this.Owner.OnReceiveEventWithoutWaiting(e, eventGroup, eventInfo);311 /// <summary>312 /// Notifies the actor that <see cref="ReceiveEventAsync(Type[])"/> or one of its overloaded methods was invoked.313 /// </summary>314 [MethodImpl(MethodImplOptions.AggressiveInlining)]315 protected virtual void OnReceiveInvoked() => this.Owner.OnReceiveInvoked();316 /// <summary>317 /// Notifies the actor that an event has been ignored.318 /// </summary>319 [MethodImpl(MethodImplOptions.AggressiveInlining)]320 protected virtual void OnIgnoreEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) => this.Owner.OnIgnoreEvent(e);321 /// <summary>322 /// Notifies the actor that an event has been deferred.323 /// </summary>324 [MethodImpl(MethodImplOptions.AggressiveInlining)]325 protected virtual void OnDeferEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) => this.Owner.OnDeferEvent(e);326 /// <summary>327 /// Notifies the actor that an event has been dropped.328 /// </summary>329 [MethodImpl(MethodImplOptions.AggressiveInlining)]330 protected virtual void OnDropEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>331 this.Owner.OnDropEvent(e, eventInfo);332 /// <summary>333 /// Checks if the assertion holds, and if not, throws an exception.334 /// </summary>335 [MethodImpl(MethodImplOptions.AggressiveInlining)]336 protected virtual void Assert(bool predicate, string s, object arg0, object arg1, object arg2) =>337 this.Owner.Context.Assert(predicate, s, arg0, arg1, arg2);338 /// <inheritdoc/>339 public int GetCachedState()340 {341 unchecked342 {343 var hash = 19;344 foreach (var (_, _, info) in this.Queue)345 {346 hash = (hash * 31) + info.EventName.GetHashCode();347 if (info.HashedState != 0)348 {349 // Adds the user-defined hashed event state.350 hash = (hash * 31) + info.HashedState;351 }...

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.TestingServices;7{8 {9 static void Main(string[] args)10 {11 using (var runtime = RuntimeFactory.Create())12 {13 runtime.CreateTest(async () =>14 {15 var actor = runtime.CreateActor(typeof(MyActor));16 runtime.SendEvent(actor, new MyEvent());17 await actor;18 },19 configuration: GetConfiguration()).Wait();20 }21 }22 private static Configuration GetConfiguration()23 {24 var configuration = Configuration.Create().WithTestingIterations(1);25 configuration.TestingEngine = TestingEngine.InProcess;26 return configuration;27 }28 }29 {30 protected override Task OnInitializeAsync(Event initialEvent)31 {32 this.Assert(initialEvent is MyEvent);33 return Task.CompletedTask;34 }35 }36 {37 }38}39using System;40using System.Threading.Tasks;41using Microsoft.Coyote;42using Microsoft.Coyote.Actors;43using Microsoft.Coyote.Actors.Mocks;44using Microsoft.Coyote.TestingServices;45{46 {47 static void Main(string[] args)48 {49 using (var runtime = RuntimeFactory.Create())50 {51 runtime.CreateTest(async () =>52 {53 var actor = runtime.CreateActor(typeof(MyActor));54 runtime.SendEvent(actor, new MyEvent());55 await actor;56 },57 configuration: GetConfiguration()).Wait();58 }59 }60 private static Configuration GetConfiguration()61 {62 var configuration = Configuration.Create().WithTesting

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.Coyote;3using Microsoft.Coyote.Actors;4using Microsoft.Coyote.Actors.Mocks;5using Microsoft.Coyote.Specifications;6using Microsoft.Coyote.Tasks;7{8 {9 static void Main(string[] args)10 {11 var config = Configuration.Create();12 config.MaxSchedulingSteps = 100;13 config.SchedulingIterations = 10;14 config.TestingIterations = 1;15 var mock = new MockEventQueue(config);16 var e = new Event();17 mock.Assert(e, Occurs.Never);18 mock.Assert(e, Occurs.AtLeastOnce);19 mock.Assert(e, Occurs.AtLeast(5));20 mock.Assert(e, Occurs.Exactly(5));21 mock.Assert(e, Occurs.AtMostOnce);22 mock.Assert(e, Occurs.AtMost(5));23 }24 }25}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors.Mocks;2using Microsoft.Coyote.Runtime;3using Microsoft.Coyote.Specifications;4using System;5using System.Collections.Generic;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9{10 {11 static void Main(string[] args)12 {13 var config = Configuration.Create();14 var runtime = RuntimeFactory.Create(config);15 var queue = new MockEventQueue(runtime);16 var e = new Event();17 queue.Enqueue(e);18 queue.Assert(e);19 }20 }21}22using Microsoft.Coyote.Actors.Mocks;23using Microsoft.Coyote.Runtime;24using Microsoft.Coyote.Specifications;25using System;26using System.Collections.Generic;27using System.Linq;28using System.Text;29using System.Threading.Tasks;30{31 {32 static void Main(string[] args)33 {34 var config = Configuration.Create();35 var runtime = RuntimeFactory.Create(config);36 var queue = new MockEventQueue(runtime);37 var e = new Event();38 queue.Enqueue(e);39 queue.Assert(e);40 queue.Assert(e);41 }42 }43}44using Microsoft.Coyote.Actors.Mocks;45using Microsoft.Coyote.Runtime;46using Microsoft.Coyote.Specifications;47using System;48using System.Collections.Generic;49using System.Linq;50using System.Text;51using System.Threading.Tasks;52{53 {54 static void Main(string[] args)55 {56 var config = Configuration.Create();57 var runtime = RuntimeFactory.Create(config);58 var queue = new MockEventQueue(runtime);59 var e = new Event();60 queue.Enqueue(e);61 queue.Assert(e);62 queue.Assert(e);63 queue.Assert(e);64 }65 }66}67using Microsoft.Coyote.Actors.Mocks;68using Microsoft.Coyote.Runtime;69using Microsoft.Coyote.Specifications;70using System;71using System.Collections.Generic;72using System.Linq;73using System.Text;74using System.Threading.Tasks;75{

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Actors.Mocks;3using Microsoft.Coyote.TestingServices;4using System;5using System.Threading.Tasks;6{7 {8 static void Main(string[] args)9 {10 var runtime = RuntimeFactory.Create();11 var test = new ActorTest(runtime);12 var queue = new MockEventQueue();13 var id = ActorId.CreateRandom();

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1Assert.IsTrue(EventQueue.IsEmpty, "EventQueue is not empty");2Assert.IsFalse(EventQueue.IsEmpty, "EventQueue is empty");3Assert.IsTrue(EventQueue.Contains(e), "EventQueue does not contain Event e");4Assert.IsFalse(EventQueue.Contains(e), "EventQueue contains Event e");5Assert.IsTrue(EventQueue.Contains<Event>(), "EventQueue does not contain Event e");6Assert.IsFalse(EventQueue.Contains<Event>(), "EventQueue contains Event e");7Assert.IsTrue(EventQueue.Contains<Event>(e), "EventQueue does not contain Event e");8Assert.IsFalse(EventQueue.Contains<Event>(e), "EventQueue contains Event e");9Assert.IsTrue(EventQueue.Contains<Event>(e, (e1, e2) => e1.Equals(e2)), "EventQueue does not contain Event e");10Assert.IsFalse(EventQueue.Contains<Event>(e, (e1, e

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1{2 public int Value;3 public E(int value)4 {5 this.Value = value;6 }7}8{9 private int counter = 0;10 protected override async Task OnInitializeAsync(Event initialEvent)11 {12 this.SendEvent(this.Id, new E(1));13 this.SendEvent(this.Id, new E(2));14 this.SendEvent(this.Id, new E(3));15 await Task.CompletedTask;16 }17 protected override async Task OnEventAsync(Event e)18 {19 var ev = e as E;20 this.Assert(ev.Value == ++counter, "Expected {0}, but got {1}", counter, ev.Value);21 }22}23{24 public static void Main()25 {26 var runtime = RuntimeFactory.Create();27 runtime.CreateActor(typeof(A));28 runtime.Run();29 }30}31{32 public int Value;33 public E(int value)34 {35 this.Value = value;36 }37}38{39 private int counter = 0;40 protected override async Task OnInitializeAsync(Event initialEvent)41 {42 this.SendEvent(this.Id, new E(1));43 this.SendEvent(this.Id, new E(2));44 this.SendEvent(this.Id, new E(3));45 await Task.CompletedTask;46 }47 protected override async Task OnEventAsync(Event e)48 {49 var ev = e as E;50 this.Assert(ev.Value == ++counter, "Expected {0}, but got {1}", counter, ev.Value);51 }52}53{54 public static void Main()55 {56 var runtime = RuntimeFactory.Create();57 runtime.CreateActor(typeof(A));58 runtime.Run();59 }60}61{62 public int Value;63 public E(int value)64 {65 this.Value = value;66 }67}68{69 private int counter = 0;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful