Best Coyote code snippet using Microsoft.Coyote.Actors.Mocks.MockEventQueue.Enqueue
ActorExecutionContext.cs
Source:ActorExecutionContext.cs  
...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/>...MockEventQueue.cs
Source:MockEventQueue.cs  
...67            this.EventWaitTypes = new Dictionary<Type, Func<Event, bool>>();68            this.IsClosed = false;69        }70        /// <inheritdoc/>71        public EnqueueStatus Enqueue(Event e, EventGroup eventGroup, EventInfo info)72        {73            if (this.IsClosed)74            {75                return EnqueueStatus.Dropped;76            }77            if (this.EventWaitTypes.TryGetValue(e.GetType(), out Func<Event, bool> predicate) &&78                (predicate is null || predicate(e)))79            {80                this.EventWaitTypes.Clear();81                this.OnReceiveEvent(e, eventGroup, info);82                this.ReceiveCompletionSource.SetResult(e);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>...Enqueue
Using AI Code Generation
1using System;2using Microsoft.Coyote;3using Microsoft.Coyote.Actors;4using Microsoft.Coyote.Actors.Mocks;5{6    {7        static void Main(string[] args)8        {9            var mockRuntime = new MockRuntime();10            var actor = mockRuntime.CreateActor(typeof(Actor1));11            mockRuntime.EnqueueEvent(actor, new E1());12            mockRuntime.SendEvent(actor, new E2());13            mockRuntime.SendEvent(actor, new E3());14            mockRuntime.SendEvent(actor, new E4());15            mockRuntime.SendEvent(actor, new E5());16            mockRuntime.SendEvent(actor, new E6());17            mockRuntime.SendEvent(actor, new E7());18            mockRuntime.SendEvent(actor, new E8());19            mockRuntime.SendEvent(actor, new E9());20            mockRuntime.SendEvent(actor, new E10());21            mockRuntime.SendEvent(actor, new E11());22            mockRuntime.SendEvent(actor, new E12());23            mockRuntime.SendEvent(actor, new E13());24            mockRuntime.SendEvent(actor, new E14());25            mockRuntime.SendEvent(actor, new E15());26            mockRuntime.SendEvent(actor, new E16());27            mockRuntime.SendEvent(actor, new E17());28            mockRuntime.SendEvent(actor, new E18());29            mockRuntime.SendEvent(actor, new E19());30            mockRuntime.SendEvent(actor, new E20());31            mockRuntime.SendEvent(actor, new E21());32            mockRuntime.SendEvent(actor, new E22());33            mockRuntime.SendEvent(actor, new E23());34            mockRuntime.SendEvent(actor, new E24());35            mockRuntime.SendEvent(actor, new E25());36            mockRuntime.SendEvent(actor, new E26());37            mockRuntime.SendEvent(actor, new E27());38            mockRuntime.SendEvent(actor, new E28());39            mockRuntime.SendEvent(actor, new E29());40            mockRuntime.SendEvent(actor, new E30());41            mockRuntime.SendEvent(actor, new E31());42            mockRuntime.SendEvent(actor, new E32());43            mockRuntime.SendEvent(actor, new E33());44            mockRuntime.SendEvent(actor, new E34());45            mockRuntime.SendEvent(actor, new E35());46            mockRuntime.SendEvent(actor, new E36());47            mockRuntime.SendEvent(actor, new E37());48            mockRuntime.SendEvent(actor, new E38());49            mockRuntime.SendEvent(actor, new EEnqueue
Using AI Code Generation
1using System;2using System.Collections.Generic;3using System.Text;4using Microsoft.Coyote.Actors.Mocks;5{6    {7        static void Main(string[] args)8        {9            MockEventQueue queue = new MockEventQueue();10            queue.Enqueue(new Event());11            queue.Enqueue(new Event());12        }13    }14}15using System;16using System.Collections.Generic;17using System.Text;18using Microsoft.Coyote.Actors.Mocks;19{20    {21        static void Main(string[] args)22        {23            MockEventQueue queue = new MockEventQueue();24            queue.Enqueue(new Event());25            queue.Enqueue(new Event());26        }27    }28}29using System;30using System.Collections.Generic;31using System.Text;32using Microsoft.Coyote.Actors.Mocks;33{34    {35        static void Main(string[] args)36        {37            MockEventQueue queue = new MockEventQueue();38            queue.Enqueue(new Event());39            queue.Enqueue(new Event());40        }41    }42}43using System;44using System.Collections.Generic;45using System.Text;46using Microsoft.Coyote.Actors.Mocks;47{48    {49        static void Main(string[] args)50        {51            MockEventQueue queue = new MockEventQueue();52            queue.Enqueue(new Event());53            queue.Enqueue(new Event());54        }55    }56}57using System;58using System.Collections.Generic;59using System.Text;60using Microsoft.Coyote.Actors.Mocks;61{62    {63        static void Main(string[] args)64        {65            MockEventQueue queue = new MockEventQueue();66            queue.Enqueue(new Event());67            queue.Enqueue(new Event());68        }69    }70}71using System;72using System.Collections.Generic;Enqueue
Using AI Code Generation
1using Microsoft.Coyote.Actors.Mocks;2using Microsoft.Coyote.Actors;3using System;4using System.Threading.Tasks;5{6    {7        static void Main(string[] args)8        {9            var eventQueue = new MockEventQueue();10            eventQueue.Enqueue(new Event());11            Console.WriteLine("Hello World!");12        }13    }14}15Error CS0246 The type or namespace name 'Microsoft' could not be found (are you missing a using directive or an assembly reference?) 1.cs 2 Active16The Microsoft.Coyote.Actors.Mocks namespace is not available in the latest Coyote package. This is because the MockEventQueue type is no longer needed. You can use the CoyoteRuntime.CreateActorRuntime() method to create an actor runtime that can be used to create actors and send events to them. For example:17using Microsoft.Coyote.Actors;18using Microsoft.Coyote.Runtime;19var runtime = CoyoteRuntime.CreateActorRuntime();20var actor = runtime.CreateActor(typeof(MyActor));21runtime.SendEvent(actor.Id, new MyEvent());22using Microsoft.Coyote;23using Microsoft.Coyote.Actors;24using Microsoft.Coyote.Actors.Mocks;25using Microsoft.Coyote.Specifications;26using Microsoft.Coyote.Tasks;27using Microsoft.Coyote.Testing;28using System.Threading.Tasks;29using System;30using System.Collections.Generic;31using System.Linq;32using System.Text;33using System.Threading;34using System.Threading.Tasks;35{36    {37        public static void Main(string[] args)38        {39            var eventQueue = new MockEventQueue();40            eventQueue.Enqueue(new Event());41            Console.WriteLine("Hello World!");42        }43    }44    {45        [OnEventDoAction(typeof(MyEvent), nameof(HandleMyEvent))]46        {47        }48        private void HandleMyEvent(Event e)49        {Enqueue
Using AI Code Generation
1using Microsoft.Coyote.Actors.Mocks;2using System;3using System.Threading.Tasks;4{5    {6        public static async Task Main()7        {8            var queue = new MockEventQueue();9            queue.Enqueue(new MyEvent());10        }11    }12    public class MyEvent { }13}14using Microsoft.Coyote.Actors.Mocks;15using System;16using System.Threading.Tasks;17{18    {19        public static async Task Main()20        {21            var runtime = new MockActorRuntime();22            runtime.Enqueue(new MyEvent());23        }24    }25    public class MyEvent { }26}27using Microsoft.Coyote.Actors.Mocks;28using System;29using System.Threading.Tasks;30{31    {32        public static async Task Main()33        {34            var actor = new MockActor();35            actor.Enqueue(new MyEvent());36        }37    }38    public class MyEvent { }39}40using Microsoft.Coyote.Actors.Mocks;41using System;42using System.Threading.Tasks;43{44    {45        public static async Task Main()46        {47            var runtime = new MockActorRuntime();48            runtime.Enqueue(new MyEvent());49        }50    }51    public class MyEvent { }52}53using Microsoft.Coyote.Actors.Mocks;54using System;55using System.Threading.Tasks;56{57    {58        public static async Task Main()59        {60            var actor = new MockActor();61            actor.Enqueue(new MyEvent());62        }63    }64    public class MyEvent { }65}66using Microsoft.Coyote.Actors.Mocks;67using System;68using System.Threading.Tasks;69{70    {Enqueue
Using AI Code Generation
1{2    private readonly MockEventQueue queue;3    public MyActor(MockEventQueue queue)4    {5        this.queue = queue;6    }7    [OnEventDoAction(typeof(UnitEvent), nameof(OnUnitEvent))]8    private class Init : Event { }9    private void OnUnitEvent()10    {11        queue.Enqueue(new UnitEvent());12    }13}14{15    private readonly MockEventQueue queue;16    public MyActor(MockEventQueue queue)17    {18        this.queue = queue;19    }20    [OnEventDoAction(typeof(UnitEvent), nameof(OnUnitEvent))]21    private class Init : Event { }22    private void OnUnitEvent()23    {24        this.queue.Enqueue(new UnitEvent());25    }26}27{28    private readonly MockEventQueue queue;29    public MyActor(MockEventQueue queue)30    {31        this.queue = queue;32    }33    [OnEventDoAction(typeof(UnitEvent), nameof(OnUnitEvent))]34    private class Init : Event { }35    private void OnUnitEvent()36    {37        this.queue.Enqueue(new UnitEvent());38    }39}40{41    private readonly MockEventQueue queue;42    public MyActor(MockEventQueue queue)43    {44        this.queue = queue;45    }46    [OnEventDoAction(typeof(UnitEvent), nameof(OnUnitEvent))]47    private class Init : Event { }48    private void OnUnitEvent()49    {50        this.queue.Enqueue(new UnitEvent());51    }52}53{54    private readonly MockEventQueue queue;55    public MyActor(MockEventQueue queue)56    {57        this.queue = queue;58    }59    [OnEventDoAction(typeof(UnitEvent), nameof(OnUnitEvent))]Enqueue
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.Actors.Timers;7using Microsoft.Coyote.Specifications;8using Microsoft.Coyote.Tasks;9{10    {11        public static async Task Main(string[] args)12        {13            var actor = new ActorId(1);14            var eventQueue = new MockEventQueue();15            var e = new Event();16            eventQueue.Enqueue(actor, e);17        }18    }19}20using System;21using System.Threading.Tasks;22using Microsoft.Coyote;23using Microsoft.Coyote.Actors;24using Microsoft.Coyote.Actors.Mocks;25using Microsoft.Coyote.Actors.Timers;26using Microsoft.Coyote.Specifications;27using Microsoft.Coyote.Tasks;28{29    {30        public static async Task Main(string[] args)31        {32            var actor = new ActorId(1);33            var eventQueue = new MockEventQueue();34            var e = new Event();35            eventQueue.Enqueue(actor, e);36            var e1 = eventQueue.Dequeue(actor);37        }38    }39}40using System;41using System.Threading.Tasks;42using Microsoft.Coyote;43using Microsoft.Coyote.Actors;44using Microsoft.Coyote.Actors.Mocks;45using Microsoft.Coyote.Actors.Timers;46using Microsoft.Coyote.Specifications;47using Microsoft.Coyote.Tasks;48{49    {50        public static async Task Main(string[]Enqueue
Using AI Code Generation
1using Microsoft.Coyote.Actors.Mocks;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Actors.Timers;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 m = new MockEventQueue();14            m.Enqueue(new Halt());15            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1));16            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1));17            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2));18            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello");19            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3));20            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3), new ActorId(4));21            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3), new ActorId(4), new ActorId(5));22            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3), new ActorId(4), new ActorId(5), new ActorId(6));23            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3), new ActorId(4), new ActorId(5), new ActorId(6), new ActorId(7));24            m.Enqueue(new Halt(), new TimeSpan(0, 0, 1), new ActorId(1), new ActorId(2), "Hello", new ActorId(3), new ActorId(Enqueue
Using AI Code Generation
1{2    public static void Main(string[] args)3    {4        var config = Configuration.Create().WithTestingIterations(10);5        var test = TestingEngineFactory.Create(config, new Test1());6        test.Run();7    }8}9{10    public void Test()11    {12        var mock = new MockEventQueue();13        mock.Enqueue(new E());14        mock.Enqueue(new E());15        mock.Enqueue(new E());16        Assert.True(mock.TryDequeue(out var e));17        Assert.True(mock.TryDequeue(out e));18        Assert.True(mock.TryDequeue(out e));19        Assert.False(mock.TryDequeue(out e));20    }21}22{23}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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
