How to use OperationGroup class of Microsoft.Coyote.Runtime package

Best Coyote code snippet using Microsoft.Coyote.Runtime.OperationGroup

PrioritizationStrategy.cs

Source:PrioritizationStrategy.cs Github

copy

Full Screen

...18 {19 /// <summary>20 /// List of prioritized operation groups.21 /// </summary>22 private readonly List<OperationGroup> PrioritizedOperationGroups;23 /// <summary>24 /// Scheduling points in the current execution where a priority change should occur.25 /// </summary>26 private readonly HashSet<int> PriorityChangePoints;27 /// <summary>28 /// Number of potential priority change points in the current iteration.29 /// </summary>30 private int NumPriorityChangePoints;31 /// <summary>32 /// Max number of potential priority change points across all iterations.33 /// </summary>34 private int MaxPriorityChangePoints;35 /// <summary>36 /// Max number of priority changes per iteration.37 /// </summary>38 private readonly int MaxPriorityChanges;39 /// <summary>40 /// Initializes a new instance of the <see cref="PrioritizationStrategy"/> class.41 /// </summary>42 internal PrioritizationStrategy(Configuration configuration, IRandomValueGenerator generator, bool isFair)43 : base(configuration, generator, isFair)44 {45 this.PrioritizedOperationGroups = new List<OperationGroup>();46 this.PriorityChangePoints = new HashSet<int>();47 this.NumPriorityChangePoints = 0;48 this.MaxPriorityChangePoints = 0;49 this.MaxPriorityChanges = configuration.StrategyBound;50 }51 /// <inheritdoc/>52 internal override bool InitializeNextIteration(uint iteration)53 {54 // The first iteration has no knowledge of the execution, so only initialize from the second55 // iteration and onwards. Note that although we could initialize the first length based on a56 // heuristic, its not worth it, as the strategy will typically explore thousands of iterations,57 // plus its also interesting to explore a schedule with no forced priority switch points.58 if (iteration > 0)59 {60 this.PrioritizedOperationGroups.Clear();61 this.PriorityChangePoints.Clear();62 this.MaxPriorityChangePoints = Math.Max(63 this.MaxPriorityChangePoints, this.NumPriorityChangePoints);64 if (this.MaxPriorityChanges > 0)65 {66 var priorityChanges = this.RandomValueGenerator.Next(this.MaxPriorityChanges) + 1;67 var range = Enumerable.Range(0, this.MaxPriorityChangePoints);68 foreach (int point in this.Shuffle(range).Take(priorityChanges))69 {70 this.PriorityChangePoints.Add(point);71 }72 }73 this.DebugPrintPriorityChangePoints();74 }75 this.NumPriorityChangePoints = 0;76 return base.InitializeNextIteration(iteration);77 }78 /// <inheritdoc/>79 internal override bool NextOperation(IEnumerable<ControlledOperation> ops, ControlledOperation current,80 bool isYielding, out ControlledOperation next)81 {82 if (this.IsFair && this.StepCount >= this.Configuration.MaxUnfairSchedulingSteps)83 {84 return base.NextOperation(ops, current, isYielding, out next);85 }86 // Set the priority of any new operation groups.87 this.SetNewOperationGroupPriorities(ops, current);88 // Check if there are at least two operation groups that can be scheduled,89 // otherwise skip the priority checking and changing logic.90 if (ops.Select(op => op.Group).Distinct().Skip(1).Any())91 {92 // Try to change the priority of the highest priority operation group.93 // If the shared-state reduction is enabled, check if there is at least94 // one 'WRITE' operation, before trying to change the priority.95 if (!this.Configuration.IsSharedStateReductionEnabled ||96 ops.Any(op => op.LastSchedulingPoint is SchedulingPointType.Write))97 {98 this.TryPrioritizeNextOperationGroup(ops);99 }100 // Get the operations that belong to the highest priority group.101 OperationGroup nextGroup = this.GetOperationGroupWithHighestPriority(ops);102 ops = ops.Where(op => nextGroup.IsMember(op));103 }104 int idx = this.RandomValueGenerator.Next(ops.Count());105 next = ops.ElementAt(idx);106 return true;107 }108 /// <summary>109 /// Returns the operation group with the highest priority.110 /// </summary>111 private OperationGroup GetOperationGroupWithHighestPriority(IEnumerable<ControlledOperation> ops)112 {113 foreach (var group in this.PrioritizedOperationGroups)114 {115 if (ops.Any(op => op.Group == group))116 {117 return group;118 }119 }120 return null;121 }122 /// <summary>123 /// Sets a random priority to any new operation groups.124 /// </summary>125 private void SetNewOperationGroupPriorities(IEnumerable<ControlledOperation> ops, ControlledOperation current)126 {127 int count = this.PrioritizedOperationGroups.Count;128 if (count is 0)129 {130 this.PrioritizedOperationGroups.Add(current.Group);131 }132 // Randomize the priority of all new operation groups.133 foreach (var group in ops.Select(op => op.Group).Where(g => !this.PrioritizedOperationGroups.Contains(g)))134 {135 // Randomly choose a priority for this group.136 int index = this.RandomValueGenerator.Next(this.PrioritizedOperationGroups.Count) + 1;137 this.PrioritizedOperationGroups.Insert(index, group);138 Debug.WriteLine("[coyote::strategy] Assigned priority '{0}' for operation group '{1}'.", index, group);139 }140 if (this.PrioritizedOperationGroups.Count > count)141 {142 this.DebugPrintOperationPriorityList();143 }144 }145 /// <summary>146 /// Reduces the priority of highest priority operation group, if there is a priority147 /// change point installed on the current execution step.148 /// </summary>149 private bool TryPrioritizeNextOperationGroup(IEnumerable<ControlledOperation> ops)150 {151 OperationGroup group = null;152 if (this.PriorityChangePoints.Contains(this.NumPriorityChangePoints))153 {154 // This scheduling step was chosen as a priority change point.155 group = this.GetOperationGroupWithHighestPriority(ops);156 Debug.WriteLine("[coyote::strategy] Reduced the priority of operation group '{0}'.", group);157 }158 this.NumPriorityChangePoints++;159 if (group != null)160 {161 // Reduce the priority of the group by putting it in the end of the list.162 this.PrioritizedOperationGroups.Remove(group);163 this.PrioritizedOperationGroups.Add(group);164 return true;165 }166 return false;167 }168 /// <inheritdoc/>169 internal override string GetDescription() =>170 $"prioritization[fair:{this.IsFair},bound:{this.MaxPriorityChanges},seed:{this.RandomValueGenerator.Seed}]";171 /// <summary>172 /// Shuffles the specified range using the Fisher-Yates algorithm.173 /// </summary>174 /// <remarks>175 /// See https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle.176 /// </remarks>177 private IList<int> Shuffle(IEnumerable<int> range)178 {179 var result = new List<int>(range);180 for (int idx = result.Count - 1; idx >= 1; idx--)181 {182 int point = this.RandomValueGenerator.Next(result.Count);183 int temp = result[idx];184 result[idx] = result[point];185 result[point] = temp;186 }187 return result;188 }189 /// <inheritdoc/>190 internal override void Reset()191 {192 this.NumPriorityChangePoints = 0;193 this.PrioritizedOperationGroups.Clear();194 this.PriorityChangePoints.Clear();195 base.Reset();196 }197 /// <summary>198 /// Print the operation group priority list, if debug is enabled.199 /// </summary>200 private void DebugPrintOperationPriorityList()201 {202 if (Debug.IsEnabled)203 {204 Debug.WriteLine("[coyote::strategy] Updated operation group priority list: ");205 for (int idx = 0; idx < this.PrioritizedOperationGroups.Count; idx++)206 {207 var group = this.PrioritizedOperationGroups[idx];208 if (group.Any(m => m.Status is OperationStatus.Enabled))209 {210 Debug.WriteLine(" |_ [{0}] operation group with id '{1}' [enabled]", idx, group);211 }212 else if (group.Any(m => m.Status != OperationStatus.Completed))213 {214 Debug.WriteLine(" |_ [{0}] operation group with id '{1}'", idx, group);215 }216 }217 }218 }219 /// <summary>220 /// Print the priority change points, if debug is enabled.221 /// </summary>...

Full Screen

Full Screen

ControlledOperation.cs

Source:ControlledOperation.cs Github

copy

Full Screen

...30 /// <summary>31 /// The group where this operation has membership. This can be used32 /// by the scheduler to optimize exploration.33 /// </summary>34 internal readonly OperationGroup Group;35 /// <summary>36 /// Queue of continuations that this operation must execute before it completes.37 /// </summary>38 private readonly Queue<Action> Continuations;39 /// <summary>40 /// Dependency that must get resolved before this operation can resume executing.41 /// </summary>42 private Func<bool> Dependency;43 /// <summary>44 /// Synchronization mechanism for controlling the execution of this operation.45 /// </summary>46 private ManualResetEventSlim SyncEvent;47 /// <summary>48 /// The type of the last encountered scheduling point.49 /// </summary>50 internal SchedulingPointType LastSchedulingPoint;51 /// <summary>52 /// A value that represents the hashed program state when this operation last executed.53 /// </summary>54 internal int LastHashedProgramState;55 /// <summary>56 /// A value that represents the shared state being accessed when this57 /// operation last executed, if there was any such state access.58 /// </summary>59 internal string LastAccessedSharedState;60 /// <summary>61 /// True if the source of this operation is uncontrolled, else false.62 /// </summary>63 internal bool IsSourceUncontrolled;64 /// <summary>65 /// True if the dependency is uncontrolled, else false.66 /// </summary>67 internal bool IsDependencyUncontrolled;68 /// <summary>69 /// True if this is the root operation, else false.70 /// </summary>71 internal bool IsRoot => this.Id is 0;72 /// <summary>73 /// True if this operation is currently paused, else false.74 /// </summary>75 internal bool IsPaused =>76 this.Status is OperationStatus.Paused ||77 this.Status is OperationStatus.PausedOnDelay ||78 this.Status is OperationStatus.PausedOnResource ||79 this.Status is OperationStatus.PausedOnReceive;80 /// <summary>81 /// Initializes a new instance of the <see cref="ControlledOperation"/> class.82 /// </summary>83 internal ControlledOperation(ulong operationId, string name, OperationGroup group, CoyoteRuntime runtime)84 {85 this.Runtime = runtime;86 this.Id = operationId;87 this.Name = name;88 this.Status = OperationStatus.None;89 this.Group = group ?? OperationGroup.Create(this);90 this.Continuations = new Queue<Action>();91 this.SyncEvent = new ManualResetEventSlim(false);92 this.LastSchedulingPoint = SchedulingPointType.Start;93 this.LastHashedProgramState = 0;94 this.LastAccessedSharedState = string.Empty;95 this.IsSourceUncontrolled = false;96 this.IsDependencyUncontrolled = false;97 // Register this operation with the runtime.98 this.Runtime.RegisterNewOperation(this);99 }100 /// <summary>101 /// Executes all continuations of this operation in order, if there are any.102 /// </summary>103 internal void ExecuteContinuations()...

Full Screen

Full Screen

RequestControllerMiddleware.cs

Source:RequestControllerMiddleware.cs Github

copy

Full Screen

...41 IO.Debug.WriteLine("[coyote::debug] Runtime '{0}' takes control of the '{1} {2}' handler on thread '{3}'.",42 runtime.Id, request.Method, request.Path, SystemThread.CurrentThread.ManagedThreadId);43 TryExtractSourceOperation(request, runtime, out ControlledOperation source);44 var op = HttpOperation.Create(ToHttpMethod(request.Method), request.Path, runtime, source);45 OperationGroup.SetCurrent(op.Group);46 await runtime.TaskFactory.StartNew(state =>47 {48 SystemTask task = this.Next(context);49 TaskServices.WaitUntilTaskCompletes(runtime, op, task);50 task.GetAwaiter().GetResult();51 },52 op,53 default,54 runtime.TaskFactory.CreationOptions | SystemTaskCreationOptions.DenyChildAttach,55 runtime.TaskFactory.Scheduler);56 }57 else58 {59 IO.Debug.WriteLine($"[coyote::debug] Unable to control the '{0} {1}' request on thread '{2}'.",...

Full Screen

Full Screen

OperationGroup

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Specifications;6using Microsoft.Coyote.Tasks;7using Microsoft.Coyote.Runtime;8{9 {10 static void Main(string[] args)11 {12 var runtime = RuntimeFactory.Create();13 runtime.RegisterMonitor(typeof(M));14 runtime.CreateActor(typeof(A));15 runtime.Run();16 }17 }18 {19 [OnEventDoAction(typeof(StartEvent), nameof(Initialize))]20 class Init : MonitorState { }21 void Initialize()22 {23 this.CreateOperationGroup("group");24 }25 [OnEventDoAction(typeof(StartEvent), nameof(Start))]26 class Start : MonitorState { }27 void Start()28 {29 this.RaiseGotoStateEvent<Wait>();30 }31 [OnEventDoAction(typeof(StartEvent), nameof(Start2))]32 class Start2 : MonitorState { }33 void Start2()34 {35 this.RaiseGotoStateEvent<Wait2>();36 }37 [OnEventDoAction(typeof(StartEvent), nameof(Start3))]38 class Start3 : MonitorState { }39 void Start3()40 {41 this.RaiseGotoStateEvent<Wait3>();42 }43 [OnEventDoAction(typeof(StartEvent), nameof(Start4))]44 class Start4 : MonitorState { }45 void Start4()46 {47 this.RaiseGotoStateEvent<Wait4>();48 }49 [OnEventDoAction(typeof(StartEvent), nameof(Start5))]50 class Start5 : MonitorState { }51 void Start5()52 {53 this.RaiseGotoStateEvent<Wait5>();54 }55 [OnEventDoAction(typeof(StartEvent), nameof(Start6))]56 class Start6 : MonitorState { }57 void Start6()58 {59 this.RaiseGotoStateEvent<Wait6>();60 }61 [OnEventDoAction(typeof(StartEvent), nameof(Start7))]62 class Start7 : MonitorState { }63 void Start7()64 {65 this.RaiseGotoStateEvent<Wait7>();66 }67 [OnEventDoAction(typeof(StartEvent), nameof(Start8))]68 class Start8 : MonitorState { }69 void Start8()70 {

Full Screen

Full Screen

OperationGroup

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Runtime;2using System;3using System.Threading.Tasks;4{5 {6 static void Main(string[] args)7 {8 Console.WriteLine("Hello World!");9 var opGroup = OperationGroup.Create();10 using (opGroup.CreateScope())11 {12 Task.Run(() => TaskMethod(1, opGroup));13 Task.Run(() => TaskMethod(2, opGroup));14 }15 opGroup.Wait();16 }17 static void TaskMethod(int id, OperationGroup opGroup)18 {19 opGroup.WaitForIdle();20 Console.WriteLine(id);21 }22 }23}

Full Screen

Full Screen

OperationGroup

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Runtime;2using System.Threading.Tasks;3{4 {5 public static void Main(string[] args)6 {7 OperationGroup opGroup = new OperationGroup();8 Task task = new Task(() => opGroup.Execute());9 task.Start();10 task.Wait();11 }12 }13}14using Microsoft.Coyote.Runtime;15{16 {17 public static void Main(string[] args)18 {19 OperationGroup opGroup = new OperationGroup();20 opGroup.Execute();21 }22 }23}24using System.Threading.Tasks;25{26 {27 public static void Main(string[] args)28 {29 OperationGroup opGroup = new OperationGroup();30 Task task = new Task(() => opGroup.Execute());31 task.Start();32 task.Wait();33 }34 }35}

Full Screen

Full Screen

OperationGroup

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Runtime;2{3 {4 public static void Main()5 {6 var opGroup = new OperationGroup();7 opGroup.AddOperation(() => Console.WriteLine("Hello"));8 opGroup.AddOperation(() => Console.WriteLine("World"));9 opGroup.Execute();10 }11 }12}

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.

Most used methods in OperationGroup

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful