public void Can_restrain_apps_to_wait_until_beginning_start_of_all_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 setter1 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Foo"), obj => (int)obj == 1); var setter2 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Bar"), obj => (int)obj == 2); using (var sync = setter1.And(setter2).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_ => { starts_Add_.Invoke(1); sync_.Begin(1).Wait(); mre1_.WaitOne(10000); processes_Add_.Invoke(1); sync_.End(1).Wait(); }), starts_Add, processes_Add, mre1)); var mre2 = new ST::ManualResetEvent(false); var task2 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain((starts_Add_, processes_Add_, mre2_) => Synchronize(sync_ => { starts_Add_.Invoke(2); sync_.Begin(2).Wait(); mre2_.WaitOne(10000); processes_Add_.Invoke(2); sync_.End(2).Wait(); }), starts_Add, processes_Add, mre2)); sync.NotifyAll(false).Wait(); // Assert CollectionAssert.AreEquivalent(new[] { 1, 2 }, starts); CollectionAssert.IsEmpty(processes); mre1.Set(); mre2.Set(); Task.WaitAll(task1, task2); }); }
public void Can_delay_apps_by_the_passed_time_span() { // Arrange var task1BeginTime = default(DateTimeOffset); var task1BeginTime_Assign = new MarshalByRefAction <DateTimeOffset>(dt => task1BeginTime = dt); var task2BegunTime = default(DateTimeOffset); var task2BegunTime_Assign = new MarshalByRefAction <DateTimeOffset>(dt => task2BegunTime = dt); void Synchronize(Action <ISynchronizer> action) { var curMeth = MethodBase.GetCurrentMethod(); var setter1 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Foo"), obj => (int)obj == 1); var setter2 = Synchronizable.SystemWideEventSet(Key.Get(curMeth, "Bar"), obj => (int)obj == 2); using (var sync = setter1.And(setter2.Delay(TimeSpan.FromMilliseconds(1000))).GetSynchronizer()) action(sync); } // Act Synchronize(sync => { var mre1 = new ST::ManualResetEvent(false); var task1 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain((task1BeginTime_Assign_, mre1_) => Synchronize(sync_ => { task1BeginTime_Assign_.Invoke(DateTimeOffset.Now); sync_.Begin(1).Wait(); mre1_.Set(); sync_.End(1).Wait(); }), task1BeginTime_Assign, mre1)); var mre2 = new ST::ManualResetEvent(false); var task2 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain((task2BegunTime_Assign_, mre2_) => Synchronize(sync_ => { sync_.Begin(2).Wait(); task2BegunTime_Assign_.Invoke(DateTimeOffset.Now); mre2_.Set(); sync_.End(2).Wait(); }), task2BegunTime_Assign, mre2)); sync.NotifyAll(false).Wait(); // Assert ST::WaitHandle.WaitAll(new[] { mre1, mre2 }); Assert.GreaterOrEqual(task2BegunTime - task1BeginTime, TimeSpan.FromMilliseconds(1000)); Task.WaitAll(task1, task2); }); }
public void Can_pause_apps_by_the_passed_time_span() { // Arrange var task1EndTime = default(DateTimeOffset); var task1EndTime_Assign = new MarshalByRefAction <DateTimeOffset>(dt => task1EndTime = dt); var task2BegunTime = default(DateTimeOffset); var task2BegunTime_Assign = new MarshalByRefAction <DateTimeOffset>(dt => task2BegunTime = dt); void Synchronize(Action <ISynchronizer> action) { var curMeth = MethodBase.GetCurrentMethod(); var waiter1 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Foo"), obj => (int)obj == 1); var waiter2 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Bar"), obj => (int)obj == 2); using (var sync = waiter1.Then(waiter2.Pause(TimeSpan.FromMilliseconds(1000))).GetSynchronizer()) action(sync); } // Act Synchronize(sync => { var task1 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain((task1EndTime_Assign_) => Synchronize(sync_ => { sync_.Begin(1).Wait(); task1EndTime_Assign_.Invoke(DateTimeOffset.Now); sync_.End(1).Wait(); }), task1EndTime_Assign)); var task2 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain((task2BegunTime_Assign_) => Synchronize(sync_ => { sync_.Begin(2).Wait(); task2BegunTime_Assign_.Invoke(DateTimeOffset.Now); sync_.End(2).Wait(); }), task2BegunTime_Assign)); sync.NotifyAll(false).Wait(); // Assert Assert.GreaterOrEqual(task2BegunTime - task1EndTime, TimeSpan.FromMilliseconds(1000)); Task.WaitAll(task1, task2); }); }
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); }
public void RunAtIsolatedProcess_can_run_repeatedly_even_if_an_exception_has_occurred() { // Arrange var errorAction = new Action(() => { throw new ApplicationException(); }); Assert.Throws <TargetInvocationException>(() => AppDomain.CurrentDomain.RunAtIsolatedProcess(errorAction)); var run = false; var run_Assign = new MarshalByRefAction <bool>(value => run = value); // Act AppDomain.CurrentDomain.RunAtIsolatedProcess(run_Assign_ => { run_Assign_.Invoke(true); }, run_Assign); // Assert Assert.IsTrue(run); }
public void Context_should_not_return_same_Monitor_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().WithMonitorsInProductionEnabled(); var runtime = PSharpRuntime.Create(configuration); var runtimeHost = HostInfo.NewRuntimeHost(runtime); using (var networkProvider = new DomainCommunicationProvider(runtimeHost, "monitors")) { var parameter = default(string); { runtimeHost.SetNetworkProvider(networkProvider); var ctx = runtimeHost.New <DistributedStorageContext>(); ctx.SafetyMonitor = runtimeHost.New(MonitorInterface.Sender <ISafetyMonitorSender>().Bundler <ISafetyMonitorBundler>().Receiver <SafetyMonitorReceiver>()); ctx.SafetyMonitor.Configure(new ConfigureSafetyMonitor(new MessageCollection())); expected_Assign_.Invoke(RuntimeHelpers.GetHashCode(ctx.SafetyMonitor)); parameter = ctx.ToJson(); } AppDomain.CurrentDomain.RunAtIsolatedDomain((actual_Assign__, parameter_) => { var ctx = parameter_.FromJson <DistributedStorageContext>(); actual_Assign__.Invoke(RuntimeHelpers.GetHashCode(ctx.SafetyMonitor)); ctx.SafetyMonitor.Handshake(new HandshakeSafetyMonitor(new IStorageNodeSender[0])); }, actual_Assign_, parameter); } }, expected_Assign, actual_Assign); // Assert Assert.AreNotEqual(expected, actual); }
public void Counter_can_communicate_remote_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().WithMonitorsInProductionEnabled(); var runtime = PSharpRuntime.Create(configuration); var runtimeHost = HostInfo.NewRuntimeHost(runtime); using (var monitorsNetworkProvider = new DomainCommunicationProvider(runtimeHost, "monitors")) { var parameter = default(string); { runtimeHost.SetNetworkProvider(monitorsNetworkProvider); var counter = runtimeHost.NewDistributedCounter(42); counter.Increment(); counter.Increment(); expected_Assign_.Invoke(counter.GetValue()); parameter = counter.ToJson(); } AppDomain.CurrentDomain.RunAtIsolatedDomain((actual_Assign__, parameter_) => { var counter = parameter_.FromJson <DistributedCounter>(); counter.Decrement(); actual_Assign__.Invoke(counter.GetValue()); }, actual_Assign_, parameter); } }, expected_Assign, actual_Assign); // Assert Assert.AreEqual(expected, actual + 1); }
public void RunAtIsolatedProcess_can_run_parallel() { // Arrange var n = 0; var n_Increment = new MarshalByRefAction(() => Interlocked.Increment(ref n)); var threads = new List <Thread>(); for (var i = 0; i < 10; i++) { var thread = new Thread(() => AppDomain.CurrentDomain.RunAtIsolatedProcess(n_Increment_ => n_Increment_.Invoke(), n_Increment)); threads.Add(thread); } // Act threads.ForEach(_ => _.Start()); threads.ForEach(_ => _.Join()); // Assert Assert.AreEqual(10, n); }
public void Can_restrain_apps_to_wait_until_ending_process_in_order_of_all_apps() { // Arrange var processes = new ConcurrentBag <int>(); var processes_Add = new MarshalByRefAction <int>(i => processes.Add(i)); void Synchronize(Action <ISynchronizer> action) { var begun = new HandledCallback((id, obj, opts) => { var threadId = ST::Thread.CurrentThread.ManagedThreadId; Debug.WriteLine($"Begun Id: { id }, Obj: { obj }, Thread: { threadId }"); }); var ended = new HandledCallback((id, obj, opts) => { var threadId = ST::Thread.CurrentThread.ManagedThreadId; Debug.WriteLine($"Ended Id: { id }, Obj: { obj }, Thread: { threadId }"); }); var curMeth = MethodBase.GetCurrentMethod(); var waiter1 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Foo"), obj => (int)obj == 1, begun, ended); var waiter2 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Bar"), obj => (int)obj == 2, begun, ended); var waiter3 = Synchronizable.SystemWideEventWait(Key.Get(curMeth, "Baz"), obj => (int)obj == 3, begun, ended); using (var sync = waiter1.Then(waiter2).Then(waiter3).GetSynchronizer()) action(sync); } // Act Synchronize(sync => { var task1 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain(processes_Add_ => Synchronize(sync_ => { sync_.Begin(1).Wait(); processes_Add_.Invoke(1); sync_.End(1).Wait(); }), processes_Add)); var task2 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain(processes_Add_ => Synchronize(sync_ => { sync_.Begin(2).Wait(); processes_Add_.Invoke(2); sync_.End(2).Wait(); }), processes_Add)); var task3 = Task.Run(() => AppDomain.CurrentDomain.RunAtIsolatedDomain(processes_Add_ => Synchronize(sync_ => { sync_.Begin(3).Wait(); processes_Add_.Invoke(3); sync_.End(3).Wait(); }), processes_Add)); sync.NotifyAll(false).Wait(); // Assert CollectionAssert.AreEqual(new[] { 1, 2, 3 }, processes); Task.WaitAll(task1, task2, task3); }); }
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); }); }