Exemplo n.º 1
0
        public void Machine_can_be_created_faster_than_the_first_time()
        {
            AppDomain.CurrentDomain.RunAtIsolatedDomain(() =>
            {
                // Arrange
                var configuration = Configuration.Create();
                var runtime       = PSharpRuntime.Create(configuration);
                var runtimeHost   = HostInfo.NewRuntimeHost(runtime);


                // Act
                var sw1 = Stopwatch.StartNew();
                runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());
                var elapsed1 = sw1.ElapsedTicks;

                var sw10 = Stopwatch.StartNew();
                for (var i = 0; i < 10; i++)
                {
                    runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());
                }
                var elapsed10 = sw10.ElapsedTicks;


                // Assert
                Assert.Less(elapsed10, elapsed1 / 10);
            });
        }
Exemplo n.º 2
0
        public void Context_should_return_same_Machine_if_it_is_in_local_and_single_application()
        {
            // Arrange
            var ctx      = default(DistributedStorageContext);
            var expected = default(int);
            var actual   = default(int);

            // Act
            {
                var configuration = Configuration.Create();
                var runtime       = PSharpRuntime.Create(configuration);
                var runtimeHost   = HostInfo.NewRuntimeHost(runtime);

                ctx        = runtimeHost.New <DistributedStorageContext>();
                ctx.Server = runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());
                expected   = RuntimeHelpers.GetHashCode(ctx.Server);
            }

            {
                actual = RuntimeHelpers.GetHashCode(ctx.Server);
            }


            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 3
0
        public void ReportFully_should_return_readable_trace_contents()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(200).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var testArtifact = configuration.CreateTestArtifact(MethodBase.GetCurrentMethod(), new DateTime(2017, 10, 10));
            var engine       = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.BugReceiver>());
                m1.Configure(new Notify());
            });

            engine.RunAndEmitTraces(testArtifact);


            // Act
            var result = engine.ReportFully();


            // Assert
            Assert.That(result, Does.Match($@"<StrategyLog> { testArtifact.TraceNameBase }_\d+_\d+\.txt"));
            Assert.That(result, Does.Match("<ErrorLog> Bug Found!!!!"));
        }
Exemplo n.º 4
0
        public void Run_should_report_all_results_excluding_the_test_that_exception_occurs_in_the_callback()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(10).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var engine = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.InfiniteLoopReceiver>());
                m1.Configure(new Notify());
            });

            engine.RegisterPerIterationCallBacks(new Action <int>[]
            {
                iteration => { },
                iteration => { },
                iteration => { throw new NotSupportedException(); },
                iteration => { },
                iteration => { },
            });


            // Act, Assert
            var ex = Assert.Throws <AggregateException>(() => engine.Run());

            Assert.IsInstanceOf <TargetInvocationException>(ex.GetBaseException());
            Assert.IsInstanceOf <NotSupportedException>(ex.GetBaseException().InnerException);
            Assert.IsNotNull(engine.TestReports[0]);
            Assert.IsNotNull(engine.TestReports[1]);
            Assert.IsNull(engine.TestReports[2]);
            Assert.IsNotNull(engine.TestReports[3]);
            Assert.IsNotNull(engine.TestReports[4]);
        }
Exemplo n.º 5
0
        public void TryEmitTraces_should_output_the_artifact_of_the_test_to_the_specified_directory()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(200).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var testArtifact = configuration.CreateTestArtifact(MethodBase.GetCurrentMethod(), new DateTime(2017, 10, 10));
            var engine       = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.BugReceiver>());
                m1.Configure(new Notify());
            });

            engine.Run();


            // Act
            engine.TryEmitTraces(testArtifact.Directory, testArtifact.TraceNameBase);


            // Assert
            Assert.GreaterOrEqual(engine.TestReport.NumOfFoundBugs, 1);
            var traceFileInfos = new DirectoryInfo(testArtifact.Directory).EnumerateFiles();

            Assert.IsTrue(traceFileInfos.Any(_ => Regex.IsMatch(_.Name, $@"{ testArtifact.TraceNameBase }_\d+_\d+\.pstrace")));
            Assert.IsTrue(traceFileInfos.Any(_ => Regex.IsMatch(_.Name, $@"{ testArtifact.TraceNameBase }_\d+_\d+\.schedule")));
            Assert.IsTrue(traceFileInfos.Any(_ => Regex.IsMatch(_.Name, $@"{ testArtifact.TraceNameBase }_\d+_\d+\.txt")));
        }
Exemplo n.º 6
0
        public void Context_should_return_same_Machine_if_it_is_in_remote_and_single_application()
        {
            // Arrange
            var expected  = default(int);
            var actual    = default(int);
            var parameter = default(string);

            // Act
            {
                var configuration = Configuration.Create();
                var runtime       = PSharpRuntime.Create(configuration);
                var runtimeHost   = HostInfo.NewRuntimeHost(runtime);

                var ctx = runtimeHost.New <DistributedStorageContext>();
                ctx.Server = runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());
                expected   = RuntimeHelpers.GetHashCode(ctx.Server);
                parameter  = ctx.ToJson();
            }

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            {
                var ctx = parameter.FromJson <DistributedStorageContext>();
                actual = RuntimeHelpers.GetHashCode(ctx.Server);
            }


            // Assert
            Assert.AreEqual(expected, actual);
        }
Exemplo n.º 7
0
        static void NewStorageNodes(RuntimeHost runtimeHost, DistributedStorageContext ctx, MessageCollection messages)
        {
            var configure    = new ConfigureStorageNode(messages, ctx.SafetyMonitor);
            var storageNodes = new List <IStorageNodeSender>();

            for (var i = 0; i < 3; i++)
            {
                var storageNode = runtimeHost.New(MachineInterface.Sender <IStorageNodeSender>().Bundler <IStorageNodeBundler>().Receiver <StorageNodeReceiver>());
                storageNode.Configure(configure);
                storageNode.Handshake(new HandshakeStorageNode(ctx.Server));
                storageNodes.Add(storageNode);
            }
            ctx.SafetyMonitor.Handshake(new HandshakeSafetyMonitor(storageNodes.ToArray()));
            ctx.StorageNodes = storageNodes.ToArray();
        }
Exemplo n.º 8
0
        public void Context_should_not_return_same_Machine_if_it_is_in_remote_and_multi_application()
        {
            // Arrange
            var expected        = default(int);
            var actual          = default(int);
            var expected_Assign = new MarshalByRefAction <int>(i => expected = i);
            var actual_Assign   = new MarshalByRefAction <int>(i => actual = i);

            // Act
            AppDomain.CurrentDomain.RunAtIsolatedDomain((expected_Assign_, actual_Assign_) =>
            {
                var configuration         = Configuration.Create().WithVerbosityEnabled(2);
                var runtime               = PSharpRuntime.Create(configuration);
                var runtimeHost           = HostInfo.NewRuntimeHost(runtime);
                var logger                = new SynchronizedLogger(new InMemoryLogger());
                var setIfHandledHandshake = logger.MachineActionHandledSet((_1, _2, actionName) => actionName == "HandleHandshake");
                logger.ApplySynchronization(setIfHandledHandshake);
                runtimeHost.SetLogger(logger);
                using (var networkProvider = new DomainCommunicationProvider(runtimeHost, "servers"))
                {
                    var parameter = default(string);
                    {
                        runtimeHost.SetNetworkProvider(networkProvider);

                        var ctx    = runtimeHost.New <DistributedStorageContext>();
                        ctx.Server = runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());
                        ctx.Server.Configure(new ConfigureServer(new MessageCollection(), null, null));
                        expected_Assign_.Invoke(RuntimeHelpers.GetHashCode(ctx.Server));
                        parameter = ctx.ToJson();
                    }

                    AppDomain.CurrentDomain.RunAtIsolatedDomain((actual_Assign__, parameter_) =>
                    {
                        var ctx = parameter_.FromJson <DistributedStorageContext>();
                        actual_Assign__.Invoke(RuntimeHelpers.GetHashCode(ctx.Server));
                        ctx.Server.Handshake(new HandshakeServer(null, new IStorageNodeSender[0]));
                    }, actual_Assign_, parameter);

                    logger.WaitForWriting(1000);
                }
            }, expected_Assign, actual_Assign);


            // Assert
            Assert.AreNotEqual(expected, actual);
        }
Exemplo n.º 9
0
        public void Machine_should_transit_its_state_according_to_passed_event()
        {
            // Arrange
            var configuration    = Configuration.Create().WithMonitorsInProductionEnabled().WithVerbosityEnabled(2);
            var runtime          = PSharpRuntime.Create(configuration);
            var runtimeHost      = HostInfo.NewRuntimeHost(runtime);
            var logger           = new SynchronizedLogger(new InMemoryLogger());
            var setIfHandledSync = logger.MachineActionHandledSet((_1, _2, actionName) => actionName == "HandleSync");

            logger.ApplySynchronization(setIfHandledSync);
            runtimeHost.SetLogger(logger);
            var server = runtimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiverWithoutBug>());


            // Act
            var messages            = new MessageCollection();
            var safetyMonitorMock   = new Mock <ISafetyMonitorSender>();
            var livenessMonitorMock = new Mock <ILivenessMonitorSender>();

            server.Configure(new ConfigureServer(messages, safetyMonitorMock.Object, livenessMonitorMock.Object));

            var clientMock = new Mock <IClientSender>();
            var clientId   = runtime.CreateMachine(typeof(Client));

            clientMock.SetupGet(_ => _.Id).Returns(clientId);
            var storageNodeMock = new Mock <IStorageNodeSender>();
            var storageNodeId   = runtime.CreateMachine(typeof(StorageNode));

            storageNodeMock.Setup(_ => _.Id).Returns(storageNodeId);
            server.Handshake(new HandshakeServer(clientMock.Object, new[] { storageNodeMock.Object }));

            var clientReq = new ClientReq(42);

            server.ClientReq(clientReq);

            server.Sync(new Sync(storageNodeMock.Object, 0));


            // Assert
            Assert.That(logger.WaitForWriting(3000), Does.Contain("handled action 'HandleSync'"));
            livenessMonitorMock.Verify(_ => _.ClientReq(clientReq));
            storageNodeMock.Verify(_ => _.ReplReq(It.Is <ReplReq>(x => x.Data == clientReq.DataToReplicate)));
        }
Exemplo n.º 10
0
        public void RegisterPerIterationCallBack_should_throw_IndexOutOfRangeException_if_registering_the_number_of_callbacks_greater_than_the_number_of_parallel_tasks()
        {
            // Arrange
            var configuration = Configuration.Create().WithParallelBugFindingTasks(5);
            var engine        = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.InfiniteLoopReceiver>());
                m1.Configure(new Notify());
            });

            engine.RegisterPerIterationCallBack(iteration => { });
            engine.RegisterPerIterationCallBack(iteration => { });
            engine.RegisterPerIterationCallBack(iteration => { });
            engine.RegisterPerIterationCallBack(iteration => { });
            engine.RegisterPerIterationCallBack(iteration => { });


            // Act, Assert
            var ex = Assert.Throws <IndexOutOfRangeException>(() => engine.RegisterPerIterationCallBack(iteration => { }));

            Assert.That(ex.Message, Does.Match(@"'RegisterPerIterationCallBackIndex'\(\d+\) was outside the bounds of the 'ParallelBugFindingTasks'\(\d+\)\."));
        }
Exemplo n.º 11
0
        public void RegisterPerIterationCallBacks_should_throw_ArgumentOutOfRangeException_if_specifying_the_number_of_callbacks_different_from_the_number_of_parallel_tasks()
        {
            // Arrange
            var configuration = Configuration.Create().WithParallelBugFindingTasks(5);
            var engine        = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.InfiniteLoopReceiver>());
                m1.Configure(new Notify());
            });


            // Act, Assert
            var ex = Assert.Throws <ArgumentOutOfRangeException>(() => engine.RegisterPerIterationCallBacks(new Action <int>[]
            {
                iteration => { },
                iteration => { },
                iteration => { },
                iteration => { },
            }));;

            Assert.That(ex.Message, Does.Match(@"Specified argument length was out of the range of valid length \d+\."));
        }
Exemplo n.º 12
0
        public void Stop_should_abort_test_in_the_middle_of_the_iteration()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(200).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var engine = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.InfiniteLoopReceiver>());
                m1.Configure(new Notify());
            });
            var runner = Task.Run(() => engine.Run());


            // Act
            engine.Stop();


            // Assert
            Assert.IsTrue(runner.Wait(5000));
        }
Exemplo n.º 13
0
        public void Run_should_report_all_results_of_the_tests_that_are_run_concurrently()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(200).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var engine = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.BugReceiver>());
                m1.Configure(new Notify());
            });


            // Act
            engine.RunAndEmitTraces();


            // Assert
            Assert.GreaterOrEqual(engine.TestReport.NumOfFoundBugs, 1);
            Assert.That(engine.Report(), Does.StartWith("..."));
            Assert.AreEqual(5, engine.TestReports.Count(_ => _ != null));
        }
        public void Initializer_and_OnEnterInvoke_can_specify_to_the_same_place()
        {
            // Arrange
            var configuration = Configuration.Create().WithVerbosityEnabled(2);
            var runtime       = PSharpRuntime.Create(configuration);
            var runtimeHost   = HostInfo.NewRuntimeHost(runtime);

            var logger = new SynchronizedLogger(new InMemoryLogger());
            var waitIfEnteredInitialized1 = logger.MachineActionHandledWait((machineId, _, actionName) => machineId.Name.Contains("M1") && actionName == "EnterInitialized");
            var waitIfEnteredInitialized2 = logger.MachineActionHandledWait((machineId, _, actionName) => machineId.Name.Contains("M2") && actionName == "EnterInitialized");

            logger.ApplySynchronization(waitIfEnteredInitialized2.Then(waitIfEnteredInitialized1));
            runtimeHost.SetLogger(logger);


            // Act
            runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.Receiver>());
            runtimeHost.New(MachineInterface.Sender <M2.ISender>().Bundler <M2.IBundler>().Receiver <M2.Receiver>());


            // Assert
            Assert.DoesNotThrow(() => logger.WaitForWriting(1000));
        }
Exemplo n.º 15
0
        public void TryEmitTraces_should_output_replayable_results()
        {
            // Arrange
            var bugFindingConfig = Configuration.Create().WithNumberOfIterations(200).
                                   WithMaxSchedulingSteps(200).
                                   WithParallelBugFindingTasks(5).
                                   WithRandomSchedulingSeed();
            var testArtifact = bugFindingConfig.CreateTestArtifact(MethodBase.GetCurrentMethod(), new DateTime(2017, 10, 10));
            var action       = default(Action <RuntimeHost>);

            action = runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.BugReceiver>());
                m1.Configure(new Notify());
            };
            var bugFindingEngine = new CompositeBugFindingEngine(bugFindingConfig, action);

            bugFindingEngine.Run();


            // Act
            bugFindingEngine.TryEmitTraces(testArtifact.Directory, testArtifact.TraceNameBase);


            // Assert
            var reproTracePath = bugFindingEngine.EmittedTraceInfos.First().EmittedReproducableTracePath;
            var replayConfig   = Configuration.Create().WithVerbosityEnabled(2);//.

            //WithAttachingDebuggerEnabled();
            replayConfig.ScheduleFile = reproTracePath;
            var replayEngine = new CompositeReplayEngine(replayConfig, action);

            replayEngine.Run();

            Assert.GreaterOrEqual(replayEngine.TestReport.NumOfFoundBugs, 1);
        }
Exemplo n.º 16
0
        public void ReportFully_should_return_empty_if_not_emitting_trace()
        {
            // Arrange
            var configuration = Configuration.Create().WithNumberOfIterations(200).
                                WithMaxSchedulingSteps(200).
                                WithParallelBugFindingTasks(5).
                                WithRandomSchedulingSeed();
            var testArtifact = configuration.CreateTestArtifact(MethodBase.GetCurrentMethod(), new DateTime(2017, 10, 10));
            var engine       = new CompositeBugFindingEngine(configuration, runtimeHost =>
            {
                var m1 = runtimeHost.New(MachineInterface.Sender <M1.ISender>().Bundler <M1.IBundler>().Receiver <M1.BugReceiver>());
                m1.Configure(new Notify());
            });

            engine.Run();


            // Act
            var result = engine.ReportFully();


            // Assert
            Assert.IsEmpty(result);
        }
Exemplo n.º 17
0
 public void NewServer(DistributedStorageContext ctx, MessageCollection messages)
 {
     ctx.Server = RuntimeHost.New(MachineInterface.Sender <IServerSender>().Bundler <IServerBundler>().Receiver <ServerReceiver>());
     ctx.Server.Configure(new ConfigureServer(messages, ctx.SafetyMonitor, ctx.LivenessMonitor));
 }
Exemplo n.º 18
0
 public void NewClient(DistributedStorageContext ctx, MessageCollection messages)
 {
     ctx.Client = RuntimeHost.New(MachineInterface.Sender <IClientSender>().Bundler <IClientBundler>().Receiver <ClientReceiver>());
     ctx.Client.Configure(new ConfigureClient(messages, ctx.Server));
     ctx.Server.Handshake(new HandshakeServer(ctx.Client, ctx.StorageNodes));
 }