Best Coyote code snippet using Microsoft.Coyote.Samples.CoffeeMachineActors.DoorSafetyMonitor.OnEventUnhandledAsync
MockSensors.cs
Source:MockSensors.cs  
...229            }230            // Turn off the water.231            this.WaterPump = false;232        }233        protected override Task OnEventUnhandledAsync(Event e, string state)234        {235            this.Log.WriteLine("### Unhandled event {0} in state {1}", e.GetType().FullName, state);236            return base.OnEventUnhandledAsync(e, state);237        }238    }239    /// <summary>240    /// This Actor models is a mock implementation of the coffee grinder in the coffee machine.241    /// This is connected to the hopper containing beans, and the porta filter that stores the ground242    /// coffee before pouring a shot.243    /// </summary>244    [OnEventDoAction(typeof(RegisterClientEvent), nameof(OnRegisterClient))]245    [OnEventDoAction(typeof(ReadPortaFilterCoffeeLevelEvent), nameof(OnReadPortaFilterCoffeeLevel))]246    [OnEventDoAction(typeof(ReadHopperLevelEvent), nameof(OnReadHopperLevel))]247    [OnEventDoAction(typeof(GrinderButtonEvent), nameof(OnGrinderButton))]248    [OnEventDoAction(typeof(GrinderTimerEvent), nameof(MonitorGrinder))]249    [OnEventDoAction(typeof(DumpGrindsButtonEvent), nameof(OnDumpGrindsButton))]250    internal class MockCoffeeGrinder : Actor251    {252        private ActorId Client;253        private bool RunSlowly;254        private double PortaFilterCoffeeLevel;255        private double HopperLevel;256        private bool GrinderButton;257        private TimerInfo GrinderTimer;258        private readonly LogWriter Log = LogWriter.Instance;259        internal class GrinderTimerEvent : TimerElapsedEvent260        {261        }262        protected override Task OnInitializeAsync(Event initialEvent)263        {264            if (initialEvent is ConfigEvent ce)265            {266                this.RunSlowly = ce.RunSlowly;267            }268            // Since this is a mock, we randomly initialize the hopper level to some value269            // between 0 and 100% full.270            this.HopperLevel = this.RandomInteger(100);271            return base.OnInitializeAsync(initialEvent);272        }273        private void OnRegisterClient(Event e)274        {275            this.Client = ((RegisterClientEvent)e).Caller;276        }277        private void OnReadPortaFilterCoffeeLevel()278        {279            if (this.Client != null)280            {281                this.SendEvent(this.Client, new PortaFilterCoffeeLevelEvent(this.PortaFilterCoffeeLevel));282            }283        }284        private void OnGrinderButton(Event e)285        {286            var evt = e as GrinderButtonEvent;287            this.GrinderButton = evt.PowerOn;288            this.OnGrinderButtonChanged();289        }290        private void OnReadHopperLevel()291        {292            if (this.Client != null)293            {294                this.SendEvent(this.Client, new HopperLevelEvent(this.HopperLevel));295            }296        }297        private void OnGrinderButtonChanged()298        {299            if (this.GrinderButton)300            {301                this.Monitor<DoorSafetyMonitor>(new BusyEvent());302                // Should never turn on the grinder when there is no coffee to grind.303                if (this.HopperLevel <= 0)304                {305                    this.Assert(false, "Please do not turn on grinder if there are no beans in the hopper");306                }307                // Start monitoring the coffee level.308                this.GrinderTimer = this.StartPeriodicTimer(TimeSpan.FromSeconds(0.1), TimeSpan.FromSeconds(0.1), new GrinderTimerEvent());309            }310            else if (this.GrinderTimer != null)311            {312                this.StopTimer(this.GrinderTimer);313                this.GrinderTimer = null;314            }315        }316        private void MonitorGrinder()317        {318            // In each time interval the porta filter fills 10%. When it's full the grinder turns319            // off automatically, unless the hopper is empty in which case grinding does nothing!320            double hopperLevel = this.HopperLevel;321            if (hopperLevel > 0)322            {323                double level = this.PortaFilterCoffeeLevel;324                // Note: when running in production mode we run in real time, and it is fun325                // to watch the porta filter filling up. But in test mode this creates too326                // many async events to explore which makes the test slow. So in test mode327                // we short circuit this process and jump straight to the boundary conditions.328                if (!this.RunSlowly && level < 99)329                {330                    hopperLevel -= 98 - (int)level;331                    level = 99;332                }333                if (level < 100)334                {335                    level += 10;336                    this.PortaFilterCoffeeLevel = level;337                    if (this.Client != null)338                    {339                        this.SendEvent(this.Client, new PortaFilterCoffeeLevelEvent(this.PortaFilterCoffeeLevel));340                    }341                    if (level == 100)342                    {343                        // Turning off the grinder is automatic.344                        this.GrinderButton = false;345                        this.OnGrinderButtonChanged();346                    }347                }348                // And the hopper level drops by 0.1 percent.349                hopperLevel -= 1;350                this.HopperLevel = hopperLevel;351            }352            if (this.HopperLevel <= 0)353            {354                hopperLevel = 0;355                if (this.Client != null)356                {357                    this.SendEvent(this.Client, new HopperEmptyEvent());358                }359                if (this.GrinderTimer != null)360                {361                    this.StopTimer(this.GrinderTimer);362                    this.GrinderTimer = null;363                }364            }365        }366        protected override Task OnEventUnhandledAsync(Event e, string state)367        {368            this.Log.WriteLine("### Unhandled event {0} in state {1}", e.GetType().FullName, state);369            return base.OnEventUnhandledAsync(e, state);370        }371        private void OnDumpGrindsButton(Event e)372        {373            var evt = e as DumpGrindsButtonEvent;374            if (evt.PowerOn)375            {376                this.Monitor<DoorSafetyMonitor>(new BusyEvent());377                // This is a toggle button, in no time grinds are dumped (just for simplicity).378                this.PortaFilterCoffeeLevel = 0;379            }380        }381    }382}...OnEventUnhandledAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Samples.CoffeeMachineActors;6{7    {8        [OnEventDoAction(typeof(OpenDoorEvent), nameof(DoorOpened))]9        [OnEventDoAction(typeof(CloseDoorEvent), nameof(DoorClosed))]10        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(DoorClosed))]11        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(CoffeeReady))]12        [OnEventGotoState(typeof(DoorOpenedEvent), typeof(DoorOpened))]13        [OnEventGotoState(typeof(DoorClosedEvent), typeof(DoorClosed))]14        [OnEventDoAction(typeof(OpenDoorEvent), nameof(DoorOpened))]15        [OnEventDoAction(typeof(CloseDoorEvent), nameof(DoorClosed))]16        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(DoorClosed))]17        [OnEventGotoState(typeof(DoorOpenedEvent), typeof(DoorOpened))]18        [OnEventGotoState(typeof(DoorClosedEvent), typeof(DoorClosed))]19        [OnEventDoAction(typeof(OpenDoorEvent), nameof(DoorOpened))]20        [OnEventDoAction(typeof(CloseDoorEvent), nameof(DoorClosed))]21        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(DoorClosed))]22        [OnEventGotoState(typeof(DoorOpenedEvent), typeof(DoorOpened))]23        [OnEventGotoState(typeof(DoorClosedEvent), typeof(DoorClosed))]24        [OnEventDoAction(typeof(OpenDoorEvent), nameof(DoorOpened))]25        [OnEventDoAction(typeof(CloseDoorEvent), nameof(DoorClosed))]26        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(DoorClosed))]27        [OnEventGotoState(typeof(DoorOpenedEvent), typeof(DoorOpened))]28        [OnEventGotoState(typeof(DoorClosedEvent), typeof(DoorClosed))]29        [OnEventDoAction(typeof(OpenDoorEvent), nameof(DoorOpened))]30        [OnEventDoAction(typeof(CloseDoorEvent), nameof(DoorClosed))]31        [OnEventDoAction(typeof(CoffeeReadyEvent), nameof(DoorClosed))]32        [OnEventGotoState(typeof(DoorOpenedEvent), typeof(DoorOpened))]OnEventUnhandledAsync
Using AI Code Generation
1using Microsoft.Coyote;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Samples.CoffeeMachineActors;4using Microsoft.Coyote.Tasks;5using System;6using System.Threading.Tasks;7{8    {9        [OnEntry(nameof(InitOnEntry))]10        [OnEventDoAction(typeof(OpenDoor), nameof(OpenDoorAction))]11        [OnEventDoAction(typeof(CloseDoor), nameof(CloseDoorAction))]12        [OnEventDoAction(typeof(OnEventUnhandledAsync), nameof(OnEventUnhandledAsync))]13        class Init : MonitorState { }14        void InitOnEntry()15        {16            this.RaiseGotoStateEvent<Init>();17        }18        void OpenDoorAction()19        {20            this.RaiseGotoStateEvent<Open>();21        }22        void CloseDoorAction()23        {24            this.RaiseGotoStateEvent<Close>();25        }26        void OnEventUnhandledAsync()27        {28            this.Assert(false, "Door safety property violated!");29        }30        [OnEntry(nameof(OpenOnEntry))]31        [OnEventGotoState(typeof(CloseDoor), typeof(Close))]32        [OnEventDoAction(typeof(OpenDoor), nameof(OpenDoorAction))]33        class Open : MonitorState { }34        void OpenOnEntry()35        {36            this.Assert(false, "Door safety property violated!");37        }38        [OnEntry(nameof(CloseOnEntry))]39        [OnEventGotoState(typeof(OpenDoor), typeof(Open))]40        [OnEventDoAction(typeof(CloseDoor), nameof(CloseDoorAction))]41        class Close : MonitorState { }42        void CloseOnEntry()43        {44            this.Assert(false, "Door safety property violated!");45        }46    }47}48using Microsoft.Coyote;49using Microsoft.Coyote.Actors;50using Microsoft.Coyote.Samples.CoffeeMachineActors;51using Microsoft.Coyote.Tasks;52using System;53using System.Threading.Tasks;54{55    {OnEventUnhandledAsync
Using AI Code Generation
1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Specifications;6using Microsoft.Coyote.Samples.CoffeeMachineActors;7{8    static async Task Main(string[] args)9    {10        var config = Configuration.Create();11        config.SchedulingIterations = 100;12        config.SchedulingSeed = 0;13        config.SchedulingStrategy = SchedulingStrategy.DFS;14        config.SchedulingMaxSteps = 100;15        config.Verbose = 2;16        var runtime = RuntimeFactory.Create(config);17        var monitor = runtime.CreateActor(typeof(DoorSafetyMonitor));18        var coffeeMachine = runtime.CreateActor(typeof(CoffeeMachine));19        runtime.SendEvent(monitor, new DoorOpenEvent());20        runtime.SendEvent(monitor, new DoorCloseEvent());21        runtime.SendEvent(monitor, new DoorOpenEvent());22        runtime.SendEvent(monitor, new DoorCloseEvent());23        runtime.SendEvent(monitor, new DoorOpenEvent());24        runtime.SendEvent(monitor, new DoorCloseEvent());25        runtime.SendEvent(monitor, new DoorOpenEvent());26        runtime.SendEvent(monitor, new DoorCloseEvent());27        runtime.SendEvent(monitor, new DoorOpenEvent());28        runtime.SendEvent(monitor, new DoorCloseEvent());29        Console.WriteLine("Press any key to exit...");30        Console.ReadKey();31    }32}33using System;34using System.Threading.Tasks;35using Microsoft.Coyote;36using Microsoft.Coyote.Actors;37using Microsoft.Coyote.Specifications;38using Microsoft.Coyote.Samples.CoffeeMachineActors;39{40    static async Task Main(string[] args)41    {42        var config = Configuration.Create();43        config.SchedulingIterations = 100;44        config.SchedulingSeed = 0;45        config.SchedulingStrategy = SchedulingStrategy.DFS;46        config.SchedulingMaxSteps = 100;47        config.Verbose = 2;48        var runtime = RuntimeFactory.Create(config);49        var monitor = runtime.CreateActor(typeof(DoorSafetyMonitor));50        var coffeeMachine = runtime.CreateActor(typeof(CoffeeMachine));51        runtime.SendEvent(monitor, new DoorOpenEvent());52        runtime.SendEvent(monitor, new DoorCloseEvent());53        runtime.SendEvent(monitor, newOnEventUnhandledAsync
Using AI Code Generation
1using Microsoft.Coyote;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Samples.CoffeeMachineActors;4using Microsoft.Coyote.Tasks;5using System;6using System.Threading.Tasks;7{8    {9        private bool doorIsOpen = false;10        [OnEventDoAction(typeof(DoorOpenedEvent), nameof(OnDoorOpened))]11        [OnEventDoAction(typeof(DoorClosedEvent), nameof(OnDoorClosed))]12        private class Init : State { }13        private void OnDoorOpened(Event e)14        {15            this.doorIsOpen = true;16        }17        private void OnDoorClosed(Event e)18        {19            this.doorIsOpen = false;20        }21        [OnEventGotoState(typeof(DrinkReadyEvent), typeof(DrinkReady))]22        private class MonitorDoorOpen : State { }23        [OnEventDoAction(typeof(DrinkReadyEvent), nameof(OnDrinkReady))]24        {25            protected override Task OnEventUnhandledAsync(Event e)26            {27                if (this.doorIsOpen)28                {29                    this.Assert(false, "Door is open while drink is ready!");30                }31                return Task.CompletedTask;32            }33        }34    }35}36using Microsoft.Coyote;37using Microsoft.Coyote.Actors;38using Microsoft.Coyote.Samples.CoffeeMachineActors;39using Microsoft.Coyote.Tasks;40using System;41using System.Threading.Tasks;42{43    {44        private bool doorIsOpen = false;45        [OnEventDoAction(typeof(DoorOpenedEvent), nameof(OnDoorOpened))]46        [OnEventDoAction(typeof(DoorClosedEvent), nameof(OnDoorClosed))]47        private class Init : State { }48        private void OnDoorOpened(Event e)49        {50            this.doorIsOpen = true;51        }52        private void OnDoorClosed(Event e)53        {54            this.doorIsOpen = false;55        }OnEventUnhandledAsync
Using AI Code Generation
1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Samples.CoffeeMachineActors;3{4    {5        private bool doorClosed = true;6        [OnEventDoAction(typeof(DoorOpenedEvent), nameof(DoorOpened))]7        [OnEventDoAction(typeof(DoorClosedEvent), nameof(DoorClosed))]8        [OnEventDoAction(typeof(StartBrewingEvent), nameof(StartBrewing))]9        [OnEventDoAction(typeof(BrewingFinishedEvent), nameof(BrewingFinished))]10        private class Init : State { }11        private void DoorOpened()12        {13            this.doorClosed = false;14        }15        private void DoorClosed()16        {17            this.doorClosed = true;18        }19        private void StartBrewing()20        {21            if (!this.doorClosed)22            {23                this.Assert(false, "Door not closed!");24            }25        }26        private void BrewingFinished()27        {28            if (!this.doorClosed)29            {30                this.Assert(false, "Door not closed!");31            }32        }33    }34}35using Microsoft.Coyote.Actors;36using Microsoft.Coyote.Samples.CoffeeMachineActors;37{38    {39        private readonly ActorId door;40        private readonly ActorId brewing;41        private readonly ActorId safetyMonitor;42        public CoffeeMachineActor(ActorId door, ActorId brewing, ActorId safetyMonitor)43        {44            this.door = door;45            this.brewing = brewing;46            this.safetyMonitor = safetyMonitor;47        }48        [OnEventDoAction(typeof(StartBrewingEvent), nameof(StartBrewing))]49        [OnEventDoAction(typeof(BrewingFinishedEvent), nameof(BrewingFinished))]50        private class Init : State { }51        private void StartBrewing()52        {53            this.SendEvent(this.door, new DoorOpenEvent());54            this.SendEvent(this.brewing, new StartBrewingEvent());55            this.SendEvent(this.safetyMonitor, new StartBrewOnEventUnhandledAsync
Using AI Code Generation
1{2    {3        [OnEventDoAction(typeof(DoorClosed), nameof(HandleDoorClosed))]4        [OnEventDoAction(typeof(DoorOpened), nameof(HandleDoorOpened))]5        [OnEventDoAction(typeof(CoffeeMachineStarted), nameof(HandleCoffeeMachineStarted))]6        [OnEventDoAction(typeof(CoffeeMachineStopped), nameof(HandleCoffeeMachineStopped))]7        class Init : MonitorState { }8        void HandleDoorClosed()9        {10            this.Assert(this.Machine.IsCoffeeMachineRunning == false, "Door closed while coffee machine running");11        }12        void HandleDoorOpened()13        {14            this.Assert(this.Machine.IsCoffeeMachineRunning == false, "Door opened while coffee machine running");15        }16        void HandleCoffeeMachineStarted()17        {18            this.Assert(this.Machine.IsDoorOpen == false, "Coffee machine started while door open");19        }20        void HandleCoffeeMachineStopped()21        {22            this.Assert(this.Machine.IsDoorOpen == false, "Coffee machine stopped while door open");23        }24    }25}26{27    {28        [OnEventDoAction(typeof(DoorClosed), nameof(HandleDoorClosed))]29        [OnEventDoAction(typeof(DoorOpened), nameof(HandleDoorOpened))]30        [OnEventDoAction(typeof(CoffeeMachineStarted), nameof(HandleCoffeeMachineStarted))]31        [OnEventDoAction(typeof(CoffeeMachineStopped), nameof(HandleCoffeeMachineStopped))]32        class Init : MonitorState { }33        void HandleDoorClosed()34        {35            this.Assert(this.Machine.IsCoffeeMachineRunning == false, "Door closed while coffee machine running");36        }37        void HandleDoorOpened()38        {39            this.Assert(this.Machine.IsCoffeeMachineRunning == false, "Door opened while coffee machine running");40        }41        void HandleCoffeeMachineStarted()42        {43            this.Assert(this.Machine.IsDoorOpen == false, "Coffee machine started while door open");44        }45        void HandleCoffeeMachineStopped()46        {47            this.Assert(this.Machine.IsDoorLearn 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!!
