Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ActionDebouncer.cs to support tests with no Sync context #15804

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/DynamoCoreWpf/Utilities/ActionDebouncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void Cancel()

/// <summary>
/// Delays the "action" for a "timeout" number of milliseconds
/// The input Action will run on same syncronization context as the Debounce method call.
/// The input Action will run on same syncronization context as the Debounce method call (or the thread pool if a sync context does not exist, ex. in non UI tests).
/// </summary>
/// <param name="timeout">Number of milliseconds to wait</param>
/// <param name="action">The action to execute after the timeout runs out.</param>
Expand All @@ -36,6 +36,25 @@ public void Debounce(int timeout, Action action)
Cancel();
cts = new CancellationTokenSource();

// The TaskScheduler.FromCurrentSynchronizationContext() exists only if there is a valid SyncronizationContex.
// Calling this method from a non UI thread could have a null SyncronizationContex.Current,
// so in that case we use the default TaskScheduler which uses the thread pool.
TaskScheduler taskScheduler = null;
if (SynchronizationContext.Current != null)
{// This should always be the case in UI threads.
taskScheduler = TaskScheduler.FromCurrentSynchronizationContext();
}
else
{
// This might happen when running tests in non UI threads.
// But if we are in a UI thread, then log this as a potential error.
if (System.Windows.Application.Current?.Dispatcher?.Thread == Thread.CurrentThread)
{// UI thread.
logger?.LogError("The UI thread does not seem to have a SyncronizationContext.");
}
taskScheduler = TaskScheduler.Default;
}

Task.Delay(timeout, cts.Token).ContinueWith((t) =>
{
try
Expand All @@ -50,7 +69,7 @@ public void Debounce(int timeout, Action action)
logger?.Log("Failed to run debounce action with the following error:");
logger?.Log(ex.ToString());
}
}, TaskScheduler.FromCurrentSynchronizationContext());
}, taskScheduler);
}

public void Dispose()
Expand Down
18 changes: 9 additions & 9 deletions test/DynamoCoreWpfTests/DynamoTestUIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void PrettyPrint(object obj)
Console.WriteLine("}");
}

internal void SetupStartupDiagnostics()
internal void StartupDiagnostics()
{
System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}");
TestUtilities.WebView2Tag = TestContext.CurrentContext.Test.Name;
Expand All @@ -69,16 +69,16 @@ internal void SetupStartupDiagnostics()
Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted;
}

internal void SetupBeforeCleanupDiagnostics()
internal void BeforeCleanupDiagnostics()
{
Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted;
if (!SkipDispatcherFlush)
{
DispatcherUtil.DoEventsLoop(() => DispatcherOpsCounter == 0);
}
Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted;
}

internal void SetupAfterCleanupDiagnostics()
internal void AfterCleanupDiagnostics()
{
TestUtilities.WebView2Tag = string.Empty;
using (var currentProc = Process.GetCurrentProcess())
Expand All @@ -93,10 +93,10 @@ internal void SetupAfterCleanupDiagnostics()
}
}

internal void SetupCleanupDiagnostics()
internal void CleanupDiagnostics()
{
SetupBeforeCleanupDiagnostics();
SetupAfterCleanupDiagnostics();
BeforeCleanupDiagnostics();
AfterCleanupDiagnostics();
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ protected string ExecutingDirectory
[SetUp]
public virtual void Start()
{
testDiagnostics.SetupStartupDiagnostics();
testDiagnostics.StartupDiagnostics();
var assemblyPath = Assembly.GetExecutingAssembly().Location;
preloader = new Preloader(Path.GetDirectoryName(assemblyPath));
preloader.Preload();
Expand Down Expand Up @@ -229,7 +229,7 @@ public void Exit()
{
Console.WriteLine(ex.StackTrace);
}
testDiagnostics.SetupAfterCleanupDiagnostics();
testDiagnostics.AfterCleanupDiagnostics();
}

protected virtual void GetLibrariesToPreload(List<string> libraries)
Expand Down
6 changes: 3 additions & 3 deletions test/DynamoCoreWpfTests/RecordedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class RecordedUnitTestBase : DynamoViewModelUnitTest

public override void Setup()
{
testDiagnostics.SetupStartupDiagnostics();
testDiagnostics.StartupDiagnostics();

base.Setup();
// Fixed seed randomizer for predictability.
Expand All @@ -63,10 +63,10 @@ public override void Setup()

public override void Cleanup()
{
testDiagnostics.SetupBeforeCleanupDiagnostics();
testDiagnostics.BeforeCleanupDiagnostics();
commandCallback = null;
base.Cleanup();
testDiagnostics.SetupAfterCleanupDiagnostics();
testDiagnostics.AfterCleanupDiagnostics();
}

#endregion
Expand Down
Loading