Beispiel #1
0
        public async SystemTasks.Task TestBasicTimerOperationInStateMachine()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(T1), new SetupEvent(tcs));

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
        public async Task TestOperationGroupingSingleMachineNoSend()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M1), new SetupEvent(tcs));

                var result = await GetResultAsync(tcs.Task);
                Assert.True(result);
            });
        }
        public async SystemTasks.Task TestReceiveEventTwoMachines()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M4), new SetupEvent(tcs));

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
Beispiel #4
0
        public async Task TestIllegalPeriodSpecificationInStateMachine()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(T5), new SetupEvent(tcs));

                var result = await GetResultAsync(tcs.Task);
                Assert.True(result);
            });
        }
        public void TestOperationGroupingTwoActorsCreate()
        {
            this.Test(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M4A), new SetupEvent(tcs));

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
Beispiel #6
0
        public async SystemTasks.Task TestDropTimeoutsAfterTimerDisposalInStateMachine()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(T3), new SetupEvent(tcs));

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
        public async SystemTasks.Task TestGetOperationGroupIdSet()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M2), new SetupEvent(tcs));

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
        public async SystemTasks.Task TestTrySetResult()
        {
            var tcs = TaskCompletionSource.Create <int>();

            tcs.SetResult(3);
            bool check  = tcs.TrySetResult(5);
            int  result = await tcs.Task;

            Assert.False(check);
            Assert.Equal(SystemTasks.TaskStatus.RanToCompletion, tcs.Task.Status);
            Assert.Equal(3, result);
        }
        public async Task TestNoMemoryLeakAfterHalt()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M), new SetupEvent(tcs));

                await WaitAsync(tcs.Task, 15000);

                (r as ActorRuntime).Stop();
            });
        }
Beispiel #10
0
 public void TestWhenAnyWithIncompleteGenericTask()
 {
     this.Test(async() =>
     {
         // Test that `WhenAny` can complete even if one of the tasks cannot complete until later.
         var tcs = TaskCompletionSource.Create <bool>();
         await Task.WhenAny(tcs.Task, Task.FromResult(true));
         tcs.SetResult(true);
         await tcs.Task;
     },
               configuration: GetConfiguration().WithTestingIterations(200));
 }
        public async SystemTasks.Task TestNoMemoryLeakInEventSending()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M), new SetupEvent(tcs));

                await this.WaitAsync(tcs.Task, 20000);

                (r as ActorRuntime).Stop();
            });
        }
Beispiel #12
0
        public async SystemTasks.Task TestCustomPeriodicTimerEventInStateMachine()
        {
            await this.RunAsync(async r =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(T6), new T6.ConfigEvent {
                    Tcs = tcs, Test = T6.TestType.CustomPeriodicTimer
                });

                var result = await this.GetResultAsync(tcs);
                Assert.True(result);
            });
        }
Beispiel #13
0
        public async Task TestNullCustomLogger()
        {
            var           tcs     = TaskCompletionSource.Create <bool>();
            IActorRuntime runtime = CreateTestRuntime(Configuration.Create(), tcs);

            runtime.SetLogger(null);

            runtime.CreateActor(typeof(M));

            await WaitAsync(tcs.Task);

            Assert.Equal("System.IO.TextWriter+NullTextWriter", runtime.Logger.ToString());
        }
Beispiel #14
0
        public void TestGraphLogger()
        {
            Configuration config = Configuration.Create().WithVerbosityEnabled();

            this.Test(async runtime =>
            {
                using (CustomLogger logger = new CustomLogger())
                {
                    runtime.SetLogger(logger);
                    var tcs = TaskCompletionSource.Create <bool>();
                    runtime.RegisterMonitor <TestMonitor>();
                    runtime.Monitor <TestMonitor>(new SetupEvent(tcs));
                    var graphBuilder = new ActorRuntimeLogGraphBuilder(false);
                    runtime.RegisterLog(graphBuilder);
                    runtime.CreateActor(typeof(M));
                    await this.WaitAsync(tcs.Task);
                    await Task.Delay(200);
                    Assert.True(tcs.Task.IsCompleted, "The task await returned but the task is not completed???");

                    string expected = @"<DirectedGraph xmlns='http://schemas.microsoft.com/vs/2009/dgml'>
  <Nodes>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0)' Category='Actor' Group='Expanded'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0).M(0)' Label='M(0)'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1)' Category='StateMachine' Group='Expanded'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Act' Label='Act'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Init' Label='Init'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor' Group='Expanded'/>
    <Node Id='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor.Init' Label='Init'/>
  </Nodes>
  <Links>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0).M(0)' Category='Contains'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1)' Label='CreateActor' Index='0' EventId='CreateActor'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0).M(0)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Init' Label='E' Index='0' EventId='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+E' HandledBy='Init'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0).M(0)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor.Init' Label='CompletedEvent' Index='0' EventId='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+CompletedEvent'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Act' Category='Contains'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1)' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Init' Category='Contains'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Act' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+M(0).M(0)' Label='E' Index='0' EventId='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+E'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Init' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+N(1).Act' Label='E' Index='0' EventId='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+E' HandledBy='Init'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor.Init' Category='Contains'/>
    <Link Source='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor.Init' Target='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+TestMonitor.Init' Label='CompletedEvent' Index='0' EventId='Microsoft.Coyote.Production.Tests.Actors.CustomActorRuntimeLogTests+CompletedEvent'/>
  </Links>
</DirectedGraph>
";

                    string dgml   = graphBuilder.Graph.ToString();
                    string actual = dgml.RemoveNonDeterministicValues();
                    expected      = expected.RemoveNonDeterministicValues();
                    Assert.Equal(expected, actual);
                }
            }, config);
        }
        public async SystemTasks.Task TestAsynchronousSetResult()
        {
            var tcs  = TaskCompletionSource.Create <int>();
            var task = Task.Run(async() =>
            {
                return(await tcs.Task);
            });

            tcs.SetResult(3);
            int result = await task;

            Assert.Equal(SystemTasks.TaskStatus.RanToCompletion, tcs.Task.Status);
            Assert.Equal(3, result);
        }
Beispiel #16
0
        public async Task TestGraphLogger()
        {
            CustomLogger  logger = new CustomLogger();
            Configuration config = Configuration.Create().WithVerbosityEnabled();

            var           graphBuilder = new ActorRuntimeLogGraphBuilder(false);
            var           tcs          = TaskCompletionSource.Create <bool>();
            IActorRuntime runtime      = CreateTestRuntime(config, tcs, logger);

            runtime.RegisterLog(graphBuilder);

            runtime.CreateActor(typeof(M));

            await WaitAsync(tcs.Task);

            await Task.Delay(200);

            string expected = @"<DirectedGraph xmlns='http://schemas.microsoft.com/vs/2009/dgml'>
  <Nodes>
    <Node Id='M()' Category='Actor' Group='Expanded'/>
    <Node Id='M().M()' Label='M()'/>
    <Node Id='N()' Category='StateMachine' Group='Expanded'/>
    <Node Id='N().Act' Label='Act'/>
    <Node Id='N().Init' Label='Init'/>
    <Node Id='TestMonitor' Group='Expanded'/>
    <Node Id='TestMonitor.Init' Label='Init'/>
    <Node Id='TestMonitor.OnCompleted' Label='OnCompleted'/>
  </Nodes>
  <Links>
    <Link Source='M().M()' Target='N().Init' Label='E' Index='' EventId='E' HandledBy='Init'/>
    <Link Source='M().M()' Target='TestMonitor.Init' Label='CompletedEvent' Index='' EventId='CompletedEvent'/>
    <Link Source='M()' Target='M().M()' Category='Contains'/>
    <Link Source='N().Act' Target='M().M()' Label='E' Index='' EventId='E'/>
    <Link Source='N().Init' Target='N().Act' Label='E' Index='' EventId='E' HandledBy='Init'/>
    <Link Source='N()' Target='N().Act' Category='Contains'/>
    <Link Source='N()' Target='N().Init' Category='Contains'/>
    <Link Source='TestMonitor.Init' Target='TestMonitor.OnCompleted' Label='CompletedEvent' Index='' EventId='CompletedEvent'/>
    <Link Source='TestMonitor' Target='TestMonitor.Init' Category='Contains'/>
    <Link Source='TestMonitor' Target='TestMonitor.OnCompleted' Category='Contains'/>
  </Links>
</DirectedGraph>
";

            string actual = RemoveNonDeterministicValuesFromReport(graphBuilder.Graph.ToString());

            Assert.Equal(expected, actual);

            logger.Dispose();
        }
        public void TestOperationGroupingTwoActorsSendBackStarter()
        {
            this.Test(async r =>
            {
                var tcs1 = TaskCompletionSource.Create <bool>();
                var tcs2 = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M8A), new SetupMultipleEvent(tcs1, tcs2));

                var result = await this.GetResultAsync(tcs1);
                Assert.True(result);

                result = await this.GetResultAsync(tcs2);
                Assert.True(result);
            });
        }
Beispiel #18
0
        public void TestCustomLoggerNoVerbosity()
        {
            Configuration config = Configuration.Create();

            this.Test(async runtime =>
            {
                runtime.SetLogger(TextWriter.Null);
                var tcs = TaskCompletionSource.Create <bool>();
                runtime.RegisterMonitor <TestMonitor>();
                runtime.Monitor <TestMonitor>(new SetupEvent(tcs));
                runtime.CreateActor(typeof(M));
                await this.WaitAsync(tcs.Task);
                Assert.Equal("System.IO.TextWriter+NullTextWriter", runtime.Logger.ToString());
            }, config);
        }
        public async Task TestOperationGroupingTwoMachinesSendBackStarter()
        {
            await this.RunAsync(async r =>
            {
                var tcs1 = TaskCompletionSource.Create <bool>();
                var tcs2 = TaskCompletionSource.Create <bool>();
                r.CreateActor(typeof(M8A), new SetupMultipleEvent(tcs1, tcs2));

                var result = await GetResultAsync(tcs1.Task);
                Assert.True(result);

                result = await GetResultAsync(tcs2.Task);
                Assert.True(result);
            });
        }
        public void TestNullCustomLogger()
        {
            Configuration config = Configuration.Create();

            this.Test(async runtime =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                runtime.RegisterMonitor <TestMonitor>();
                runtime.Monitor <TestMonitor>(new SetupEvent(tcs));
                runtime.Logger = null;
                runtime.CreateActor(typeof(M));
                await this.WaitAsync(tcs.Task);
                Assert.Equal("Microsoft.Coyote.IO.NullLogger", runtime.Logger.ToString());
            }, config);
        }
 public void TestIsCompleted()
 {
     this.TestWithError(async() =>
     {
         var tcs  = TaskCompletionSource.Create <int>();
         var task = tcs.Task;
         tcs.SetResult(3);
         Specification.Assert(tcs.Task.IsCompleted, "Task is not completed.");
         await task;
         Specification.Assert(task.IsCompleted, "Task is not completed.");
         Specification.Assert(false, "Reached test assertion.");
     },
                        expectedError: "Reached test assertion.",
                        replay: true);
 }
 public void TestSetResult()
 {
     this.TestWithError(async() =>
     {
         var tcs = TaskCompletionSource.Create <int>();
         tcs.SetResult(3);
         int result = await tcs.Task;
         Specification.Assert(tcs.Task.Status is SystemTasks.TaskStatus.RanToCompletion,
                              "Found unexpected status {0}.", tcs.Task.Status);
         Specification.Assert(result is 3, "Found unexpected value {0}.", result);
         Specification.Assert(false, "Reached test assertion.");
     },
                        expectedError: "Reached test assertion.",
                        replay: true);
 }
        public void TestNullCustomLogger()
        {
            Configuration config = Configuration.Create();

            this.Test(new Func <IActorRuntime, Task>(async(runtime) =>
            {
                var tcs = TaskCompletionSource.Create <bool>();
                runtime.RegisterMonitor <TestMonitor>();
                runtime.Monitor <TestMonitor>(new SetupEvent(tcs));
                runtime.SetLogger(null);
                runtime.CreateActor(typeof(M));
                await this.WaitAsync(tcs.Task);
                Assert.Equal("System.IO.TextWriter+NullTextWriter", runtime.Logger.ToString());
            }), config);
        }
 public void TestWaitAllWithResultsAndDeadlock()
 {
     this.TestWithError(async() =>
     {
         // Test that `WaitAll` deadlocks because one of the tasks cannot complete until later.
         var tcs = TaskCompletionSource.Create <bool>();
         Task.WaitAll(tcs.Task, Task.FromResult(true));
         tcs.SetResult(true);
         await tcs.Task;
     },
                        errorChecker: (e) =>
     {
         Assert.StartsWith("Deadlock detected.", e);
     },
                        replay: true);
 }
        public async Task TestReceiveEvent()
        {
            for (int i = 0; i < 100; i++)
            {
                await this.RunAsync(async r =>
                {
                    r.Logger.WriteLine($"Iteration #{i}");

                    var tcs = TaskCompletionSource.Create <bool>();
                    r.CreateActor(typeof(M1), new SetupTcsEvent(tcs, 18000));

                    var result = await GetResultAsync(tcs.Task);
                    Assert.True(result);
                });
            }
        }
Beispiel #26
0
        protected async Task RunAsync(Func <IActorRuntime, Task> test, Configuration configuration = null, bool handleFailures = true)
        {
            configuration = configuration ?? GetConfiguration();

            TextWriter logger;

            if (configuration.IsVerbose)
            {
                logger = new TestOutputLogger(this.TestOutput, true);
            }
            else
            {
                logger = TextWriter.Null;
            }

            try
            {
                configuration.IsMonitoringEnabledInInProduction = true;
                var runtime = RuntimeFactory.Create(configuration);
                runtime.SetLogger(logger);

                var errorTask = TaskCompletionSource.Create <Exception>();
                if (handleFailures)
                {
                    runtime.OnFailure += (e) =>
                    {
                        errorTask.SetResult(Unwrap(e));
                    };
                }

                await Task.WhenAny(test(runtime), errorTask.Task);

                if (handleFailures && errorTask.Task.IsCompleted)
                {
                    Assert.False(true, errorTask.Task.Result.Message);
                }
            }
            catch (Exception ex)
            {
                Exception e = Unwrap(ex);
                Assert.False(true, e.Message + "\n" + e.StackTrace);
            }
            finally
            {
                logger.Dispose();
            }
        }
        public void TestCustomLogger()
        {
            Configuration config = Configuration.Create().WithVerbosityEnabled();

            this.Test(new Func <IActorRuntime, Task>(async(runtime) =>
            {
                using (CustomLogger logger = new CustomLogger())
                {
                    runtime.SetLogger(logger);
                    var tcs = TaskCompletionSource.Create <bool>();
                    runtime.RegisterMonitor <TestMonitor>();
                    runtime.Monitor <TestMonitor>(new SetupEvent(tcs));
                    runtime.CreateActor(typeof(M));
                    await this.WaitAsync(tcs.Task);
                    await Task.Delay(200);
                    Assert.True(tcs.Task.IsCompleted, "The task await returned but the task is not completed???");

                    string expected = @"<CreateLog> TestMonitor was created.
<MonitorLog> TestMonitor enters state 'Init'.
<MonitorLog> TestMonitor is processing event 'SetupEvent' in state 'Init'.
<MonitorLog> TestMonitor executed action 'OnSetup' in state 'Init'.
<CreateLog> M() was created by task ''.
<CreateLog> N() was created by M().
<SendLog> M() in state '' sent event 'E' to N().
<EnqueueLog> N() enqueued event 'E'.
<StateLog> N() enters state 'Init'.
<ActionLog> N() invoked action 'OnInitEntry' in state 'Init'.
<DequeueLog> N() dequeued event 'E' in state 'Init'.
<GotoLog> N() is transitioning from state 'Init' to state 'N.Act'.
<StateLog> N() exits state 'Init'.
<StateLog> N() enters state 'Act'.
<ActionLog> N() invoked action 'ActOnEntry' in state 'Act'.
<SendLog> N() in state 'Act' sent event 'E' to M().
<EnqueueLog> M() enqueued event 'E'.
<DequeueLog> M() dequeued event 'E'.
<ActionLog> M() invoked action 'Act'.
<MonitorLog> TestMonitor is processing event 'CompletedEvent' in state 'Init'.
<MonitorLog> TestMonitor executed action 'OnCompleted' in state 'Init'.";

                    string actual = logger.ToString().RemoveNonDeterministicValues();
                    expected      = expected.NormalizeNewLines();
                    actual        = actual.SortLines(); // threading makes this non-deterministic otherwise.
                    expected      = expected.SortLines();
                    Assert.Equal(expected, actual);
                }
            }), config);
        }
Beispiel #28
0
        public async SystemTasks.Task TestTimerLifetime()
        {
            await this.RunAsync(async r =>
            {
                int numTimers = 1000;
                var awaiters  = new Task[numTimers];
                for (int i = 0; i < numTimers; i++)
                {
                    var tcs = TaskCompletionSource.Create <bool>();
                    r.CreateActor(typeof(T1), new SetupEvent(tcs));
                    awaiters[i] = tcs.Task;
                }

                Task task = Task.WhenAll(awaiters);
                await this.WaitAsync(task);
            });
        }
Beispiel #29
0
        public async SystemTasks.Task TestHandledExceptionOnSendExec()
        {
            await this.RunAsync(async r =>
            {
                var failed   = false;
                var tcs      = TaskCompletionSource.Create <bool>();
                r.OnFailure += (ex) =>
                {
                    failed = true;
                    tcs.SetResult(false);
                };

                r.CreateActor(typeof(M4), new Config2(true, tcs));

                await this.WaitAsync(tcs.Task);
                Assert.False(failed);
            });
        }
Beispiel #30
0
        public async SystemTasks.Task TestSendCycleDoesNotDeadlock()
        {
            await this.RunAsync(async r =>
            {
                var failed   = false;
                var tcs      = TaskCompletionSource.Create <bool>();
                r.OnFailure += (ex) =>
                {
                    failed = true;
                    tcs.SetResult(false);
                };

                r.CreateActor(typeof(M2), new Config1(tcs));

                await this.WaitAsync(tcs.Task);
                Assert.False(failed);
            });
        }