How to use OnReceiveEvent method of Microsoft.Coyote.Actors.Mocks.MockEventQueue class

Best Coyote code snippet using Microsoft.Coyote.Actors.Mocks.MockEventQueue.OnReceiveEvent

MockEventQueue.cs

Source:MockEventQueue.cs Github

copy

Full Screen

...78 if (this.EventWaitTypes.TryGetValue(e.GetType(), out Func<Event, bool> predicate) &&79 (predicate is null || predicate(e)))80 {81 this.EventWaitTypes.Clear();82 this.OnReceiveEvent(e, eventGroup, info);83 this.ReceiveCompletionSource.SetResult(e);84 return EnqueueStatus.EventHandlerRunning;85 }86 this.OnEnqueueEvent(e, eventGroup, info);87 this.Queue.AddLast((e, eventGroup, info));88 if (info.Assert >= 0)89 {90 var eventCount = this.Queue.Count(val => val.e.GetType().Equals(e.GetType()));91 this.Assert(eventCount <= info.Assert,92 "There are more than {0} instances of '{1}' in the input queue of {2}.",93 info.Assert, info.EventName, this.Owner.Id);94 }95 if (!this.IsEventHandlerRunning)96 {97 if (this.TryDequeueEvent(true).e is null)98 {99 return EnqueueStatus.NextEventUnavailable;100 }101 else102 {103 this.IsEventHandlerRunning = true;104 return EnqueueStatus.EventHandlerNotRunning;105 }106 }107 return EnqueueStatus.EventHandlerRunning;108 }109 /// <inheritdoc/>110 public (DequeueStatus status, Event e, EventGroup eventGroup, EventInfo info) Dequeue()111 {112 // Try to get the raised event, if there is one. Raised events113 // have priority over the events in the inbox.114 if (this.RaisedEvent != default)115 {116 if (this.IsEventIgnored(this.RaisedEvent.e))117 {118 // TODO: should the user be able to raise an ignored event?119 // The raised event is ignored in the current state.120 this.OnIgnoreEvent(this.RaisedEvent.e, this.RaisedEvent.eventGroup, this.RaisedEvent.info);121 this.RaisedEvent = default;122 }123 else124 {125 (Event e, EventGroup eventGroup, EventInfo info) raisedEvent = this.RaisedEvent;126 this.RaisedEvent = default;127 return (DequeueStatus.Raised, raisedEvent.e, raisedEvent.eventGroup, raisedEvent.info);128 }129 }130 // Make sure this happens before a potential dequeue.131 var hasDefaultHandler = this.IsDefaultHandlerAvailable();132 // Try to dequeue the next event, if there is one.133 var (e, eventGroup, info) = this.TryDequeueEvent();134 if (e != null)135 {136 // Found next event that can be dequeued.137 return (DequeueStatus.Success, e, eventGroup, info);138 }139 // No event can be dequeued, so check if there is a default event handler.140 if (!hasDefaultHandler)141 {142 // There is no default event handler installed, so do not return an event.143 this.IsEventHandlerRunning = false;144 return (DequeueStatus.NotAvailable, null, null, null);145 }146 // TODO: check op-id of default event.147 // A default event handler exists.148 string stateName = this.Owner is StateMachine stateMachine ?149 NameResolver.GetStateNameForLogging(stateMachine.CurrentState) : string.Empty;150 var eventOrigin = new EventOriginInfo(this.Owner.Id, this.Owner.GetType().FullName, stateName);151 return (DequeueStatus.Default, DefaultEvent.Instance, null, new EventInfo(DefaultEvent.Instance, eventOrigin));152 }153 /// <summary>154 /// Dequeues the next event and its metadata, if there is one available, else returns null.155 /// </summary>156 private (Event e, EventGroup eventGroup, EventInfo info) TryDequeueEvent(bool checkOnly = false)157 {158 // Try to dequeue the next event, if there is one.159 var node = this.Queue.First;160 while (node != null)161 {162 // Iterates through the events and metadata in the inbox.163 var nextNode = node.Next;164 var currentEvent = node.Value;165 if (this.IsEventIgnored(currentEvent.e))166 {167 if (!checkOnly)168 {169 // Removes an ignored event.170 this.Queue.Remove(node);171 this.OnIgnoreEvent(currentEvent.e, currentEvent.eventGroup, currentEvent.info);172 }173 node = nextNode;174 continue;175 }176 else if (this.IsEventDeferred(currentEvent.e))177 {178 // Skips a deferred event.179 this.OnDeferEvent(currentEvent.e, currentEvent.eventGroup, currentEvent.info);180 node = nextNode;181 continue;182 }183 if (!checkOnly)184 {185 this.Queue.Remove(node);186 }187 return currentEvent;188 }189 return default;190 }191 /// <inheritdoc/>192 public void RaiseEvent(Event e, EventGroup eventGroup)193 {194 string stateName = this.Owner is StateMachine stateMachine ?195 NameResolver.GetStateNameForLogging(stateMachine.CurrentState) : string.Empty;196 var eventOrigin = new EventOriginInfo(this.Owner.Id, this.Owner.GetType().FullName, stateName);197 var info = new EventInfo(e, eventOrigin);198 this.RaisedEvent = (e, eventGroup, info);199 this.OnRaiseEvent(e, eventGroup, info);200 }201 /// <inheritdoc/>202 public Task<Event> ReceiveEventAsync(Type eventType, Func<Event, bool> predicate = null)203 {204 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>205 {206 { eventType, predicate }207 };208 return this.ReceiveEventAsync(eventWaitTypes);209 }210 /// <inheritdoc/>211 public Task<Event> ReceiveEventAsync(params Type[] eventTypes)212 {213 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>();214 foreach (var type in eventTypes)215 {216 eventWaitTypes.Add(type, null);217 }218 return this.ReceiveEventAsync(eventWaitTypes);219 }220 /// <inheritdoc/>221 public Task<Event> ReceiveEventAsync(params Tuple<Type, Func<Event, bool>>[] events)222 {223 var eventWaitTypes = new Dictionary<Type, Func<Event, bool>>();224 foreach (var e in events)225 {226 eventWaitTypes.Add(e.Item1, e.Item2);227 }228 return this.ReceiveEventAsync(eventWaitTypes);229 }230 /// <summary>231 /// Waits for an event to be enqueued.232 /// </summary>233 private Task<Event> ReceiveEventAsync(Dictionary<Type, Func<Event, bool>> eventWaitTypes)234 {235 this.OnReceiveInvoked();236 (Event e, EventGroup eventGroup, EventInfo info) receivedEvent = default;237 var node = this.Queue.First;238 while (node != null)239 {240 // Dequeue the first event that the caller waits to receive, if there is one in the queue.241 if (eventWaitTypes.TryGetValue(node.Value.e.GetType(), out Func<Event, bool> predicate) &&242 (predicate is null || predicate(node.Value.e)))243 {244 receivedEvent = node.Value;245 this.Queue.Remove(node);246 break;247 }248 node = node.Next;249 }250 if (receivedEvent == default)251 {252 this.ReceiveCompletionSource = new TaskCompletionSource<Event>();253 this.EventWaitTypes = eventWaitTypes;254 this.OnWaitEvent(this.EventWaitTypes.Keys);255 return this.ReceiveCompletionSource.Task;256 }257 this.OnReceiveEventWithoutWaiting(receivedEvent.e, receivedEvent.eventGroup, receivedEvent.info);258 return Task.FromResult(receivedEvent.e);259 }260 /// <summary>261 /// Checks if the specified event is currently ignored.262 /// </summary>263 [MethodImpl(MethodImplOptions.AggressiveInlining)]264 protected virtual bool IsEventIgnored(Event e) => this.Owner.IsEventIgnored(e);265 /// <summary>266 /// Checks if the specified event is currently deferred.267 /// </summary>268 [MethodImpl(MethodImplOptions.AggressiveInlining)]269 protected virtual bool IsEventDeferred(Event e) => this.Owner.IsEventDeferred(e);270 /// <summary>271 /// Checks if a default handler is currently available.272 /// </summary>273 [MethodImpl(MethodImplOptions.AggressiveInlining)]274 protected virtual bool IsDefaultHandlerAvailable()275 {276 bool result = this.Owner.IsDefaultHandlerInstalled();277 if (result)278 {279 this.Owner.Context.Scheduler.ScheduleNextOperation();280 }281 return result;282 }283 /// <summary>284 /// Notifies the actor that an event has been enqueued.285 /// </summary>286 [MethodImpl(MethodImplOptions.AggressiveInlining)]287 protected virtual void OnEnqueueEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>288 this.Owner.OnEnqueueEvent(e, eventGroup, eventInfo);289 /// <summary>290 /// Notifies the actor that an event has been raised.291 /// </summary>292 [MethodImpl(MethodImplOptions.AggressiveInlining)]293 protected virtual void OnRaiseEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>294 this.Owner.OnRaiseEvent(e, eventGroup, eventInfo);295 /// <summary>296 /// Notifies the actor that it is waiting to receive an event of one of the specified types.297 /// </summary>298 [MethodImpl(MethodImplOptions.AggressiveInlining)]299 protected virtual void OnWaitEvent(IEnumerable<Type> eventTypes) => this.Owner.OnWaitEvent(eventTypes);300 /// <summary>301 /// Notifies the actor that an event it was waiting to receive has been enqueued.302 /// </summary>303 [MethodImpl(MethodImplOptions.AggressiveInlining)]304 protected virtual void OnReceiveEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) =>305 this.Owner.OnReceiveEvent(e, eventGroup, eventInfo);306 /// <summary>307 /// Notifies the actor that an event it was waiting to receive was already in the308 /// event queue when the actor invoked the receive statement.309 /// </summary>310 [MethodImpl(MethodImplOptions.AggressiveInlining)]311 protected virtual void OnReceiveEventWithoutWaiting(Event e, EventGroup eventGroup, EventInfo eventInfo) =>312 this.Owner.OnReceiveEventWithoutWaiting(e, eventGroup, eventInfo);313 /// <summary>314 /// Notifies the actor that <see cref="ReceiveEventAsync(Type[])"/> or one of its overloaded methods was invoked.315 /// </summary>316 [MethodImpl(MethodImplOptions.AggressiveInlining)]317 protected virtual void OnReceiveInvoked() => this.Owner.OnReceiveInvoked();318 /// <summary>319 /// Notifies the actor that an event has been ignored.320 /// </summary>321 [MethodImpl(MethodImplOptions.AggressiveInlining)]322 protected virtual void OnIgnoreEvent(Event e, EventGroup eventGroup, EventInfo eventInfo) => this.Owner.OnIgnoreEvent(e);323 /// <summary>324 /// Notifies the actor that an event has been deferred.325 /// </summary>326 [MethodImpl(MethodImplOptions.AggressiveInlining)]...

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Threading.Tasks;4using Microsoft.Coyote;5using Microsoft.Coyote.Actors;6using Microsoft.Coyote.Actors.Mocks;7using Microsoft.Coyote.Specifications;8using Microsoft.Coyote.Tasks;9using Microsoft.Coyote.TestingServices;10{11 {12 static void Main(string[] args)13 {14 var configuration = Configuration.Create().WithTestingIterations(100);15 var test = new CoyoteTest(configuration, () =>16 {17 var runtime = RuntimeFactory.Create();18 var eventQueue = new MockEventQueue();19 runtime.RegisterEventQueue(eventQueue);20 runtime.CreateActor(typeof(Actor1));21 runtime.CreateActor(typeof(Actor2));22 runtime.CreateActor(typeof(Actor3));23 runtime.CreateActor(typeof(Actor4));24 runtime.Start();25 });26 test.Execute();27 }28 }29 {30 public int Value;31 public e(int value)32 {33 this.Value = value;34 }35 }36 {37 protected override Task OnInitializeAsync(Event initialEvent)38 {39 this.SendEvent(this.Id, new e(1));40 return Task.CompletedTask;41 }42 }43 {44 protected override Task OnInitializeAsync(Event initialEvent)45 {46 this.SendEvent(this.Id, new e(1));47 return Task.CompletedTask;48 }49 }50 {51 protected override Task OnInitializeAsync(Event initialEvent)52 {53 this.SendEvent(this.Id, new e(1));54 return Task.CompletedTask;55 }56 }57 {58 protected override Task OnInitializeAsync(Event initialEvent)59 {60 this.SendEvent(this.Id, new e(1));61 return Task.CompletedTask;62 }63 }64}

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.Specifications;7using Microsoft.Coyote.Tasks;8{9 {10 public static void Main(string[] args)11 {12 var runtime = RuntimeFactory.Create();13 runtime.RegisterMonitor(typeof(Monitor));14 runtime.RegisterMonitor(typeof(Monitor2));15 runtime.CreateActor(typeof(Producer));16 runtime.CreateActor(typeof(Consumer));17 runtime.CreateActor(typeof(Consumer2));18 runtime.CreateActor(typeof(Consumer3));19 runtime.Run();20 }21 }22 {23 [OnEventDoAction(typeof(OnReceiveEvent), nameof(OnReceive))]24 class Init : MonitorState { }25 private void OnReceive(Event e)26 {27 Console.WriteLine("Monitor received event {0}", e);28 }29 }30 {31 [OnEventDoAction(typeof(OnReceiveEvent), nameof(OnReceive))]32 class Init : MonitorState { }33 private void OnReceive(Event e)34 {35 Console.WriteLine("Monitor2 received event {0}", e);36 }37 }38 {39 private int count = 0;40 [OnEventDoAction(typeof(UnitEvent), nameof(Send))]41 private class Init : State { }42 private void Send()43 {44 this.count++;45 this.SendEvent(this.Id, new E(this.count));46 this.SendEvent(this.Id, new UnitEvent());47 }48 }49 {50 [OnEventDoAction(typeof(E), nameof(Receive))]51 private class Init : State { }52 private void Receive()53 {54 this.Assert(this.ReceivedEvent is E);55 Console.WriteLine("Consumer received event {0}", this.ReceivedEvent);56 }57 }58 {59 [OnEventDoAction(typeof(E), nameof(Receive))]60 private class Init : State { }61 private void Receive()62 {63 this.Assert(this.ReceivedEvent is E);64 Console.WriteLine("Consumer2 received event {0}", this.ReceivedEvent);65 }66 }

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.Specifications;7using Microsoft.Coyote.Tests.Common;8using Microsoft.Coyote.Tasks;9{10 {11 public static void Main(string[] args)12 {13 var runtime = RuntimeFactory.Create();14 var mockEventQueue = new MockEventQueue();15 var actor = runtime.CreateActor(typeof(Actor1), new ActorId(1), mockEventQueue);16 runtime.SendEvent(actor, new e1());17 runtime.SendEvent(actor, new e2());18 runtime.SendEvent(actor, new e3());19 runtime.SendEvent(actor, new e4());20 runtime.SendEvent(actor, new e5());21 runtime.SendEvent(actor, new e6());22 runtime.SendEvent(actor, new e7());23 runtime.SendEvent(actor, new e8());24 runtime.SendEvent(actor, new e9());25 runtime.SendEvent(actor, new e10());26 runtime.SendEvent(actor, new e11());27 runtime.SendEvent(actor, new e12());28 runtime.SendEvent(actor, new e13());29 runtime.SendEvent(actor, new e14());30 runtime.SendEvent(actor, new e15());31 runtime.SendEvent(actor, new e16());32 runtime.SendEvent(actor, new e17

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using Microsoft.Coyote;3using Microsoft.Coyote.Actors;4using Microsoft.Coyote.Actors.Mocks;5using Microsoft.Coyote.Specifications;6using Microsoft.Coyote.Tasks;7{8 {9 internal static void Main(string[] args)10 {11 var eventQueue = new MockEventQueue();12 var runtime = new MockRuntime(eventQueue);13 var actor = runtime.CreateActor(typeof(TestActor));14 runtime.SendEvent(actor, new TestEvent());15 var receivedEvent = eventQueue.Dequeue();16 Specification.Assert(receivedEvent is TestEvent, "Received event is not of the expected type.");17 }18 }19 {20 }21 {22 protected override Task OnInitializeAsync(Event initialEvent)23 {24 this.SendEvent(this.Id, new TestEvent());25 return Task.CompletedTask;26 }27 }28}29using System;30using Microsoft.Coyote;31using Microsoft.Coyote.Actors;32using Microsoft.Coyote.Actors.Mocks;33using Microsoft.Coyote.Specifications;34using Microsoft.Coyote.Tasks;35{36 {37 internal static void Main(string[] args)38 {39 var eventQueue = new MockEventQueue();40 var runtime = new MockRuntime(eventQueue);

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Collections.Generic;3using System.Text;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.Actors.Timers;7using Microsoft.Coyote.Actors.Timers.Mocks;8using Microsoft.Coyote.Specifications;9using Microsoft.Coyote.SystematicTesting;10using Microsoft.Coyote.SystematicTesting.Mocks;11using Microsoft.Coyote.SystematicTesting.Timers;12using Microsoft.Coyote.SystematicTesting.Timers.Mocks;13using Microsoft.Coyote.Tasks;14using Microsoft.Coyote.Tasks.Mocks;15using Microsoft.Coyote.Tasks.Timers;16using Microsoft.Coyote.Tasks.Timers.Mocks;17using Microsoft.Coyote.Tests.Common;18using Microsoft.Coyote.Tests.Common.Mocks;19using Microsoft.Coyote.Tests.Common.Timers;20using Microsoft.Coyote.Tests.Common.Timers.Mocks;21using Microsoft.Coyote.Tests.Common.Tasks;22using Microsoft.Coyote.Tests.Common.Tasks.Mocks;23using Microsoft.Coyote.Tests.Common.Tasks.Timers;24using Microsoft.Coyote.Tests.Common.Tasks.Timers.Mocks;25using Microsoft.Coyote.Tests.Common.Actors;26using Microsoft.Coyote.Tests.Common.Actors.Mocks;27using Microsoft.Coyote.Tests.Common.Actors.Timers;28using Microsoft.Coyote.Tests.Common.Actors.Timers.Mocks;29{30 {31 public MockEventQueue() { }32 public MockEventQueue(ActorId actorId) : base(actorId) { }33 public MockEventQueue(ActorId actorId, int maxEventCount) : base(actorId, maxEventCount) { }34 public MockEventQueue(ActorId actorId, int maxEventCount, int maxBufferedEventCount) : base(actorId, maxEventCount, maxBufferedEventCount) { }35 public MockEventQueue(ActorId actorId, int maxEventCount, int maxBufferedEventCount, int maxSchedulingSteps) : base(actorId, maxEventCount, maxBufferedEventCount, maxSchedulingSteps) { }36 public MockEventQueue(ActorId actorId, int maxEventCount, int maxBufferedEventCount, int maxSchedulingSteps, bool enableBoundedRandomExploration) : base(actorId, maxEventCount, maxBufferedEventCount, maxSchedulingSteps,

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1using System;2using System.Threading.Tasks;3using Microsoft.Coyote;4using Microsoft.Coyote.Actors;5using Microsoft.Coyote.Actors.Mocks;6using Microsoft.Coyote.Testing;7using Microsoft.Coyote.TestingServices;8using Microsoft.Coyote.TestingServices.Runtime;9using Microsoft.Coyote.Tests.Common;10using Microsoft.Coyote.Tests.Common.Actors;11using Microsoft.Coyote.Tests.Common.Runtime;12using Microsoft.Coyote.Tests.Common.TestingServices;13using Microsoft.Coyote.Tests.Common.TestingServices.Runtime;14using Microsoft.Coyote.Tests.Common.TestScenarios;15using Microsoft.Coyote.Tests.Common.Utilities;16using Microsoft.Coyote.Tests.Mocks;17using Microsoft.Coyote.Tests.TestScenarios;18using Microsoft.Coyote.Tests.Utilities;19using Xunit;20using Xunit.Abstractions;21{22 {23 public e1()24 {25 }26 }27 {28 [OnEventDoAction(typeof(e1), nameof(HandleEvent))]29 {30 }31 private void HandleEvent()32 {33 }34 }35 {36 private readonly ITestOutputHelper output;37 public UnitTest1(ITestOutputHelper output)38 {39 this.output = output;40 }41 [Fact(Timeout = 5000)]42 public void Test1()43 {44 var configuration = Configuration.Create();45 var runtime = TestingEngineFactory.Create(configuration, this.output);46 runtime.CreateActor(typeof(A));47 var actor = runtime.GetActor<A>();48 var e = new e1();49 runtime.SendEvent(actor, e);50 var queue = new MockEventQueue(runtime, actor);51 Assert.True(queue.OnReceiveEvent(e));52 }53 }54}

Full Screen

Full Screen

OnReceiveEvent

Using AI Code Generation

copy

Full Screen

1{2 protected override async Task OnInitializeAsync(Event initialEvent)3 {4 this.OnReceiveEvent += this.MyActor_OnReceiveEvent;5 }6 private void MyActor_OnReceiveEvent(object sender, Event e)7 {8 }9}10{11 protected override async Task OnInitializeAsync(Event initialEvent)12 {13 this.OnReceiveEvent += this.MyActor_OnReceiveEvent;14 }15 private void MyActor_OnReceiveEvent(object sender, Event e)16 {17 }18}19{20 protected override async Task OnInitializeAsync(Event initialEvent)21 {22 this.Runtime.OnReceiveEvent += this.Runtime_OnReceiveEvent;23 }24 private void Runtime_OnReceiveEvent(object sender, Event e)25 {26 }27}28{29 protected override async Task OnInitializeAsync(Event initialEvent)30 {31 this.Runtime.OnReceiveEvent += this.Runtime_OnReceiveEvent;32 }33 private void Runtime_OnReceiveEvent(object sender, Event e)34 {35 }36}37{38 protected override async Task OnInitializeAsync(Event initialEvent)39 {40 this.Runtime.OnReceiveEvent += this.Runtime_OnReceiveEvent;41 }42 private void Runtime_OnReceiveEvent(object sender, Event e)43 {44 }45}46{47 protected override async Task OnInitializeAsync(Event initialEvent)48 {49 this.Runtime.OnReceiveEvent += this.Runtime_OnReceiveEvent;50 }51 private void Runtime_OnReceiveEvent(object sender, Event e)52 {

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