Skip to content

Commit

Permalink
udpate
Browse files Browse the repository at this point in the history
  • Loading branch information
pinzart90 committed Apr 23, 2024
1 parent 2bcb444 commit 3bbecab
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 24 deletions.
6 changes: 3 additions & 3 deletions src/DynamoCore/Scheduler/AsyncTaskExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -27,7 +27,7 @@ public static IDisposable Then(this AsyncTask task, AsyncTaskCompletedHandler ac
/// <returns>An IDisposable representing the event subscription</returns>
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));
}

Expand All @@ -38,7 +38,7 @@ public static IDisposable ThenPost(this AsyncTask task, AsyncTaskCompletedHandle
/// <returns>An IDisposable representing the event subscription</returns>
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));
}

Expand Down
21 changes: 20 additions & 1 deletion test/DynamoCoreWpfTests/DynamoTestUIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,7 +16,6 @@
using Dynamo.Nodes;
using Dynamo.Scheduler;
using Dynamo.ViewModels;
using Dynamo.Wpf.Utilities;
using DynamoCoreWpfTests.Utility;
using DynamoShapeManager;
using DynamoUtilities;
Expand All @@ -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
{
Expand All @@ -39,6 +40,7 @@ protected string ExecutingDirectory

protected string TempFolder { get; private set; }

static List<int> names = [];
[SetUp]
public virtual void Start()
{
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}

/// <summary>
/// Derived test classes can override this method to provide different configurations.
/// </summary>
Expand All @@ -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;
Expand Down
26 changes: 9 additions & 17 deletions test/DynamoCoreWpfTests/PreviewBubbleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WatchTree>();
Expand All @@ -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<WatchTree>();
Expand All @@ -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<ICSharpCode.AvalonEdit.Editing.TextArea>().Focus();

Expand Down
6 changes: 3 additions & 3 deletions test/DynamoCoreWpfTests/Utility/DispatcherUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -29,7 +29,7 @@ public static void DoEvents()
/// <param name="check">When check returns true, the even loop is stopped.</param>
public static void DoEventsLoop(Func<bool> check = null)
{
const int max_count = 200;
const int max_count = 20;

int count = 0;
while (true)
Expand All @@ -44,7 +44,7 @@ public static void DoEventsLoop(Func<bool> check = null)
}

DispatcherUtil.DoEvents();
Thread.Sleep(100);
Thread.Sleep(1000);
count++;
}
}
Expand Down

0 comments on commit 3bbecab

Please sign in to comment.