How to use ActorExecutionContext class of Microsoft.Coyote.Actors package

Best Coyote code snippet using Microsoft.Coyote.Actors.ActorExecutionContext

SharedDictionary.cs

Source:SharedDictionary.cs Github

copy

Full Screen

...20 /// <typeparam name="TValue">The type of the value.</typeparam>21 /// <param name="runtime">The actor runtime.</param>22 public static SharedDictionary<TKey, TValue> Create<TKey, TValue>(IActorRuntime runtime)23 {24 if (runtime is ActorExecutionContext.Mock executionContext)25 {26 return new Mock<TKey, TValue>(executionContext, null);27 }28 return new SharedDictionary<TKey, TValue>(new ConcurrentDictionary<TKey, TValue>());29 }30 /// <summary>31 /// Creates a new shared dictionary.32 /// </summary>33 /// <typeparam name="TKey">The type of the key.</typeparam>34 /// <typeparam name="TValue">The type of the value.</typeparam>35 /// <param name="comparer">The key comparer.</param>36 /// <param name="runtime">The actor runtime.</param>37 public static SharedDictionary<TKey, TValue> Create<TKey, TValue>(IEqualityComparer<TKey> comparer, IActorRuntime runtime)38 {39 if (runtime is ActorExecutionContext.Mock executionContext)40 {41 return new Mock<TKey, TValue>(executionContext, comparer);42 }43 return new SharedDictionary<TKey, TValue>(new ConcurrentDictionary<TKey, TValue>(comparer));44 }45 /// <summary>46 /// Mock implementation of <see cref="SharedDictionary{TKey, TValue}"/> that can be controlled during systematic testing.47 /// </summary>48 private sealed class Mock<TKey, TValue> : SharedDictionary<TKey, TValue>49 {50 // TODO: port to the new resource API or controlled locks once we integrate actors with tasks.51 /// <summary>52 /// Actor modeling the shared dictionary.53 /// </summary>54 private readonly ActorId DictionaryActor;55 /// <summary>56 /// The execution context associated with this shared dictionary.57 /// </summary>58 private readonly ActorExecutionContext.Mock Context;59 /// <summary>60 /// Initializes a new instance of the <see cref="Mock{TKey, TValue}"/> class.61 /// </summary>62 internal Mock(ActorExecutionContext.Mock context, IEqualityComparer<TKey> comparer)63 : base(null)64 {65 this.Context = context;66 if (comparer != null)67 {68 this.DictionaryActor = context.CreateActor(69 typeof(SharedDictionaryActor<TKey, TValue>),70 SharedDictionaryEvent.InitializeEvent(comparer));71 }72 else73 {74 this.DictionaryActor = context.CreateActor(typeof(SharedDictionaryActor<TKey, TValue>));75 }76 }...

Full Screen

Full Screen

SharedCounter.cs

Source:SharedCounter.cs Github

copy

Full Screen

...28 /// <param name="runtime">The actor runtime.</param>29 /// <param name="value">The initial value.</param>30 public static SharedCounter Create(IActorRuntime runtime, int value = 0)31 {32 if (runtime is ActorExecutionContext.Mock executionContext)33 {34 return new Mock(value, executionContext);35 }36 return new SharedCounter(value);37 }38 /// <summary>39 /// Increments the shared counter.40 /// </summary>41 public virtual void Increment()42 {43 Interlocked.Increment(ref this.Counter);44 }45 /// <summary>46 /// Decrements the shared counter.47 /// </summary>48 public virtual void Decrement()49 {50 Interlocked.Decrement(ref this.Counter);51 }52 /// <summary>53 /// Gets the current value of the shared counter.54 /// </summary>55 public virtual int GetValue() => this.Counter;56 /// <summary>57 /// Adds a value to the counter atomically.58 /// </summary>59 public virtual int Add(int value) => Interlocked.Add(ref this.Counter, value);60 /// <summary>61 /// Sets the counter to a value atomically.62 /// </summary>63 public virtual int Exchange(int value) => Interlocked.Exchange(ref this.Counter, value);64 /// <summary>65 /// Sets the counter to a value atomically if it is equal to a given value.66 /// </summary>67 public virtual int CompareExchange(int value, int comparand) =>68 Interlocked.CompareExchange(ref this.Counter, value, comparand);69 /// <summary>70 /// Mock implementation of <see cref="SharedCounter"/> that can be controlled during systematic testing.71 /// </summary>72 private sealed class Mock : SharedCounter73 {74 // TODO: port to the new resource API or controlled locks once we integrate actors with tasks.75 /// <summary>76 /// Actor modeling the shared counter.77 /// </summary>78 private readonly ActorId CounterActor;79 /// <summary>80 /// The execution context associated with this shared counter.81 /// </summary>82 private readonly ActorExecutionContext.Mock Context;83 /// <summary>84 /// Initializes a new instance of the <see cref="Mock"/> class.85 /// </summary>86 internal Mock(int value, ActorExecutionContext.Mock context)87 : base(value)88 {89 this.Context = context;90 this.CounterActor = context.CreateActor(typeof(SharedCounterActor));91 var op = context.Scheduler.GetExecutingOperation<ActorOperation>();92 context.SendEvent(this.CounterActor, SharedCounterEvent.SetEvent(op.Actor.Id, value));93 op.Actor.ReceiveEventAsync(typeof(SharedCounterResponseEvent)).Wait();94 }95 /// <summary>96 /// Increments the shared counter.97 /// </summary>98 public override void Increment() =>99 this.Context.SendEvent(this.CounterActor, SharedCounterEvent.IncrementEvent());100 /// <summary>...

Full Screen

Full Screen

SharedRegister.cs

Source:SharedRegister.cs Github

copy

Full Screen

...19 /// <param name="value">The initial value.</param>20 public static SharedRegister<T> Create<T>(IActorRuntime runtime, T value = default)21 where T : struct22 {23 if (runtime is ActorExecutionContext.Mock executionContext)24 {25 return new Mock<T>(executionContext, value);26 }27 return new SharedRegister<T>(value);28 }29 /// <summary>30 /// Mock implementation of <see cref="SharedRegister{T}"/> that can be controlled during systematic testing.31 /// </summary>32 private sealed class Mock<T> : SharedRegister<T>33 where T : struct34 {35 // TODO: port to the new resource API or controlled locks once we integrate actors with tasks.36 /// <summary>37 /// Actor modeling the shared register.38 /// </summary>39 private readonly ActorId RegisterActor;40 /// <summary>41 /// The execution context associated with this shared register.42 /// </summary>43 private readonly ActorExecutionContext.Mock Context;44 /// <summary>45 /// Initializes a new instance of the <see cref="Mock{T}"/> class.46 /// </summary>47 internal Mock(ActorExecutionContext.Mock context, T value)48 : base(value)49 {50 this.Context = context;51 this.RegisterActor = context.CreateActor(typeof(SharedRegisterActor<T>));52 context.SendEvent(this.RegisterActor, SharedRegisterEvent.SetEvent(value));53 }54 /// <summary>55 /// Reads and updates the register.56 /// </summary>57 public override T Update(Func<T, T> func)58 {59 var op = this.Context.Scheduler.GetExecutingOperation<ActorOperation>();60 this.Context.SendEvent(this.RegisterActor, SharedRegisterEvent.UpdateEvent(func, op.Actor.Id));61 var e = op.Actor.ReceiveEventAsync(typeof(SharedRegisterResponseEvent<T>)).Result as SharedRegisterResponseEvent<T>;...

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Actors;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors;6using Microsoft.Coyote.Actors;7{8{9public int Value;10public MyEvent(int v)11{12Value = v;13}14}15{16[OnEventDoAction(typeof(MyEvent), nameof(OnMyEvent))]17{18}19private void OnMyEvent(Event e)20{21}22}23{24[OnEventDoAction(typeof(MyEvent), nameof(OnMyEvent))]25{26}27private void OnMyEvent(Event e)28{29}30}31{32public static void Main(string[] args)33{34using (var runtime = RuntimeFactory.Create())35{36ActorId actor = runtime.CreateActor(typeof(MyActor));37ActorId monitor = runtime.CreateMonitor(typeof(MyMonitor));38runtime.SendEvent(actor, new MyEvent(1));39runtime.SendEvent(monitor, new MyEvent(2));40}41}42}43}44using Microsoft.Coyote.Actors;45using Microsoft.Coyote.Actors;46using Microsoft.Coyote.Actors;

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Actors;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors;6using Microsoft.Coyote.Actors;7using Microsoft.Coyote.Actors;8using Microsoft.Coyote.Actors;9using System.Threading.Tasks;10using Microsoft.Coyote.Actors;11using Microsoft.Coyote.Actors;12using Microsoft.Coyote.Actors;13using Microsoft.Coyote.Actors;14using Microsoft.Coyote.Actors;15using Microsoft.Coyote.Actors;16using Microsoft.Coyote.Actors;17using System.Threading.Tasks;18using Microsoft.Coyote.Actors;19using Microsoft.Coyote.Actors;20using Microsoft.Coyote.Actors;21using Microsoft.Coyote.Actors;22using Microsoft.Coyote.Actors;

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using Microsoft.Coyote;3{4 {5 static void Main(string[] args)6 {7 ActorRuntime runtime = new ActorRuntime();8 ActorExecutionContext context = new ActorExecutionContext(runtime);9 ActorId actorId = new ActorId();10 Actor actor = new Actor(context, actorId);11 }12 }13}14Actor(ActorExecutionContext context, ActorId id)15protected ActorId CreateActor(Type type)16protected ActorId CreateActor(Type type, Event e)17protected void SendEvent(ActorId target, Event e)18protected void SendEvent(ActorId target, Event e, SendOptions options)19protected void Monitor<T>(ActorId target, Event e)20protected void Monitor<T>(ActorId target, Event e, SendOptions options)21protected void Unmonitor<T>(ActorId target)22protected void Unmonitor<T>(ActorId target, Event e)23protected void RaiseEvent(Event e)24protected void RaiseEvent(Event e, SendOptions options)25protected void WaitEvent(Event e)26protected void WaitEvent<T>(Func<T, bool> predicate)27protected void WaitEvent<T>(Func<T, bool> predicate, Action<T> handler)

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using System;3using System.Collections.Generic;4using System.Threading.Tasks;5{6 {7 static void Main(string[] args)8 {9 var runtime = RuntimeFactory.Create();10 runtime.CreateActor(typeof(MyActor));11 Console.ReadLine();12 }13 }14 {15 protected override Task OnInitializeAsync(Event initialEvent)16 {17 this.SendEvent(this.Id, new MyEvent());18 return Task.CompletedTask;19 }20 private Task OnEvent(MyEvent evt)21 {22 Console.WriteLine("Hello World!");23 return Task.CompletedTask;24 }25 }26 {27 }28}29using Microsoft.Coyote;30using System;31using System.Collections.Generic;32using System.Threading.Tasks;33{34 {35 static void Main(string[] args)36 {37 var runtime = RuntimeFactory.Create();38 runtime.CreateActor(typeof(MyActor));39 Console.ReadLine();40 }41 }42 {43 protected override Task OnInitializeAsync(Event initialEvent)44 {45 this.SendEvent(this.Id, new MyEvent());46 return Task.CompletedTask;47 }48 private Task OnEvent(MyEvent evt)49 {50 Console.WriteLine("Hello World!");51 return Task.CompletedTask;52 }53 }54 {55 }56}57 at CoyoteActorModel.MyActor.OnInitializeAsync(Event initialEvent) in C:\Users\jim\source\repos\CoyoteActorModel\CoyoteActorModel\1.cs:line 3058 at Microsoft.Coyote.Actors.Actor.OnInitializeAsync(Event initialEvent)59 at Microsoft.Coyote.Actors.Actor.InitializeAsync(Event initialEvent)60 at Microsoft.Coyote.Actors.Runtime.ActorManager.CreateActor(Type type, Event initialEvent, ActorId id, Boolean createSynchronously)61 at Microsoft.Coyote.Actors.Runtime.ActorManager.CreateActor(Type type, Event initialEvent, ActorId id)

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using System;3{4 {5 static void Main(string[] args)6 {7 ActorExecutionContext actorExecutionContext = new ActorExecutionContext();8 actorExecutionContext.RunAsync(new HelloActor()).Wait();9 }10 }11}12using Microsoft.Coyote.Actors;13using System;14{15 {16 protected override Task OnInitializeAsync(Event initialEvent)17 {18 this.SendEvent(this.Id, new HelloEvent());19 return Task.CompletedTask;20 }21 protected override Task OnEventAsync(Event e)22 {23 if (e is HelloEvent)24 {25 Console.WriteLine("Hello World!");26 }27 return Task.CompletedTask;28 }29 }30 {31 }32}33using Microsoft.Coyote.Actors;34using Microsoft.Coyote.Actors.Timers;35using Microsoft.Coyote.Runtime;36using Microsoft.Coyote.Testing;37using Microsoft.Coyote.TestingServices;38using System;39using System.Threading.Tasks;40using Xunit;41{42 {43 public async Task TestHelloActor()44 {45 var configuration = Configuration.Create();46 configuration.TestingIterations = 100;47 configuration.MaxSchedulingSteps = 1000;48 configuration.Verbose = 1;49 var runtime = TestingRuntime.Create(configuration);50 await this.TestAsync(runtime, async () =>51 {

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote.Actors;4{5 {6 protected override async Task OnInitializeAsync(Event initialEvent)7 {8 await this.SendEvent(this.Id, new MyEvent());9 }10 protected override async Task OnEventAsync(Event e)11 {12 ActorExecutionContext context = this.GetExecutionContext();13 Console.WriteLine("Actor Id: " + context.Id);14 Console.WriteLine("Actor Type: " + context.Type);15 Console.WriteLine("Actor State: " + context.State);16 Console.WriteLine("Actor Current Event: " + context.CurrentEvent);17 Console.WriteLine("Actor Current Event Type: " + context.CurrentEventType);18 Console.WriteLine("Actor Current Event Handler: " + context.CurrentEventHandler);19 Console.WriteLine("Actor Current Event Handler Name: " + context.CurrentEventHandlerName);20 Console.WriteLine("Actor Current Event Handler Type: " + context.CurrentEventHandlerType);21 Console.WriteLine("Actor Current Event Handler Declaring Type: " + context.CurrentEventHandlerDeclaringType);22 Console.WriteLine("Actor Current Event Handler Declaring Type Name: " + context.CurrentEventHandlerDeclaringTypeName);23 Console.WriteLine("Actor Current Event Handler Declaring Type Namespace: " + context.CurrentEventHandlerDeclaringTypeNamespace);24 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly: " + context.CurrentEventHandlerDeclaringTypeAssembly);25 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Qualified Name: " + context.CurrentEventHandlerDeclaringTypeAssemblyQualifiedName);26 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Location: " + context.CurrentEventHandlerDeclaringTypeAssemblyLocation);27 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Code Base: " + context.CurrentEventHandlerDeclaringTypeAssemblyCodeBase);28 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Full Name: " + context.CurrentEventHandlerDeclaringTypeAssemblyFullName);29 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Version: " + context.CurrentEventHandlerDeclaringTypeAssemblyVersion);30 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Image Runtime Version: " + context.CurrentEventHandlerDeclaringTypeAssemblyImageRuntimeVersion);31 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Configuration: " + context.CurrentEventHandlerDeclaringTypeAssemblyConfiguration);32 Console.WriteLine("Actor Current Event Handler Declaring Type Assembly Public Key Token:

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.Coyote.Actors;3using Microsoft.Coyote.Actors.Timers;4using System.Threading.Tasks;5using System.Collections.Generic;6using System.Threading;7using System.Diagnostics;8{9 {10 public static void Main(string[] args)11 {12 ActorExecutionContext.Run(async () =>13 {14 var a = Actor.Create<Actor1>();15 await a.ReceiveEventAsync<Done>();16 });17 }18 }19 public class Done : Event { }20 {21 protected override Task OnInitializeAsync(Event initialEvent)22 {23 this.SendEvent(this.Id, new Done());24 return Task.CompletedTask;25 }26 protected override Task OnEventAsync(Event e)27 {28 switch (e)29 {30 this.Stop();31 break;32 }33 return Task.CompletedTask;34 }35 }36}37 at Microsoft.Coyote.Actors.ActorRuntime.SendEvent(ActorId target, Event e, EventGroup group, EventInfo info, Guid opGroupId, EventWaitHandle waitHandle)38 at Microsoft.Coyote.Actors.Actor.SendEvent(ActorId target, Event e, EventGroup group, EventInfo info, Guid opGroupId, EventWaitHandle waitHandle)39 at Microsoft.Coyote.Actors.Actor.SendEvent(ActorId target, Event e, EventInfo info, Guid opGroupId, EventWaitHandle waitHandle)40 at Microsoft.Coyote.Actors.Actor.SendEvent(ActorId target, Event e, EventInfo info)41 at Microsoft.Coyote.Actors.Actor.SendEvent(ActorId target, Event e)42 at Test.Actor1.OnInitializeAsync(Event initialEvent) in C:\Users\user\source\repos\Test\Test\1.cs:line 3443 at Microsoft.Coyote.Actors.Actor.OnEventAsync(Event e)44 at Microsoft.Coyote.SystematicTesting.Runtime.SchedulingStrategy.ProcessEventAsync(Actor actor, Event e, Boolean isReplaying)

Full Screen

Full Screen

ActorExecutionContext

Using AI Code Generation

copy

Full Screen

1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Runtime;3using System.Threading.Tasks;4using System;5{6 {7 public static void Main()8 {9 var context = new ActorExecutionContext();10 var runtime = new ActorRuntime(context);11 var id = ActorId.CreateRandom();12 var actor = new Actor1(id, runtime);13 var task = new ActorTask(actor, id, runtime);14 task.Start();15 task.Wait();16 }17 }18}19using Microsoft.Coyote.Actors;20using Microsoft.Coyote.Runtime;21using System.Threading.Tasks;22using System;23{24 {25 public static void Main()26 {27 var context = new ActorExecutionContext();28 var runtime = new ActorRuntime(context);29 var id = ActorId.CreateRandom();30 var actor = new Actor1(id, runtime);31 var task = new ActorTask(actor, id, runtime);32 task.Start();33 task.Wait();34 }35 }36}37using Microsoft.Coyote.Actors;38using Microsoft.Coyote.Runtime;39using System.Threading.Tasks;40using System;41{42 {43 public static void Main()44 {45 var context = new ActorExecutionContext();46 var runtime = new ActorRuntime(context);47 var id = ActorId.CreateRandom();

Full Screen

Full Screen

Automation Testing Tutorials

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

LambdaTest Learning Hubs:

YouTube

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful