How to use Stop method of Microsoft.Coyote.Samples.CoffeeMachineTasks.ControlledTimer class

Best Coyote code snippet using Microsoft.Coyote.Samples.CoffeeMachineTasks.ControlledTimer.Stop

MockSensors.cs

Source:MockSensors.cs Github

copy

Full Screen

...100 this.WaterHeaterTimer = new ControlledTimer("WaterHeaterTimer", TimeSpan.FromSeconds(0.1), this.MonitorWaterTemperature);101 }102 public Task TerminateAsync()103 {104 StopTimer(this.WaterHeaterTimer);105 StopTimer(this.CoffeeLevelTimer);106 StopTimer(this.ShotTimer);107 return Task.CompletedTask;108 }109 public async Task<bool> GetPowerSwitchAsync()110 {111 // to model real async behavior we insert a delay here.112 await Task.Delay(1);113 return this.PowerOn;114 }115 public async Task<double> GetWaterLevelAsync()116 {117 await Task.Delay(1);118 return this.WaterLevel;119 }120 public async Task<double> GetHopperLevelAsync()121 {122 await Task.Delay(1);123 return this.HopperLevel;124 }125 public async Task<double> GetWaterTemperatureAsync()126 {127 await Task.Delay(1);128 return this.WaterTemperature;129 }130 public async Task<double> GetPortaFilterCoffeeLevelAsync()131 {132 await Task.Delay(1);133 return this.PortaFilterCoffeeLevel;134 }135 public async Task<bool> GetReadDoorOpenAsync()136 {137 await Task.Delay(1);138 return this.DoorOpen;139 }140 public async Task SetPowerSwitchAsync(bool value)141 {142 await Task.Delay(1);143 // NOTE: you should not use C# locks that interact with Tasks (like Task.Run) because144 // it can result in deadlocks, instead use the Coyote AsyncLock as follows.145 using (await this.Lock.AcquireAsync())146 {147 this.PowerOn = value;148 if (!this.PowerOn)149 {150 // Master power override then also turns everything else off for safety!151 this.WaterHeaterButton = false;152 this.GrinderButton = false;153 this.ShotButton = false;154 StopTimer(this.CoffeeLevelTimer);155 this.CoffeeLevelTimer = null;156 StopTimer(this.ShotTimer);157 this.ShotTimer = null;158 }159 }160 }161 public async Task SetWaterHeaterButtonAsync(bool value)162 {163 await Task.Delay(1);164 using (await this.Lock.AcquireAsync())165 {166 this.WaterHeaterButton = value;167 // Should never turn on the heater when there is no water to heat.168 if (this.WaterHeaterButton && this.WaterLevel <= 0)169 {170 Specification.Assert(false, "Please do not turn on heater if there is no water");171 }172 }173 }174 public async Task SetGrinderButtonAsync(bool value)175 {176 await Task.Delay(1);177 await this.OnGrinderButtonChanged(value);178 }179 private async Task OnGrinderButtonChanged(bool value)180 {181 using (await this.Lock.AcquireAsync())182 {183 this.GrinderButton = value;184 if (this.GrinderButton)185 {186 // Should never turn on the grinder when there is no coffee to grind.187 if (this.HopperLevel <= 0)188 {189 Specification.Assert(false, "Please do not turn on grinder if there are no beans in the hopper");190 }191 }192 if (value && this.CoffeeLevelTimer == null)193 {194 // Start monitoring the coffee level.195 this.CoffeeLevelTimer = new ControlledTimer("CoffeeLevelTimer", TimeSpan.FromSeconds(0.1), this.MonitorGrinder);196 }197 else if (!value && this.CoffeeLevelTimer != null)198 {199 StopTimer(this.CoffeeLevelTimer);200 this.CoffeeLevelTimer = null;201 }202 }203 }204 public async Task SetShotButtonAsync(bool value)205 {206 await Task.Delay(1);207 using (await this.Lock.AcquireAsync())208 {209 this.ShotButton = value;210 if (this.ShotButton)211 {212 // Should never turn on the make shots button when there is no water.213 if (this.WaterLevel <= 0)214 {215 Specification.Assert(false, "Please do not turn on shot maker if there is no water");216 }217 }218 if (value && this.ShotTimer == null)219 {220 // Start monitoring the coffee level.221 this.ShotTimer = new ControlledTimer("ShotTimer", TimeSpan.FromSeconds(1), this.MonitorShot);222 }223 else if (!value && this.ShotTimer != null)224 {225 StopTimer(this.ShotTimer);226 this.ShotTimer = null;227 }228 }229 }230 public async Task SetDumpGrindsButtonAsync(bool value)231 {232 await Task.Delay(1);233 if (value)234 {235 // This is a toggle button, in no time grinds are dumped (just for simplicity).236 this.PortaFilterCoffeeLevel = 0;237 }238 }239 private void MonitorWaterTemperature()240 {241 double temp = this.WaterTemperature;242 if (this.WaterHeaterButton)243 {244 // Note: when running in production mode we run forever, and it is fun to245 // watch the water heat up and cool down. But in test mode this creates too246 // many async events to explore which makes the test slow. So in test mode247 // we short circuit this process and jump straight to the boundary conditions.248 if (!this.RunSlowly && temp < 99)249 {250 temp = 99;251 }252 // Every time interval the temperature increases by 10 degrees up to 100 degrees.253 if (temp < 100)254 {255 temp = (int)temp + 10;256 this.WaterTemperature = temp;257 this.WaterTemperatureChanged?.Invoke(this, this.WaterTemperature);258 }259 else260 {261 this.WaterHot?.Invoke(this, true);262 }263 }264 else265 {266 // Then it is cooling down to room temperature, more slowly.267 if (temp > 70)268 {269 temp -= 0.1;270 this.WaterTemperature = temp;271 }272 }273 // Start another callback.274 this.WaterHeaterTimer = new ControlledTimer("WaterHeaterTimer", TimeSpan.FromSeconds(0.1), this.MonitorWaterTemperature);275 }276 private void MonitorGrinder()277 {278 // Every time interval the porta filter fills 10%. When it's full the grinder turns off279 // automatically, unless the hopper is empty in which case grinding does nothing!280 Task.Run(async () =>281 {282 bool changed = false;283 bool notifyEmpty = false;284 bool turnOffGrinder = false;285 using (await this.Lock.AcquireAsync())286 {287 double hopperLevel = this.HopperLevel;288 if (hopperLevel > 0)289 {290 double level = this.PortaFilterCoffeeLevel;291 // Note: when running in production mode we run in real time, and it is fun292 // to watch the porta filter filling up. But in test mode this creates too293 // many async events to explore which makes the test slow. So in test mode294 // we short circuit this process and jump straight to the boundary conditions.295 if (!this.RunSlowly && level < 99)296 {297 hopperLevel -= 98 - (int)level;298 this.Log.WriteLine("### HopperLevel: RunSlowly = {0}, level = {1}", this.RunSlowly, hopperLevel);299 level = 99;300 }301 if (level < 100)302 {303 level += 10;304 this.PortaFilterCoffeeLevel = level;305 changed = true;306 if (level >= 100)307 {308 turnOffGrinder = true;309 }310 }311 // And the hopper level drops by 0.1 percent.312 hopperLevel -= 1;313 this.HopperLevel = hopperLevel;314 }315 if (this.HopperLevel <= 0)316 {317 hopperLevel = 0;318 notifyEmpty = true;319 StopTimer(this.CoffeeLevelTimer);320 this.CoffeeLevelTimer = null;321 }322 }323 if (turnOffGrinder)324 {325 // Turning off the grinder is automatic.326 await this.OnGrinderButtonChanged(false);327 }328 // Event callbacks should not be inside the lock otherwise we could get deadlocks.329 if (notifyEmpty && this.HopperEmpty != null)330 {331 this.HopperEmpty(this, true);332 }333 if (changed && this.PortaFilterCoffeeLevelChanged != null)334 {335 this.PortaFilterCoffeeLevelChanged(this, this.PortaFilterCoffeeLevel);336 }337 if (this.HopperLevel <= 0 && this.HopperEmpty != null)338 {339 this.HopperEmpty(this, true);340 }341 // Start another callback.342 this.CoffeeLevelTimer = new ControlledTimer("WaterHeaterTimer", TimeSpan.FromSeconds(0.1), this.MonitorGrinder);343 });344 }345 private void MonitorShot()346 {347 Task.Run(async () =>348 {349 // One second of running water completes the shot.350 using (await this.Lock.AcquireAsync())351 {352 this.WaterLevel -= 1;353 // Turn off the water.354 this.ShotButton = false;355 this.ShotTimer = null;356 }357 // Event callbacks should not be inside the lock otherwise we could get deadlocks.358 if (this.WaterLevel > 0)359 {360 this.ShotComplete?.Invoke(this, true);361 }362 else363 {364 this.WaterEmpty?.Invoke(this, true);365 }366 });367 }368 private static void StopTimer(ControlledTimer timer)369 {370 if (timer != null)371 {372 timer.Stop();373 }374 }375 }376}...

Full Screen

Full Screen

FailoverDriver.cs

Source:FailoverDriver.cs Github

copy

Full Screen

...59 {60 // Setup a timer to randomly kill the coffee machine. When the timer fires we61 // will restart the coffee machine and this is testing that the machine can62 // recover gracefully when that happens.63 this.HaltTimer = new ControlledTimer("HaltTimer", TimeSpan.FromSeconds(this.RandomGenerator.NextInteger(7) + 1), new Action(this.OnStopTest));64 // Request a coffee!65 var shots = this.RandomGenerator.NextInteger(3) + 1;66 error = await this.CoffeeMachine.MakeCoffeeAsync(shots);67 }68 if (string.Compare(error, "<halted>", StringComparison.OrdinalIgnoreCase) == 0)69 {70 // Then OnStopTest did it's thing, so it is time to create new coffee machine.71 this.Log.WriteWarning("CoffeeMachine is halted.");72 halted = true;73 }74 else if (!string.IsNullOrEmpty(error))75 {76 this.Log.WriteWarning("CoffeeMachine reported an error.");77 // No point trying to make more coffee.78 this.RunForever = false;79 this.Iterations = 10;80 }81 else82 {83 // In this case we let the same CoffeeMachine continue on then.84 this.Log.WriteLine("CoffeeMachine completed the job.");85 }86 this.Iterations++;87 }88 // Shutdown the sensors because test is now complete.89 this.Log.WriteLine("Test is complete, press ENTER to continue...");90 await this.Sensors.TerminateAsync();91 }92 internal void OnStopTest()93 {94 if (!this.IsInitialized)95 {96 // Not ready!97 return;98 }99 if (this.HaltTimer != null)100 {101 this.HaltTimer.Stop();102 this.HaltTimer = null;103 }104 // Halt the CoffeeMachine. HaltEvent is async and we must ensure the CoffeeMachine105 // is really halted before we create a new one because MockSensors will get confused106 // if two CoffeeMachines are running at the same time. So we've implemented a terminate107 // handshake here. We send event to the CoffeeMachine to terminate, and it sends back108 // a HaltedEvent when it really has been halted.109 this.Log.WriteLine("forcing termination of CoffeeMachine.");110 Task.Run(this.CoffeeMachine.TerminateAsync);111 }112 }113}...

Full Screen

Full Screen

ControlledTimer.cs

Source:ControlledTimer.cs Github

copy

Full Screen

...14 private readonly CancellationTokenSource Source = new CancellationTokenSource();15 private readonly TimeSpan StartDelay;16 private readonly TimeSpan? Interval;17 private readonly Action Handler;18 private bool Stopped;19 private readonly string Name;20 public ControlledTimer(string name, TimeSpan startDelay, TimeSpan interval, Action handler)21 {22 this.Name = name;23 this.StartDelay = startDelay;24 this.Interval = interval;25 this.Handler = handler;26 this.StartTimer(startDelay);27 }28 public ControlledTimer(string name, TimeSpan dueTime, Action handler)29 {30 this.Name = name;31 this.StartDelay = dueTime;32 this.Handler = handler;33 this.StartTimer(dueTime);34 }35 private void StartTimer(TimeSpan dueTime)36 {37 Task.Run(async () =>38 {39 await Task.Delay(dueTime, this.Source.Token);40 this.OnTick();41 });42 }43 private void OnTick()44 {45 if (!this.Stopped)46 {47 this.Handler();48 if (this.Interval.HasValue)49 {50 this.StartTimer(this.Interval.Value);51 }52 }53 }54 public void Stop()55 {56 this.Stopped = true;57 this.Source.Cancel();58 }59 public override string ToString()60 {61 return this.Name;62 }63 }64}...

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Samples.CoffeeMachineTasks;5{6 {7 public static void Main(string[] args)8 {9 ControlledTimer timer = new ControlledTimer(1000, new TimerCallback(OnTimer), null);10 timer.Stop();11 Console.WriteLine("Press Enter to exit the application.");12 Console.ReadLine();13 }14 private static void OnTimer(object state)15 {16 Console.WriteLine("Timer fired.");17 }18 }19}20using System;21using System.Threading.Tasks;22using Microsoft.Coyote;23using Microsoft.Coyote.Samples.CoffeeMachineTasks;24{25 {26 public static void Main(string[] args)27 {28 ControlledTimer timer = new ControlledTimer(1000, new TimerCallback(OnTimer), null);29 timer.Stop();30 Console.WriteLine("Press Enter to exit the application.");31 Console.ReadLine();32 timer.Dispose();33 }34 private static void OnTimer(object state)35 {36 Console.WriteLine("Timer fired.");37 }38 }39}

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Samples.CoffeeMachineTasks;5using Microsoft.Coyote.Tasks;6{7 {8 static async Task Main(string[] args)9 {10 await Runtime.RunAsync(async () =>11 {12 var timer = new ControlledTimer();13 var t = Task.Run(async () =>14 {15 await timer.Start(TimeSpan.FromSeconds(2));16 Console.WriteLine("Timer expired");17 });18 await Task.Delay(1000);19 timer.Stop();20 await t;21 });22 }23 }24}25using System;26using System.Threading.Tasks;27using Microsoft.Coyote;28using Microsoft.Coyote.Samples.CoffeeMachineTasks;29using Microsoft.Coyote.Tasks;30{31 {32 static async Task Main(string[] args)33 {34 await Runtime.RunAsync(async () =>35 {36 var timer = new ControlledTimer();37 var t = Task.Run(async () =>38 {39 await timer.Start(TimeSpan.FromSeconds(2));40 Console.WriteLine("Timer expired");41 });42 await Task.Delay(1000);43 timer.Stop();44 await t;45 });46 }47 }48}49using System;50using System.Threading.Tasks;51using Microsoft.Coyote;52using Microsoft.Coyote.Samples.CoffeeMachineTasks;53using Microsoft.Coyote.Tasks;54{55 {56 static async Task Main(string[] args)57 {58 await Runtime.RunAsync(async () =>59 {60 var timer = new ControlledTimer();61 var t = Task.Run(async () =>62 {63 await timer.Start(TimeSpan.FromSeconds(2));64 Console.WriteLine("Timer expired");65 });66 await Task.Delay(1000);67 timer.Stop();68 await t;69 });70 }71 }72}73using System;74using System.Threading.Tasks;75using Microsoft.Coyote;76using Microsoft.Coyote.Samples.CoffeeMachineTasks;

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Tasks;5using Microsoft.Coyote.Samples.CoffeeMachineTasks;6{7 {8 private Task _timerTask;9 private TaskCompletionSource<bool> _tcs;10 private int _timeout;11 public ControlledTimer()12 {13 _tcs = new TaskCompletionSource<bool>();14 _timerTask = _tcs.Task;15 }16 public Task Start(int timeout)17 {18 _timeout = timeout;19 _timerTask = Task.Delay(timeout);20 return _timerTask;21 }22 public void Stop()23 {24 _tcs.TrySetResult(true);25 _tcs = new TaskCompletionSource<bool>();26 }27 {28 {29 return _timeout;30 }31 }32 {33 {34 return _timerTask;35 }36 }37 }38}39using System;40using System.Threading.Tasks;41using Microsoft.Coyote;42using Microsoft.Coyote.Tasks;43using Microsoft.Coyote.Samples.CoffeeMachineTasks;44{45 {46 private ControlledTimer _timer;47 private TaskCompletionSource<bool> _tcs;48 public CoffeeMachine()49 {50 _timer = new ControlledTimer();51 _tcs = new TaskCompletionSource<bool>();52 }53 public async Task MakeCoffee()54 {55 await _timer.Start(1000);56 await _tcs.Task;57 }58 public void Stop()59 {60 _timer.Stop();61 _tcs.TrySetResult(true);62 _tcs = new TaskCompletionSource<bool>();63 }64 }65}66using System;67using System.Threading.Tasks;68using Microsoft.Coyote;69using Microsoft.Coyote.Tasks;70using Microsoft.Coyote.Samples.CoffeeMachineTasks;71{72 {73 private static async Task Main(string[] args)74 {75 var coffeeMachine = new CoffeeMachine();76 var task = coffeeMachine.MakeCoffee();77 coffeeMachine.Stop();

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.CoffeeMachineTasks;2using System.Threading.Tasks;3{4 {5 public static async Task Main(string[] args)6 {7 var timer = new ControlledTimer();8 var task = timer.Start(1000);9 await Task.Delay(500);10 timer.Stop();11 await task;12 }13 }14}15using Microsoft.Coyote.Samples.CoffeeMachineTasks;16using System.Threading.Tasks;17{18 {19 public static async Task Main(string[] args)20 {21 var timer = new ControlledTimer();22 var task = timer.Start(1000);23 await Task.Delay(500);24 timer.Start(500);25 await task;26 }27 }28}29using Microsoft.Coyote.Samples.CoffeeMachineTasks;30using System.Threading.Tasks;31{32 {33 public static async Task Main(string[] args)34 {35 var timer = new ControlledTimer();36 var task = timer.Start(1000);37 await Task.Delay(500);38 timer.Start(1000);39 await task;40 }41 }42}43using Microsoft.Coyote.Samples.CoffeeMachineTasks;44using System.Threading.Tasks;45{46 {47 public static async Task Main(string[] args)48 {49 var timer = new ControlledTimer();50 var task = timer.Start(1000);51 await Task.Delay(500);52 timer.Start(500);53 await Task.Delay(500);54 timer.Start(1000);55 await task;56 }57 }58}59using Microsoft.Coyote.Samples.CoffeeMachineTasks;60using System.Threading.Tasks;61{62 {63 public static async Task Main(string[] args)

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.CoffeeMachineTasks;2using System;3using System.Threading;4using System.Threading.Tasks;5{6 {7 public static void Main(string[] args)8 {9 var timer = new ControlledTimer();10 timer.Start(TimeSpan.FromSeconds(2));11 timer.Stop();12 Console.WriteLine("Timer stopped");13 }14 }15}16using Microsoft.Coyote.Samples.CoffeeMachineTasks;17using System;18using System.Threading;19using System.Threading.Tasks;20{21 {22 public static void Main(string[] args)23 {24 var timer = new ControlledTimer();25 timer.Start(TimeSpan.FromSeconds(2));26 Thread.Sleep(2000);27 timer.Stop();28 Console.WriteLine("Timer stopped");29 }30 }31}32using Microsoft.Coyote.Samples.CoffeeMachineTasks;33using System;34using System.Threading;35using System.Threading.Tasks;36{37 {38 public static void Main(string[] args)39 {40 var timer = new ControlledTimer();41 timer.Start(TimeSpan.FromSeconds(2));42 Thread.Sleep(5000);43 timer.Stop();44 Console.WriteLine("Timer stopped");45 }46 }47}48using Microsoft.Coyote.Samples.CoffeeMachineTasks;49using System;50using System.Threading;51using System.Threading.Tasks;52{53 {54 public static void Main(string[] args)55 {56 var timer = new ControlledTimer();57 timer.Start(TimeSpan.FromSeconds(2));58 Thread.Sleep(1000);59 timer.Stop();60 Console.WriteLine("Timer stopped");61 }62 }63}64using Microsoft.Coyote.Samples.CoffeeMachineTasks;65using System;66using System.Threading;67using System.Threading.Tasks;68{

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Samples.CoffeeMachineTasks;2using System.Threading.Tasks;3using System;4{5 {6 static void Main(string[] args)7 {8 var timer = new ControlledTimer();9 var task = Task.Run(() => timer.Start(1000));10 Task.Delay(100).Wait();11 timer.Stop();12 task.Wait();13 }14 }15}16using Microsoft.Coyote.Samples.CoffeeMachineTasks;17using System.Threading.Tasks;18using System;19{20 {21 static void Main(string[] args)22 {23 var timer = new ControlledTimer();24 var task = Task.Run(() => timer.Start(1000));25 Task.Delay(100).Wait();26 timer.Start(1000);27 task.Wait();28 }29 }30}31 at Microsoft.Coyote.Samples.CoffeeMachineTasks.ControlledTimer.Start(Int32 duration)32 at Microsoft.Coyote.Samples.CoffeeMachineTasks.Program.Main(String[] args)33using System;34using System.Threading;35using System.Threading.Tasks;36{37 {38 private CancellationTokenSource cts;39 public void Start(int duration)40 {41 if (this.cts != null)42 {43 throw new InvalidOperationException("Timer already started.");44 }45 this.cts = new CancellationTokenSource();46 Task.Delay(duration, this.cts.Token).ContinueWith(tsk =>47 {48 this.cts = null;49 Console.WriteLine("Timer elapsed.");50 });51 }52 public void Stop()53 {54 if (this.cts != null)55 {56 this.cts.Cancel();57 this.cts = null;58 }59 }60 }61}

Full Screen

Full Screen

Stop

Using AI Code Generation

copy

Full Screen

1 public void StartTimer()2 {3 ControlledTimer timer = new ControlledTimer();4 timer.Start(1000, () =>5 {6 Console.WriteLine("Timer expired");7 });8 timer.Stop();9 }10 }11}

Full Screen

Full Screen

Automation Testing Tutorials

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

LambdaTest Learning Hubs:

YouTube

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

Run Coyote automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful