From 3bbecab0e09f3b99ef83e5d8c25fb5a7daa0a331 Mon Sep 17 00:00:00 2001 From: pinzart90 Date: Tue, 23 Apr 2024 16:38:10 -0400 Subject: [PATCH] udpate --- .../Scheduler/AsyncTaskExtensions.cs | 6 ++--- test/DynamoCoreWpfTests/DynamoTestUIBase.cs | 21 ++++++++++++++- test/DynamoCoreWpfTests/PreviewBubbleTests.cs | 26 +++++++------------ .../Utility/DispatcherUtil.cs | 6 ++--- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/DynamoCore/Scheduler/AsyncTaskExtensions.cs b/src/DynamoCore/Scheduler/AsyncTaskExtensions.cs index d80ff1d7b38..bab83a64845 100644 --- a/src/DynamoCore/Scheduler/AsyncTaskExtensions.cs +++ b/src/DynamoCore/Scheduler/AsyncTaskExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -27,7 +27,7 @@ public static IDisposable Then(this AsyncTask task, AsyncTaskCompletedHandler ac /// An IDisposable representing the event subscription public static IDisposable ThenPost(this AsyncTask task, AsyncTaskCompletedHandler action, SynchronizationContext context = null) { - if (context == null) context = new SynchronizationContext(); // uses the default + if (context == null) context = SynchronizationContext.Current ?? new SynchronizationContext(); // uses the current or a default return task.Then((t) => context.Post((_) => action(task), null)); } @@ -38,7 +38,7 @@ public static IDisposable ThenPost(this AsyncTask task, AsyncTaskCompletedHandle /// An IDisposable representing the event subscription public static IDisposable ThenSend(this AsyncTask task, AsyncTaskCompletedHandler action, SynchronizationContext context = null) { - if (context == null) context = new SynchronizationContext(); // uses the default + if (context == null) context = SynchronizationContext.Current ?? new SynchronizationContext(); // uses the current or a default return task.Then((t) => context.Send((_) => action(task), null)); } diff --git a/test/DynamoCoreWpfTests/DynamoTestUIBase.cs b/test/DynamoCoreWpfTests/DynamoTestUIBase.cs index f71bcbbbd33..d3ded8ba358 100644 --- a/test/DynamoCoreWpfTests/DynamoTestUIBase.cs +++ b/test/DynamoCoreWpfTests/DynamoTestUIBase.cs @@ -5,6 +5,7 @@ using System.Linq; using System.Reflection; using System.Threading; +using System.Threading.Tasks; using System.Windows; using System.Windows.Threading; using Dynamo.Configuration; @@ -15,7 +16,6 @@ using Dynamo.Nodes; using Dynamo.Scheduler; using Dynamo.ViewModels; -using Dynamo.Wpf.Utilities; using DynamoCoreWpfTests.Utility; using DynamoShapeManager; using DynamoUtilities; @@ -31,6 +31,7 @@ public class DynamoTestUIBase protected DynamoViewModel ViewModel { get; set; } protected DynamoView View { get; set; } protected DynamoModel Model { get; set; } + protected int DispatcherOpsCounter = 0; protected string ExecutingDirectory { @@ -39,6 +40,7 @@ protected string ExecutingDirectory protected string TempFolder { get; private set; } + static List names = []; [SetUp] public virtual void Start() { @@ -84,6 +86,8 @@ public virtual void Start() View.Show(); SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext()); + + Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted; } protected static void RaiseLoadedEvent(FrameworkElement element) @@ -96,6 +100,17 @@ protected static void RaiseLoadedEvent(FrameworkElement element) eventMethod.Invoke(element, new object[] { args }); } + private void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e) + { + e.Operation.Task.ContinueWith((t) => { + names.Remove(e.Operation.Task.Id); + Interlocked.Decrement(ref DispatcherOpsCounter); + }, TaskScheduler.Default); + + names.Add(e.Operation.Task.Id); + Interlocked.Increment(ref DispatcherOpsCounter); + } + /// /// Derived test classes can override this method to provide different configurations. /// @@ -114,6 +129,10 @@ protected virtual DynamoModel.IStartConfiguration CreateStartConfiguration(IPath [TearDown] public void Exit() { + Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted; + + DispatcherUtil.DoEventsLoop(() => DispatcherOpsCounter == 0); + //Ensure that we leave the workspace marked as //not having changes. ViewModel.HomeSpace.HasUnsavedChanges = false; diff --git a/test/DynamoCoreWpfTests/PreviewBubbleTests.cs b/test/DynamoCoreWpfTests/PreviewBubbleTests.cs index a38ccb58421..b3d5900ec6f 100644 --- a/test/DynamoCoreWpfTests/PreviewBubbleTests.cs +++ b/test/DynamoCoreWpfTests/PreviewBubbleTests.cs @@ -102,12 +102,9 @@ public void PreviewBubble_ListMargin() nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); // Fire transition on dynamo main ui thread. - View.Dispatcher.Invoke(() => - { - nodeView.PreviewControl.BindToDataSource(); - nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); - nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded); - }); + nodeView.PreviewControl.BindToDataSource(); + nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); + nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded); DispatcherUtil.DoEvents(); var watchTree = nodeView.PreviewControl.ChildOfType(); @@ -124,12 +121,9 @@ public void PreviewBubble_NumberMargin() nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); // Fire transition on dynamo main ui thread. - View.Dispatcher.Invoke(() => - { - nodeView.PreviewControl.BindToDataSource(); - nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); - nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded); - }); + nodeView.PreviewControl.BindToDataSource(); + nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); + nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Expanded); DispatcherUtil.DoEvents(); var watchTree = nodeView.PreviewControl.ChildOfType(); @@ -145,11 +139,9 @@ public void PreviewBubble_CloseWhenFocusInCodeBlock() var nodeView = NodeViewWithGuid("1382aaf9-9432-4cf0-86ae-c586d311767e"); nodeView.PreviewControl.RaiseEvent(new RoutedEventArgs(FrameworkElement.LoadedEvent)); - View.Dispatcher.Invoke(() => - { - nodeView.PreviewControl.BindToDataSource(); - nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); - }); + nodeView.PreviewControl.BindToDataSource(); + nodeView.PreviewControl.TransitionToState(Dynamo.UI.Controls.PreviewControl.State.Condensed); + DispatcherUtil.DoEvents(); nodeView.ChildOfType().Focus(); diff --git a/test/DynamoCoreWpfTests/Utility/DispatcherUtil.cs b/test/DynamoCoreWpfTests/Utility/DispatcherUtil.cs index 18812562d00..529c0a7d3f0 100644 --- a/test/DynamoCoreWpfTests/Utility/DispatcherUtil.cs +++ b/test/DynamoCoreWpfTests/Utility/DispatcherUtil.cs @@ -17,7 +17,7 @@ public static class DispatcherUtil public static void DoEvents() { var frame = new DispatcherFrame(); - Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.Background, + Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle, new DispatcherOperationCallback(ExitFrame), frame); Dispatcher.PushFrame(frame); } @@ -29,7 +29,7 @@ public static void DoEvents() /// When check returns true, the even loop is stopped. public static void DoEventsLoop(Func check = null) { - const int max_count = 200; + const int max_count = 20; int count = 0; while (true) @@ -44,7 +44,7 @@ public static void DoEventsLoop(Func check = null) } DispatcherUtil.DoEvents(); - Thread.Sleep(100); + Thread.Sleep(1000); count++; } }