예제 #1
0
        static string StartClients(string sctx, SynchronizedLogger logger, out DomainCommunicationProvider clientsNetworkProvider)
        {
            var configuration = Configuration.Create().WithMonitorsInProductionEnabled().WithVerbosityEnabled(2);
            var runtime       = PSharpRuntime.Create(configuration);
            var runtimeHost   = HostInfo.NewRuntimeHost(runtime);

            clientsNetworkProvider = new DomainCommunicationProvider(runtimeHost, "clients");
            runtimeHost.SetNetworkProvider(clientsNetworkProvider);
            runtimeHost.SetLogger(logger);

            var messages = new MessageCollection();

            messages.CollectionChanged += (sender, e) => logger.WriteLine(e.NewItems[0].ToString());
            var ctx = sctx.FromJson <DistributedStorageContext>();

            var synchronizable = Synchronizable.Empty();

            foreach (var storageNode in ctx.StorageNodes)
            {
                var waitIfHandledReplReq = logger.MachineActionHandledWait((machineId, _, actionName) => Equals(machineId, storageNode.Id) && actionName == "HandleReplReq");
                synchronizable = synchronizable.Then(waitIfHandledReplReq.Delay(10));
            }
            logger.ApplySynchronization(synchronizable);

            NewClients(runtimeHost, ctx, messages);
            logger.WaitForWriting(6000);
            return(ctx.ToJson());
        }
예제 #2
0
        public void Should_ignore_empty_when_restraining_apps()
        {
            // Arrange
            var starts        = new ConcurrentBag <int>();
            var starts_Add    = new MarshalByRefAction <int>(i => starts.Add(i));
            var processes     = new ConcurrentBag <int>();
            var processes_Add = new MarshalByRefAction <int>(i => processes.Add(i));

            void Synchronize(Action <ISynchronizer> action)
            {
                var curMeth = MethodBase.GetCurrentMethod();
                var empty   = Synchronizable.Empty();
                var setter1 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Foo"), obj => (int)obj == 1);
                var setter2 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Bar"), obj => (int)obj == 2);
                var waiter3 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Baz"), obj => (int)obj == 3);
                var waiter4 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Qux"), obj => (int)obj == 4);

                using (var sync = setter1.Or(setter2).Or(empty).And(empty.Then(waiter3).Then(empty).Then(waiter4).Then(empty)).And(empty).GetSynchronizer())
                    action(sync);
            }

            // Act
            Synchronize(sync =>
            {
                var mre1  = new ST::ManualResetEvent(false);
                var task1 = Task.Run(() =>
                                     AppDomain.CurrentDomain.RunAtIsolatedDomain((starts_Add_, processes_Add_, mre1_) =>
                                                                                 Synchronize(sync_ =>
                {
                    mre1_.WaitOne(10000);
                    starts_Add_.Invoke(1);
                    sync_.Begin(1).Wait();
                    processes_Add_.Invoke(1);
                    sync_.End(1).Wait();
                }), starts_Add, processes_Add, mre1));

                var task2 = Task.Run(() =>
                                     AppDomain.CurrentDomain.RunAtIsolatedDomain((starts_Add_, processes_Add_) =>
                                                                                 Synchronize(sync_ =>
                {
                    starts_Add_.Invoke(2);
                    sync_.Begin(2).Wait();
                    processes_Add_.Invoke(2);
                    sync_.End(2).Wait();
                }), starts_Add, processes_Add));

                var task3 = Task.Run(() =>
                                     AppDomain.CurrentDomain.RunAtIsolatedDomain((starts_Add_, processes_Add_) =>
                                                                                 Synchronize(sync_ =>
                {
                    starts_Add_.Invoke(3);
                    sync_.Begin(3).Wait();
                    processes_Add_.Invoke(3);
                    sync_.End(3).Wait();
                }), starts_Add, processes_Add));

                var task4 = Task.Run(() =>
                                     AppDomain.CurrentDomain.RunAtIsolatedDomain((starts_Add_, processes_Add_) =>
                                                                                 Synchronize(sync_ =>
                {
                    starts_Add_.Invoke(4);
                    sync_.Begin(4).Wait();
                    processes_Add_.Invoke(4);
                    sync_.End(4).Wait();
                }), starts_Add, processes_Add));

                sync.NotifyAll(false).Wait();


                // Assert
                CollectionAssert.DoesNotContain(starts, 1);
                CollectionAssert.AreEqual(new[] { 3, 4 }, processes.Intersect(new[] { 3, 4 }));
                mre1.Set();
                Task.WaitAll(task1, task2, task3, task4);
            });
        }
예제 #3
0
        public void Should_ignore_empty_when_restraining_tasks()
        {
            // Arrange
            var starts    = new ConcurrentBag <int>();
            var processes = new ConcurrentBag <int>();
            var empty     = Synchronizable.Empty();
            var setter1   = Synchronizable.EventSet(obj => (int)obj == 1);
            var setter2   = Synchronizable.EventSet(obj => (int)obj == 2);
            var waiter3   = Synchronizable.EventWait(obj => (int)obj == 3);
            var waiter4   = Synchronizable.EventWait(obj => (int)obj == 4);


            var sync = default(ISynchronizer);

            try
            {
                // Act
                sync = setter1.Or(setter2).Or(empty).And(empty.Then(waiter3).Then(empty).Then(waiter4).Then(empty)).And(empty).GetSynchronizer();

                var mre1  = new ST::ManualResetEventSlim(false);
                var task1 = Task.Run(() =>
                {
                    mre1.Wait(10000);
                    starts.Add(1);
                    sync.Begin(1).Wait();
                    processes.Add(1);
                    sync.End(1).Wait();
                });

                var task2 = Task.Run(() =>
                {
                    starts.Add(2);
                    sync.Begin(2).Wait();
                    processes.Add(2);
                    sync.End(2).Wait();
                });

                var task3 = Task.Run(() =>
                {
                    starts.Add(3);
                    sync.Begin(3).Wait();
                    processes.Add(3);
                    sync.End(3).Wait();
                });

                var task4 = Task.Run(() =>
                {
                    starts.Add(4);
                    sync.Begin(4).Wait();
                    processes.Add(4);
                    sync.End(4).Wait();
                });

                sync.NotifyAll(false).Wait();


                // Assert
                CollectionAssert.DoesNotContain(starts, 1);
                CollectionAssert.AreEqual(new[] { 3, 4 }, processes.Intersect(new[] { 3, 4 }));
                mre1.Set();
                Task.WaitAll(task1, task2, task3, task4);
            }
            finally
            {
                sync?.Dispose();
            }
        }