Best Coyote code snippet using Microsoft.Coyote.Actors.Tests.E.Configure
BaseTest.cs
Source:BaseTest.cs  
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3using System;4using System.Diagnostics;5using System.IO;6using System.Linq;7using System.Text;8using System.Threading.Tasks;9using Microsoft.Coyote.Actors;10using Microsoft.Coyote.Actors.Coverage;11using Microsoft.Coyote.IO;12using Microsoft.Coyote.SystematicTesting;13using Microsoft.Coyote.SystematicTesting.Strategies;14using Xunit;15using Xunit.Abstractions;16using CoyoteTasks = Microsoft.Coyote.Tasks;17namespace Microsoft.Coyote.Tests.Common18{19    public delegate void TestErrorChecker(string error);20    public abstract class BaseTest21    {22        protected readonly ITestOutputHelper TestOutput;23        public BaseTest(ITestOutputHelper output)24        {25            this.TestOutput = output;26        }27        /// <summary>28        /// Override to true to run a systematic test under the <see cref="TestingEngine"/>.29        /// By default this value is false.30        /// </summary>31        protected virtual bool IsSystematicTest => false;32        protected void Test(Action test, Configuration configuration = null)33        {34            if (this.IsSystematicTest)35            {36                this.InternalTest(test, configuration);37            }38            else39            {40                this.Run((r) => test(), configuration);41            }42        }43        protected void Test(Action<IActorRuntime> test, Configuration configuration = null)44        {45            if (this.IsSystematicTest)46            {47                this.InternalTest(test, configuration);48            }49            else50            {51                this.Run(test, configuration);52            }53        }54        protected void Test(Func<Task> test, Configuration configuration = null)55        {56            if (this.IsSystematicTest)57            {58                this.InternalTest(test, configuration);59            }60            else61            {62                this.RunAsync(async (r) => await test(), configuration).Wait();63            }64        }65        protected void Test(Func<IActorRuntime, Task> test, Configuration configuration = null)66        {67            if (this.IsSystematicTest)68            {69                this.InternalTest(test, configuration);70            }71            else72            {73                this.RunAsync(test, configuration).Wait();74            }75        }76        protected string TestCoverage(Action<IActorRuntime> test, Configuration configuration)77        {78            var engine = this.InternalTest(test, configuration);79            using var writer = new StringWriter();80            var activityCoverageReporter = new ActivityCoverageReporter(engine.TestReport.CoverageInfo);81            activityCoverageReporter.WriteCoverageText(writer);82            string result = writer.ToString().RemoveNamespaceReferences();83            return result;84        }85        private TestingEngine InternalTest(Delegate test, Configuration configuration)86        {87            configuration ??= GetConfiguration();88            ILogger logger = this.GetLogger(configuration);89            TestingEngine engine = null;90            try91            {92                engine = RunTest(test, configuration, logger);93                var numErrors = engine.TestReport.NumOfFoundBugs;94                Assert.True(numErrors is 0, GetBugReport(engine));95            }96            catch (Exception ex)97            {98                Assert.False(true, ex.Message + "\n" + ex.StackTrace);99            }100            finally101            {102                logger.Dispose();103            }104            return engine;105        }106        protected void TestWithError(Action test, Configuration configuration = null, string expectedError = null,107            bool replay = false)108        {109            if (this.IsSystematicTest)110            {111                this.TestWithErrors(test, configuration, (e) => { CheckSingleError(e, expectedError); }, replay);112            }113            else114            {115                this.RunWithErrors((r) => test(), configuration, (e) => { CheckSingleError(e, expectedError); });116            }117        }118        protected void TestWithError(Action<IActorRuntime> test, Configuration configuration = null,119            string expectedError = null, bool replay = false)120        {121            if (this.IsSystematicTest)122            {123                this.TestWithErrors(test, configuration, (e) => { CheckSingleError(e, expectedError); }, replay);124            }125            else126            {127                this.RunWithErrors(test, configuration, (e) => { CheckSingleError(e, expectedError); });128            }129        }130        protected void TestWithError(Func<Task> test, Configuration configuration = null, string expectedError = null,131            bool replay = false)132        {133            if (this.IsSystematicTest)134            {135                this.TestWithErrors(test, configuration, (e) => { CheckSingleError(e, expectedError); }, replay);136            }137            else138            {139                this.RunWithErrorsAsync(async (r) => await test(), configuration, (e) => { CheckSingleError(e, expectedError); }).Wait();140            }141        }142        protected void TestWithError(Func<IActorRuntime, Task> test, Configuration configuration = null,143            string expectedError = null, bool replay = false)144        {145            if (this.IsSystematicTest)146            {147                this.TestWithErrors(test, configuration, (e) => { CheckSingleError(e, expectedError); }, replay);148            }149            else150            {151                this.RunWithErrorsAsync(test, configuration, (e) => { CheckSingleError(e, expectedError); }).Wait();152            }153        }154        protected void TestWithError(Action test, Configuration configuration = null, string[] expectedErrors = null,155            bool replay = false)156        {157            if (this.IsSystematicTest)158            {159                this.TestWithErrors(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }, replay);160            }161            else162            {163                this.RunWithErrors((r) => test(), configuration, (e) => { CheckMultipleErrors(e, expectedErrors); });164            }165        }166        protected void TestWithError(Action<IActorRuntime> test, Configuration configuration = null,167            string[] expectedErrors = null, bool replay = false)168        {169            if (this.IsSystematicTest)170            {171                this.TestWithErrors(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }, replay);172            }173            else174            {175                this.RunWithErrors(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); });176            }177        }178        protected void TestWithError(Func<Task> test, Configuration configuration = null, string[] expectedErrors = null,179            bool replay = false)180        {181            if (this.IsSystematicTest)182            {183                this.TestWithErrors(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }, replay);184            }185            else186            {187                this.RunWithErrorsAsync(async (r) => await test(), configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }).Wait();188            }189        }190        protected void TestWithError(Func<IActorRuntime, Task> test, Configuration configuration = null,191            string[] expectedErrors = null, bool replay = false)192        {193            if (this.IsSystematicTest)194            {195                this.TestWithErrors(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }, replay);196            }197            else198            {199                this.RunWithErrorsAsync(test, configuration, (e) => { CheckMultipleErrors(e, expectedErrors); }).Wait();200            }201        }202        protected void TestWithError(Action test, TestErrorChecker errorChecker, Configuration configuration = null,203            bool replay = false)204        {205            if (this.IsSystematicTest)206            {207                this.TestWithErrors(test, configuration, errorChecker, replay);208            }209            else210            {211                this.RunWithErrors((r) => test(), configuration, errorChecker);212            }213        }214        protected void TestWithError(Action<IActorRuntime> test, TestErrorChecker errorChecker, Configuration configuration = null,215            bool replay = false)216        {217            if (this.IsSystematicTest)218            {219                this.TestWithErrors(test, configuration, errorChecker, replay);220            }221            else222            {223                this.RunWithErrors(test, configuration, errorChecker);224            }225        }226        protected void TestWithError(Func<Task> test, TestErrorChecker errorChecker, Configuration configuration = null,227            bool replay = false)228        {229            if (this.IsSystematicTest)230            {231                this.TestWithErrors(test, configuration, errorChecker, replay);232            }233            else234            {235                this.RunWithErrorsAsync(async (r) => await test(), configuration, errorChecker).Wait();236            }237        }238        protected void TestWithError(Func<IActorRuntime, Task> test, TestErrorChecker errorChecker, Configuration configuration = null,239            bool replay = false)240        {241            if (this.IsSystematicTest)242            {243                this.TestWithErrors(test, configuration, errorChecker, replay);244            }245            else246            {247                this.RunWithErrorsAsync(test, configuration, errorChecker).Wait();248            }249        }250        private void TestWithErrors(Delegate test, Configuration configuration, TestErrorChecker errorChecker, bool replay)251        {252            configuration ??= GetConfiguration();253            ILogger logger = this.GetLogger(configuration);254            try255            {256                var engine = RunTest(test, configuration, logger);257                CheckErrors(engine, errorChecker);258                if (replay)259                {260                    configuration.WithReplayStrategy(engine.ReproducibleTrace);261                    engine = RunTest(test, configuration, logger);262                    string replayError = (engine.Strategy as ReplayStrategy).ErrorText;263                    Assert.True(replayError.Length is 0, replayError);264                    CheckErrors(engine, errorChecker);265                }266            }267            catch (Exception ex)268            {269                Assert.False(true, ex.Message + "\n" + ex.StackTrace);270            }271            finally272            {273                logger.Dispose();274            }275        }276        protected void TestWithException<TException>(Action test, Configuration configuration = null, bool replay = false)277            where TException : Exception278        {279            if (this.IsSystematicTest)280            {281                this.InternalTestWithException<TException>(test, configuration, replay);282            }283            else284            {285                this.RunWithException<TException>(test, configuration);286            }287        }288        protected void TestWithException<TException>(Action<IActorRuntime> test, Configuration configuration = null,289            bool replay = false)290            where TException : Exception291        {292            if (this.IsSystematicTest)293            {294                this.InternalTestWithException<TException>(test, configuration, replay);295            }296            else297            {298                this.RunWithException<TException>(test, configuration);299            }300        }301        protected void TestWithException<TException>(Func<Task> test, Configuration configuration = null, bool replay = false)302            where TException : Exception303        {304            if (this.IsSystematicTest)305            {306                this.InternalTestWithException<TException>(test, configuration, replay);307            }308            else309            {310                this.RunWithExceptionAsync<TException>(test, configuration).Wait();311            }312        }313        protected void TestWithException<TException>(Func<IActorRuntime, Task> test, Configuration configuration = null,314            bool replay = false)315            where TException : Exception316        {317            if (this.IsSystematicTest)318            {319                this.InternalTestWithException<TException>(test, configuration, replay);320            }321            else322            {323                this.RunWithExceptionAsync<TException>(test, configuration).Wait();324            }325        }326        private void InternalTestWithException<TException>(Delegate test, Configuration configuration = null, bool replay = false)327            where TException : Exception328        {329            configuration ??= GetConfiguration();330            Type exceptionType = typeof(TException);331            Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " +332                $"Type '{exceptionType}' is not an exception type.");333            ILogger logger = this.GetLogger(configuration);334            try335            {336                var engine = RunTest(test, configuration, logger);337                CheckErrors(engine, exceptionType);338                if (replay)339                {340                    configuration.SchedulingStrategy = "replay";341                    configuration.ScheduleTrace = engine.ReproducibleTrace;342                    engine = RunTest(test, configuration, logger);343                    string replayError = (engine.Strategy as ReplayStrategy).ErrorText;344                    Assert.True(replayError.Length is 0, replayError);345                    CheckErrors(engine, exceptionType);346                }347            }348            catch (Exception ex)349            {350                Assert.False(true, ex.Message + "\n" + ex.StackTrace);351            }352            finally353            {354                logger.Dispose();355            }356        }357        protected void Run(Action<IActorRuntime> test, Configuration configuration = null)358        {359            configuration ??= GetConfiguration();360            ILogger logger = this.GetLogger(configuration);361            try362            {363                configuration.IsMonitoringEnabledInInProduction = true;364                var runtime = RuntimeFactory.Create(configuration);365                runtime.Logger = logger;366                for (int i = 0; i < configuration.TestingIterations; i++)367                {368                    test(runtime);369                }370            }371            catch (Exception ex)372            {373                Assert.False(true, ex.Message + "\n" + ex.StackTrace);374            }375            finally376            {377                logger.Dispose();378            }379        }380        protected async Task RunAsync(Func<IActorRuntime, Task> test, Configuration configuration = null, bool handleFailures = true)381        {382            configuration ??= GetConfiguration();383            uint iterations = Math.Max(1, configuration.TestingIterations);384            for (int i = 0; i < iterations; i++)385            {386                ILogger logger = this.GetLogger(configuration);387                try388                {389                    configuration.IsMonitoringEnabledInInProduction = true;390                    var runtime = RuntimeFactory.Create(configuration);391                    runtime.Logger = logger;392                    var errorTask = new TaskCompletionSource<Exception>();393                    if (handleFailures)394                    {395                        runtime.OnFailure += (e) =>396                        {397                            errorTask.TrySetResult(Unwrap(e));398                        };399                    }400                    // BUGBUG: but is this actually letting the test complete in the case401                    // of actors which run completely asynchronously?402                    await Task.WhenAny(test(runtime), errorTask.Task);403                    if (handleFailures && errorTask.Task.IsCompleted)404                    {405                        Assert.False(true, errorTask.Task.Result.Message);406                    }407                }408                catch (Exception ex)409                {410                    Exception e = Unwrap(ex);411                    Assert.False(true, e.Message + "\n" + e.StackTrace);412                }413                finally414                {415                    logger.Dispose();416                }417            }418        }419        private static Exception Unwrap(Exception ex)420        {421            Exception e = ex;422            if (e is AggregateException ae)423            {424                e = ae.InnerException;425            }426            else if (e is ActionExceptionFilterException fe)427            {428                e = fe.InnerException;429            }430            return e;431        }432        private static string ExtractErrorMessage(Exception ex)433        {434            if (ex is ActionExceptionFilterException actionException)435            {436                ex = actionException.InnerException;437            }438            var msg = ex.Message;439            if (ex is AggregateException ae)440            {441                StringBuilder sb = new StringBuilder();442                foreach (var e in ae.InnerExceptions)443                {444                    sb.AppendLine(e.Message);445                }446                msg = sb.ToString();447            }448            return msg;449        }450        private void RunWithErrors(Action<IActorRuntime> test, Configuration configuration, TestErrorChecker errorChecker)451        {452            configuration ??= GetConfiguration();453            string errorMessage = string.Empty;454            ILogger logger = this.GetLogger(configuration);455            try456            {457                configuration.IsMonitoringEnabledInInProduction = true;458                var runtime = RuntimeFactory.Create(configuration);459                var errorTask = new TaskCompletionSource<Exception>();460                runtime.OnFailure += (e) =>461                {462                    errorTask.TrySetResult(e);463                };464                runtime.Logger = logger;465                for (int i = 0; i < configuration.TestingIterations; i++)466                {467                    test(runtime);468                    if (configuration.TestingIterations is 1)469                    {470                        Assert.True(errorTask.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");471                        errorMessage = ExtractErrorMessage(errorTask.Task.Result);472                    }473                }474            }475            catch (Exception ex)476            {477                errorMessage = ExtractErrorMessage(ex);478            }479            finally480            {481                logger.Dispose();482            }483            if (string.IsNullOrEmpty(errorMessage))484            {485                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));486            }487            errorChecker(errorMessage);488        }489        private async Task RunWithErrorsAsync(Func<IActorRuntime, Task> test, Configuration configuration, TestErrorChecker errorChecker)490        {491            configuration ??= GetConfiguration();492            string errorMessage = string.Empty;493            ILogger logger = this.GetLogger(configuration);494            try495            {496                configuration.IsMonitoringEnabledInInProduction = true;497                var runtime = RuntimeFactory.Create(configuration);498                var errorCompletion = new TaskCompletionSource<Exception>();499                runtime.OnFailure += (e) =>500                {501                    errorCompletion.TrySetResult(e);502                };503                runtime.Logger = logger;504                for (int i = 0; i < configuration.TestingIterations; i++)505                {506                    await test(runtime);507                    if (configuration.TestingIterations is 1)508                    {509                        Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");510                        errorMessage = ExtractErrorMessage(errorCompletion.Task.Result);511                    }512                }513            }514            catch (Exception ex)515            {516                errorMessage = ExtractErrorMessage(ex);517            }518            finally519            {520                logger.Dispose();521            }522            if (string.IsNullOrEmpty(errorMessage))523            {524                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));525            }526            errorChecker(errorMessage);527        }528        protected void RunWithException<TException>(Action<IActorRuntime> test, Configuration configuration = null)529        {530            configuration ??= GetConfiguration();531            Exception actualException = null;532            Type exceptionType = typeof(TException);533            Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " +534                $"Type '{exceptionType}' is not an exception type.");535            ILogger logger = this.GetLogger(configuration);536            try537            {538                configuration.IsMonitoringEnabledInInProduction = true;539                var runtime = RuntimeFactory.Create(configuration);540                var errorCompletion = new TaskCompletionSource<Exception>();541                runtime.OnFailure += (e) =>542                {543                    errorCompletion.TrySetResult(e);544                };545                runtime.Logger = logger;546                for (int i = 0; i < configuration.TestingIterations; i++)547                {548                    test(runtime);549                    if (configuration.TestingIterations is 1)550                    {551                        Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");552                        actualException = errorCompletion.Task.Result;553                    }554                }555            }556            catch (Exception ex)557            {558                actualException = ex;559            }560            finally561            {562                logger.Dispose();563            }564            if (actualException is null)565            {566                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));567            }568            Assert.True(actualException.GetType() == exceptionType, actualException.Message + "\n" + actualException.StackTrace);569        }570        protected void RunWithException<TException>(Action test, Configuration configuration = null)571        {572            configuration ??= GetConfiguration();573            Exception actualException = null;574            Type exceptionType = typeof(TException);575            Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " +576                $"Type '{exceptionType}' is not an exception type.");577            ILogger logger = this.GetLogger(configuration);578            try579            {580                configuration.IsMonitoringEnabledInInProduction = true;581                var runtime = RuntimeFactory.Create(configuration);582                var errorCompletion = new TaskCompletionSource<Exception>();583                runtime.OnFailure += (e) =>584                {585                    errorCompletion.TrySetResult(e);586                };587                runtime.Logger = logger;588                for (int i = 0; i < configuration.TestingIterations; i++)589                {590                    test();591                    if (configuration.TestingIterations is 1)592                    {593                        Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");594                        actualException = errorCompletion.Task.Result;595                    }596                }597            }598            catch (Exception ex)599            {600                actualException = ex;601            }602            finally603            {604                logger.Dispose();605            }606            if (actualException is null)607            {608                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));609            }610            Assert.True(actualException.GetType() == exceptionType, actualException.Message + "\n" + actualException.StackTrace);611        }612        protected async Task RunWithExceptionAsync<TException>(Func<IActorRuntime, Task> test, Configuration configuration = null)613        {614            configuration ??= GetConfiguration();615            Exception actualException = null;616            Type exceptionType = typeof(TException);617            Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " +618                $"Type '{exceptionType}' is not an exception type.");619            ILogger logger = this.GetLogger(configuration);620            try621            {622                configuration.IsMonitoringEnabledInInProduction = true;623                var runtime = RuntimeFactory.Create(configuration);624                var errorCompletion = new TaskCompletionSource<Exception>();625                runtime.OnFailure += (e) =>626                {627                    errorCompletion.TrySetResult(e);628                };629                runtime.Logger = logger;630                for (int i = 0; i < configuration.TestingIterations; i++)631                {632                    await test(runtime);633                    if (configuration.TestingIterations is 1)634                    {635                        Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");636                        actualException = errorCompletion.Task.Result;637                    }638                }639            }640            catch (Exception ex)641            {642                actualException = ex;643            }644            finally645            {646                logger.Dispose();647            }648            if (actualException is null)649            {650                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));651            }652            Assert.True(actualException.GetType() == exceptionType, actualException.Message + "\n" + actualException.StackTrace);653        }654        protected async Task RunWithExceptionAsync<TException>(Func<Task> test, Configuration configuration = null)655        {656            configuration ??= GetConfiguration();657            Exception actualException = null;658            Type exceptionType = typeof(TException);659            Assert.True(exceptionType.IsSubclassOf(typeof(Exception)), "Please configure the test correctly. " +660                $"Type '{exceptionType}' is not an exception type.");661            ILogger logger = this.GetLogger(configuration);662            try663            {664                configuration.IsMonitoringEnabledInInProduction = true;665                var runtime = RuntimeFactory.Create(configuration);666                var errorCompletion = new TaskCompletionSource<Exception>();667                runtime.OnFailure += (e) =>668                {669                    errorCompletion.TrySetResult(e);670                };671                runtime.Logger = logger;672                for (int i = 0; i < configuration.TestingIterations; i++)673                {674                    await test();675                    if (configuration.TestingIterations is 1)676                    {677                        Assert.True(errorCompletion.Task.Wait(GetExceptionTimeout()), "Timeout waiting for error");678                        actualException = errorCompletion.Task.Result;679                    }680                }681            }682            catch (Exception ex)683            {684                actualException = ex;685            }686            finally687            {688                logger.Dispose();689            }690            if (actualException is null)691            {692                Assert.True(false, string.Format("Error not found after all {0} test iterations", configuration.TestingIterations));693            }694            Assert.True(actualException.GetType() == exceptionType, actualException.Message + "\n" + actualException.StackTrace);695        }696        private ILogger GetLogger(Configuration configuration)697        {698            ILogger logger;699            if (configuration.IsVerbose)700            {701                logger = new TestOutputLogger(this.TestOutput, true);702            }703            else704            {705                logger = new NullLogger();706            }707            return logger;708        }709        private static int GetExceptionTimeout(int millisecondsDelay = 5000)710        {711            if (Debugger.IsAttached)712            {713                millisecondsDelay = 500000;714            }715            return millisecondsDelay;716        }717        protected async CoyoteTasks.Task WaitAsync(CoyoteTasks.Task task, int millisecondsDelay = 5000)718        {719            millisecondsDelay = GetExceptionTimeout(millisecondsDelay);720            if (this.IsSystematicTest)721            {722                // The TestEngine will throw a Deadlock exception if this task can't possibly complete.723                await task;724            }725            else726            {727                await CoyoteTasks.Task.WhenAny(task, CoyoteTasks.Task.Delay(millisecondsDelay));728            }729            if (task.IsFaulted)730            {731                // unwrap the AggregateException so unit tests can more easily732                // Assert.Throws to match a more specific inner exception.733                throw task.Exception.InnerException;734            }735            Assert.True(task.IsCompleted);736        }737        protected async CoyoteTasks.Task<TResult> GetResultAsync<TResult>(CoyoteTasks.TaskCompletionSource<TResult> tcs, int millisecondsDelay = 5000)738        {739            return await this.GetResultAsync(tcs.Task, millisecondsDelay);740        }741        protected async CoyoteTasks.Task<TResult> GetResultAsync<TResult>(CoyoteTasks.Task<TResult> task, int millisecondsDelay = 5000)742        {743            millisecondsDelay = GetExceptionTimeout(millisecondsDelay);744            if (this.IsSystematicTest)745            {746                // The TestEngine will throw a Deadlock exception if this task can't possibly complete.747                await task;748            }749            else750            {751                await CoyoteTasks.Task.WhenAny(task, CoyoteTasks.Task.Delay(millisecondsDelay));752            }753            if (task.IsFaulted)754            {755                // unwrap the AggregateException so unit tests can more easily756                // Assert.Throws to match a more specific inner exception.757                throw task.Exception.InnerException;758            }759            Assert.True(task.IsCompleted, string.Format("Task timed out after '{0}' milliseconds", millisecondsDelay));760            return await task;761        }762        private static TestingEngine RunTest(Delegate test, Configuration configuration, ILogger logger)763        {764            var engine = new TestingEngine(configuration, test)765            {766                Logger = logger767            };768            engine.Run();769            return engine;770        }771        private static void CheckSingleError(string actual, string expected)772        {773            var a = actual.RemoveNonDeterministicValues();774            var b = expected.RemoveNonDeterministicValues();775            Assert.Equal(b, a);776        }777        private static void CheckMultipleErrors(string actual, string[] expectedErrors)778        {779            var stripped = actual.RemoveNonDeterministicValues();780            try781            {782                Assert.Contains(expectedErrors, (e) => e.RemoveNonDeterministicValues() == stripped);783            }784            catch (Exception)785            {786                throw new Exception("Actual string was not in the expected list: " + actual);787            }788        }789        private static void CheckErrors(TestingEngine engine, TestErrorChecker errorChecker)790        {791            Assert.True(engine.TestReport.NumOfFoundBugs > 0, "Expected bugs to be found, but we found none");792            foreach (var bugReport in engine.TestReport.BugReports)793            {794                errorChecker(bugReport);795            }796        }797        private static void CheckErrors(TestingEngine engine, Type exceptionType)798        {799            Assert.Equal(1, engine.TestReport.NumOfFoundBugs);800            Assert.Contains(exceptionType.FullName,801                engine.TestReport.BugReports.First().Split(new[] { '\r', '\n' }).FirstOrDefault());802        }803        /// <summary>804        /// Throw an exception of the specified type.805        /// </summary>806        /// <typeparam name="T">The type of the exception.</typeparam>807        protected static void ThrowException<T>()808            where T : Exception, new() =>809            throw new T();810        protected static Configuration GetConfiguration()811        {812            return Configuration.Create().WithTelemetryEnabled(false);813        }814        protected static string GetBugReport(TestingEngine engine)815        {816            string report = string.Empty;817            foreach (var bug in engine.TestReport.BugReports)818            {819                report += bug + "\n";820            }821            return report;822        }823    }824}...ActorTaskDelayTests.cs
Source:ActorTaskDelayTests.cs  
...67        {68            protected override async Task OnInitializeAsync(Event initialEvent)69            {70                this.SendEvent(this.Id, UnitEvent.Instance);71                await Task.Delay(10).ConfigureAwait(false);72                this.SendEvent(this.Id, UnitEvent.Instance);73            }74#pragma warning disable CA1822 // Mark members as static75            private void IgnoreUnitEvent()76#pragma warning restore CA1822 // Mark members as static77            {78            }79        }80        [Fact(Timeout = 5000)]81        public void TestDelayWithOtherSynchronizationContextInActor()82        {83            this.Test(r =>84            {85                r.CreateActor(typeof(A2));86            },87            configuration: GetConfiguration().WithTestingIterations(100));88        }89        private class M2 : StateMachine90        {91            [Start]92            [OnEntry(nameof(InitOnEntry))]93            [IgnoreEvents(typeof(UnitEvent))]94            private class Init : State95            {96            }97            private async Task InitOnEntry()98            {99                this.SendEvent(this.Id, UnitEvent.Instance);100                await Task.Delay(10).ConfigureAwait(false);101                this.SendEvent(this.Id, UnitEvent.Instance);102            }103        }104        [Fact(Timeout = 5000)]105        public void TestDelayWithOtherSynchronizationContextInStateMachine()106        {107            this.Test(r =>108            {109                r.CreateActor(typeof(M2));110            },111            configuration: GetConfiguration().WithTestingIterations(100));112        }113        private class A3 : Actor114        {115            protected override async Task OnInitializeAsync(Event initialEvent)116            {117                Task[] tasks = new Task[2];118                for (int i = 0; i < 2; i++)119                {120                    tasks[i] = this.DelayedRandomAsync();121                }122                await Task.WhenAll(tasks);123            }124            private async Task DelayedRandomAsync()125            {126                await Task.Delay(10).ConfigureAwait(false);127                this.RandomBoolean();128            }129        }130        [Fact(Timeout = 5000)]131        public void TestDelayLoopWithOtherSynchronizationContextInActor()132        {133            this.Test(r =>134            {135                r.CreateActor(typeof(A3));136            },137            configuration: GetConfiguration().WithTestingIterations(100));138        }139        private class M3 : StateMachine140        {141            [Start]142            [OnEntry(nameof(InitOnEntry))]143            private class Init : State144            {145            }146            private async Task InitOnEntry()147            {148                Task[] tasks = new Task[2];149                for (int i = 0; i < 2; i++)150                {151                    tasks[i] = this.DelayedRandomAsync();152                }153                await Task.WhenAll(tasks);154            }155            private async Task DelayedRandomAsync()156            {157                await Task.Delay(10).ConfigureAwait(false);158                this.RandomBoolean();159            }160        }161        [Fact(Timeout = 5000)]162        public void TestDelayLoopWithOtherSynchronizationContextInStateMachine()163        {164            this.Test(r =>165            {166                r.CreateActor(typeof(M3));167            },168            configuration: GetConfiguration().WithTestingIterations(100));169        }170        [OnEventDoAction(typeof(UnitEvent), nameof(IgnoreUnitEvent))]171        private class A4 : Actor172        {173            protected override async Task OnInitializeAsync(Event initialEvent)174            {175                this.SendEvent(this.Id, UnitEvent.Instance);176                await AsyncProvider.DelayAsync(10);177                this.SendEvent(this.Id, UnitEvent.Instance);178            }179#pragma warning disable CA1822 // Mark members as static180            private void IgnoreUnitEvent()181#pragma warning restore CA1822 // Mark members as static182            {183            }184        }185        [Fact(Timeout = 5000)]186        public void TestUncontrolledDelayInActor()187        {188            this.TestWithError(r =>189            {190                r.CreateActor(typeof(A4));191            },192            configuration: GetConfiguration().WithTestingIterations(100),193            errorChecker: (e) =>194            {195                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);196            });197        }198        private class M4 : StateMachine199        {200            [Start]201            [OnEntry(nameof(InitOnEntry))]202            [IgnoreEvents(typeof(UnitEvent))]203            private class Init : State204            {205            }206            private async Task InitOnEntry()207            {208                this.SendEvent(this.Id, UnitEvent.Instance);209                await AsyncProvider.DelayAsync(10);210                this.SendEvent(this.Id, UnitEvent.Instance);211            }212        }213        [Fact(Timeout = 5000)]214        public void TestUncontrolledDelayInStateMachine()215        {216            this.TestWithError(r =>217            {218                r.CreateActor(typeof(M4));219            },220            configuration: GetConfiguration().WithTestingIterations(100),221            errorChecker: (e) =>222            {223                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);224            });225        }226        [OnEventDoAction(typeof(UnitEvent), nameof(IgnoreUnitEvent))]227        private class A5 : Actor228        {229            protected override async Task OnInitializeAsync(Event initialEvent)230            {231                this.SendEvent(this.Id, UnitEvent.Instance);232                await AsyncProvider.DelayAsync(10).ConfigureAwait(false);233                this.SendEvent(this.Id, UnitEvent.Instance);234            }235#pragma warning disable CA1822 // Mark members as static236            private void IgnoreUnitEvent()237#pragma warning restore CA1822 // Mark members as static238            {239            }240        }241        [Fact(Timeout = 5000)]242        public void TestUncontrolledDelayWithOtherSynchronizationContextInActor()243        {244            this.TestWithError(r =>245            {246                r.CreateActor(typeof(A5));247            },248            configuration: GetConfiguration().WithTestingIterations(100),249            errorChecker: (e) =>250            {251                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);252            });253        }254        private class M5 : StateMachine255        {256            [Start]257            [OnEntry(nameof(InitOnEntry))]258            [IgnoreEvents(typeof(UnitEvent))]259            private class Init : State260            {261            }262            private async Task InitOnEntry()263            {264                this.SendEvent(this.Id, UnitEvent.Instance);265                await AsyncProvider.DelayAsync(10).ConfigureAwait(false);266                this.SendEvent(this.Id, UnitEvent.Instance);267            }268        }269        [Fact(Timeout = 5000)]270        public void TestUncontrolledDelayWithOtherSynchronizationContextInStateMachine()271        {272            this.TestWithError(r =>273            {274                r.CreateActor(typeof(M5));275            },276            configuration: GetConfiguration().WithTestingIterations(100),277            errorChecker: (e) =>278            {279                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);280            });281        }282        private class A6 : Actor283        {284            protected override async Task OnInitializeAsync(Event initialEvent)285            {286                Task[] tasks = new Task[2];287                for (int i = 0; i < 2; i++)288                {289                    tasks[i] = this.DelayedRandomAsync();290                }291                await Task.WhenAll(tasks);292            }293            private async Task DelayedRandomAsync()294            {295                await AsyncProvider.DelayAsync(10).ConfigureAwait(false);296                this.RandomBoolean();297            }298        }299        [Fact(Timeout = 5000)]300        public void TestUncontrolledDelayLoopWithOtherSynchronizationContextInActor()301        {302            this.TestWithError(r =>303            {304                r.CreateActor(typeof(A6));305            },306            configuration: GetConfiguration().WithTestingIterations(100),307            errorChecker: (e) =>308            {309                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);310            });311        }312        private class M6 : StateMachine313        {314            [Start]315            [OnEntry(nameof(InitOnEntry))]316            private class Init : State317            {318            }319            private async Task InitOnEntry()320            {321                Task[] tasks = new Task[2];322                for (int i = 0; i < 2; i++)323                {324                    tasks[i] = this.DelayedRandomAsync();325                }326                await Task.WhenAll(tasks);327            }328            private async Task DelayedRandomAsync()329            {330                await AsyncProvider.DelayAsync(10).ConfigureAwait(false);331                this.RandomBoolean();332            }333        }334        [Fact(Timeout = 5000)]335        public void TestUncontrolledDelayLoopWithOtherSynchronizationContextInStateMachine()336        {337            this.TestWithError(r =>338            {339                r.CreateActor(typeof(M6));340            },341            configuration: GetConfiguration().WithTestingIterations(100),342            errorChecker: (e) =>343            {344                Assert.StartsWith($"Method '{ExpectedMethodName}' returned an uncontrolled task", e);...MemoryLeakTests.cs
Source:MemoryLeakTests.cs  
...74        {75            private int[] Buffer;76            private SetupEvent Setup;77            [Start]78            [OnEntry(nameof(Configure))]79            [OnEventDoAction(typeof(E), nameof(Act))]80            private class Init : State81            {82            }83            private void Configure(Event e)84            {85                this.Setup = (SetupEvent)e;86                this.Buffer = new int[10000];87                this.Buffer[this.Buffer.Length - 1] = 1;88                this.Setup.Add(this.Buffer);89            }90            private void Act(Event e)91            {92                var sender = (e as E).Id;93                var send = new E(this.Id);94                this.Setup.Add(send.Buffer);95                this.SendEvent(sender, new E(this.Id));96                if (this.Setup.HaltTest)97                {...Configure
Using AI Code Generation
1{2    static void Main(string[] args)3    {4        Coyote.Actors.Tests.E.Configure();5    }6}7{8    static void Main(string[] args)9    {10        Coyote.Actors.Tests.E.Configure();11    }12}13{14    static void Main(string[] args)15    {16        Coyote.Actors.Tests.E.Configure();17    }18}19{20    static void Main(string[] args)21    {22        Coyote.Actors.Tests.E.Configure();23    }24}25{26    static void Main(string[] args)27    {28        Coyote.Actors.Tests.E.Configure();29    }30}31{32    static void Main(string[] args)33    {34        Coyote.Actors.Tests.E.Configure();35    }36}37{38    static void Main(string[] args)39    {40        Coyote.Actors.Tests.E.Configure();41    }42}43{44    static void Main(string[] args)45    {46        Coyote.Actors.Tests.E.Configure();47    }48}49{50    static void Main(string[] args)51    {52        Coyote.Actors.Tests.E.Configure();53    }54}55{56    static void Main(string[] args)57    {58        Coyote.Actors.Tests.E.Configure();59    }60}Configure
Using AI Code Generation
1using Microsoft.Coyote.Actors;2using Microsoft.Coyote.Actors.Tests;3{4{5public static void Main(string[] args)6{7E.Configure();8}9}10}11using Microsoft.Coyote.Actors;12using Microsoft.Coyote.Actors.Tests;13{14{15public static void Main(string[] args)16{17Microsoft.Coyote.Actors.Tests.E.Configure();18}19}20}21Microsoft.Coyote.Actors.Tests.E.Configure() methodConfigure
Using AI Code Generation
1public class E {2    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {3        runtime.SendEvent(id, new ConfigureEvent(name, value));4    }5}6public class E {7    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {8        runtime.SendEvent(id, new ConfigureEvent(name, value));9    }10}11public class E {12    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {13        runtime.SendEvent(id, new ConfigureEvent(name, value));14    }15}16public class E {17    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {18        runtime.SendEvent(id, new ConfigureEvent(name, value));19    }20}21public class E {22    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {23        runtime.SendEvent(id, new ConfigureEvent(name, value));24    }25}26public class E {27    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {28        runtime.SendEvent(id, new ConfigureEvent(name, value));29    }30}31public class E {32    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {33        runtime.SendEvent(id, new ConfigureEvent(name, value));34    }35}36public class E {37    public static void Configure(ActorRuntime runtime, ActorId id, string name, int value) {38        runtime.SendEvent(id, new ConfigureEvent(name, value));39    }40}Configure
Using AI Code Generation
1{2    static void Main(string[] args)3    {4        CoyoteRuntime.Configure();5    }6}7{8    static void Main(string[] args)9    {10        CoyoteRuntime.Configure(new ActorRuntimeConfiguration11        {12            LogWriter = new TextWriterLogWriter(Console.Out)13        });14    }15}16{17    static void Main(string[] args)18    {19        CoyoteRuntime.Configure(new ActorRuntimeConfiguration20        {21            Scheduler = new CustomActorScheduler()22        });23    }24}25{26    static void Main(string[] args)27    {28        CoyoteRuntime.Configure(new ActorRuntimeConfiguration29        {30            ActorIdProvider = new CustomActorIdProvider()31        });32    }33}34{35    static void Main(string[] args)36    {37        CoyoteRuntime.Configure(new ActorRuntimeConfiguration38        {39            RandomGenerator = new CustomRandomNumberGenerator()40        });41    }42}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.
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!!
