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

Stabilize tests pt2 #15779

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
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ async void InitializeAsync()
//This indicates in which location will be created the WebView2 cache folder
documentationBrowser.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = WebBrowserUserDataFolder
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(DocumentationBrowserView)) : WebBrowserUserDataFolder
};
}

Expand Down
5 changes: 3 additions & 2 deletions src/DynamoCoreWpf/Utilities/ResourceUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Dynamo.Logging;
using Dynamo.Models;
using Dynamo.Wpf.Properties;
using Dynamo.Wpf.UI.GuidedTour;
using Dynamo.Wpf.Utilities;
using DynamoUtilities;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -598,7 +599,7 @@ internal static async Task<object> ExecuteJSFunction(UIElement MainWindow, objec
//This indicates in which location will be created the WebView2 cache folder
webBrowserComponent.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = userDataFolder
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(ResourceUtilities)) : userDataFolder
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Views/HomePage/HomePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private async void UserControl_Loaded(object sender, System.Windows.RoutedEventA

dynWebView.CreationProperties = new CoreWebView2CreationProperties
{
UserDataFolder = webBrowserUserDataFolder.FullName
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(HomePage)) : webBrowserUserDataFolder.FullName
};

//ContentRendered ensures that the webview2 component is visible.
Expand Down
2 changes: 1 addition & 1 deletion src/DynamoCoreWpf/Views/SplashScreen/SplashScreen.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ protected override async void OnContentRendered(EventArgs e)

webView.CreationProperties = new CoreWebView2CreationProperties
{
UserDataFolder = webBrowserUserDataFolder.FullName
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(SplashScreen)) : webBrowserUserDataFolder.FullName
};

//ContentRendered ensures that the webview2 component is visible.
Expand Down
9 changes: 9 additions & 0 deletions src/DynamoUtilities/TestUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -11,5 +14,11 @@ internal static class TestUtilities
{
// Simple string that we can store in DynamoWebView2 instances so that we can track them down more easily
internal static string WebView2Tag;

internal static string UserDataFolderDuringTests(string appName)
{
var directory = new DirectoryInfo(Assembly.GetExecutingAssembly().Location);
return Path.Combine(directory.Parent.Parent.Parent.FullName, "test", $"webview2_{Environment.ProcessId}_{appName}_appdata");
}
}
}
2 changes: 1 addition & 1 deletion src/LibraryViewExtensionWebView2/LibraryViewController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ async void InitializeAsync()
//This indicates in which location will be created the WebView2 cache folder
this.browser.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = WebBrowserUserDataFolder
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(LibraryViewController)) : WebBrowserUserDataFolder
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Notifications/NotificationCenterController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private async void InitializeBrowserAsync(object sender, RoutedEventArgs e)
//This indicates in which location will be created the WebView2 cache folder
notificationUIPopup.webView.CreationProperties = new CoreWebView2CreationProperties()
{
UserDataFolder = webBrowserUserDataFolder.FullName
UserDataFolder = DynamoModel.IsTestMode ? TestUtilities.UserDataFolderDuringTests(nameof(NotificationCenterController)) : webBrowserUserDataFolder.FullName
};
}
notificationUIPopup.webView.CoreWebView2InitializationCompleted += WebView_CoreWebView2InitializationCompleted;
Expand Down
23 changes: 21 additions & 2 deletions test/DynamoCoreWpfTests/RecordedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
using Dynamo.Tests;
using Dynamo.Utilities;
using Dynamo.ViewModels;
using DynamoShapeManager;
using NUnit.Framework;
using ProtoCore;
using PythonNodeModels;
using SystemTestServices;
using TestServices;
using System.Windows.Threading;
using DynamoCoreWpfTests.Utility;
using System.Diagnostics;
using System.Threading.Tasks;

namespace DynamoCoreWpfTests
{
Expand All @@ -48,17 +50,34 @@ public class RecordedUnitTestBase : DynamoViewModelUnitTest
protected WorkspaceViewModel workspaceViewModel = null;
protected double tolerance = 1e-6;
protected double codeBlockPortHeight = Configurations.PortHeightInPixels;
protected int DispatcherOpsCounter = 0;

private void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e)
{
e.Operation.Task.ContinueWith((t) => Interlocked.Decrement(ref DispatcherOpsCounter));
Interlocked.Increment(ref DispatcherOpsCounter);
}

public override void Setup()
{
base.Setup();

Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted;
// Fixed seed randomizer for predictability.
randomizer = new System.Random(123456);
}

public override void Cleanup()
{
Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted;
DispatcherUtil.DoEventsLoop(() => DispatcherOpsCounter == 0);

var name = TestContext.CurrentContext.Test.Name;
using (var currentProc = Process.GetCurrentProcess())
{
System.Console.WriteLine($"PID {currentProc.Id} Finished test: {name} with DispatcherOpsCounter = {DispatcherOpsCounter}");
Copy link
Member

@mjkkirschner mjkkirschner Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the memory logging as well?
Maybe we can create a new class TestDiagnostics ? or something to avoid copying this code around?
Or add it to the all the test base classes?

}

commandCallback = null;
base.Cleanup();
}
Expand Down
18 changes: 18 additions & 0 deletions test/DynamoCoreWpfTests/SplashScreenTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using DynamoUtilities;
using DynamoCoreWpfTests.Utility;
using NUnit.Framework;
using System.Windows.Threading;

namespace DynamoCoreWpfTests
{
Expand All @@ -32,6 +33,8 @@ public void SplashScreen_ClosePersistSetsPrefs()
[TestFixture]
internal class SplashScreenTests
{
protected int DispatcherOpsCounter = 0;

public enum WindowsMessage
{
WM_CLOSE = 0x0010
Expand All @@ -46,19 +49,34 @@ public enum WindowsMessage
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);

private void Hooks_OperationPosted(object sender, DispatcherHookEventArgs e)
{
e.Operation.Task.ContinueWith((t) => Interlocked.Decrement(ref DispatcherOpsCounter));
Interlocked.Increment(ref DispatcherOpsCounter);
}

[SetUp]
public void SetUp()
{
TestUtilities.WebView2Tag = TestContext.CurrentContext.Test.Name;
Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted;
}

[TearDown]
public void CleanUp()
{
Dispatcher.CurrentDispatcher.Hooks.OperationPosted -= Hooks_OperationPosted;
DispatcherUtil.DoEventsLoop(() => DispatcherOpsCounter == 0);

var name = TestContext.CurrentContext.Test.Name;
using (var currentProc = Process.GetCurrentProcess())
{
System.Console.WriteLine($"PID {currentProc.Id} Finished test: {name} with DispatcherOpsCounter = {DispatcherOpsCounter}");
}
TestUtilities.WebView2Tag = string.Empty;
}


[Test]
public void SplashScreen_CloseExplicitPropIsCorrect1()
{
Expand Down
Loading