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

[DNM] [WIP] run tests in separate threads #15189

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions test/DynamoCoreWpfTests/AnnotationViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected override void GetLibrariesToPreload(List<string> libraries)
base.GetLibrariesToPreload(libraries);
}

[Test]
[TestOnSeparateThread]
public void TestAnnotationMouseClickEvent()
{
Open(@"UI\GroupTest.dyn");
Expand All @@ -73,7 +73,7 @@ public void TestAnnotationMouseClickEvent()
Assert.AreEqual(2, DynamoSelection.Instance.Selection.Count());
}

[Test]
[TestOnSeparateThread]
public void TestAnnotationOnDummyNodes()
{
Open(@"UI\GroupDummyNodes.dyn");
Expand All @@ -90,7 +90,7 @@ public void TestAnnotationOnDummyNodes()
Assert.AreEqual(1,modelCount);
}

[Test]
[TestOnSeparateThread]
public void UngroupingCollapsedGroupWillUnCollapseAllGroupContent()
{
// Arrange
Expand Down
18 changes: 9 additions & 9 deletions test/DynamoCoreWpfTests/ConnectorContextMenuTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace DynamoCoreWpfTests
{
public class ConnectorContextMenuTests: DynamoTestUIBase
{
[Test]
[TestOnSeparateThread]
public void ConstructingContextMenuTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -32,7 +32,7 @@ public void ConstructingContextMenuTest()
Assert.AreEqual(connectorViewModel.MousePosition, contextMenuViewModel.CurrentPosition);
}

[Test]
[TestOnSeparateThread]
public void SetContextMenuToNullAfterUseTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -46,7 +46,7 @@ public void SetContextMenuToNullAfterUseTest()
//Assert property on ConnectorViewModel is set to null.
Assert.IsNull(connectorViewModel.ConnectorContextMenuViewModel);
}
[Test]
[TestOnSeparateThread]
public void BreakConnectionFromContextMenuTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -63,7 +63,7 @@ public void BreakConnectionFromContextMenuTest()
//Assert that the only connector in the model has been destroyed.
Assert.AreEqual(connectorCount-1, updatedConnectorCount);
}
[Test]
[TestOnSeparateThread]
public void SelectedConnectedFromContextMenuTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -78,7 +78,7 @@ public void SelectedConnectedFromContextMenuTest()
//Assert that the selection of the two adjacent nodes is accurate.
Assert.AreEqual(DynamoSelection.Instance.Selection.Count, initialSelectedCount + 2);
}
[Test]
[TestOnSeparateThread]
public void HideConnectorFromContextMenuTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -94,7 +94,7 @@ public void HideConnectorFromContextMenuTest()
}


[Test]
[TestOnSeparateThread]
public void AreNodeConnectionsInMenu()
{
// Mock a WorkspaceView
Expand All @@ -107,7 +107,7 @@ public void AreNodeConnectionsInMenu()
Assert.AreEqual(contextMenu.Items.Count, 2);
}

[Test]
[TestOnSeparateThread]
public void ShowAllConnectorFromWorkspaceContextMenuTest()
{
Open(@"UI/ConnectorShowHideAllWires.dyn");
Expand All @@ -134,7 +134,7 @@ public void ShowAllConnectorFromWorkspaceContextMenuTest()
Assert.AreEqual(4, hiddenConnectors.Count());
}

[Test]
[TestOnSeparateThread]
public void GoToStartNodeTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand All @@ -149,7 +149,7 @@ public void GoToStartNodeTest()
Assert.AreEqual(connectorViewModel.ConnectorModel.Start.Owner.IsSelected, true);
}

[Test]
[TestOnSeparateThread]
public void GoToEndNodeTest()
{
Open(@"UI/ConnectorContextMenuTestFile.dyn");
Expand Down
36 changes: 18 additions & 18 deletions test/DynamoCoreWpfTests/ConnectorViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq;
using System.Linq;
using Dynamo.Selection;
using NUnit.Framework;
using static Dynamo.Models.DynamoModel;
Expand All @@ -17,7 +17,7 @@ public class ConnectorViewModelTests : DynamoTestUIBase
/// <summary>
/// Check to see if a connector is visible after pre 2.13 graph open
/// </summary>
[Test]
[TestOnSeparateThread]
public void ConnectorVisibilityForLegacyGraphTest()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -31,7 +31,7 @@ public void ConnectorVisibilityForLegacyGraphTest()
/// <summary>
/// Check to see a connector is visible after xml graph open
/// </summary>
[Test]
[TestOnSeparateThread]
public void ConnectorVisibilityForLegacyXMLGraphTest()
{
var filePath = @"UI/UI_visual_test.dyn";
Expand All @@ -50,7 +50,7 @@ public void ConnectorVisibilityForLegacyXMLGraphTest()
/// <summary>
/// Check to see a pin can be added to a connector
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanPinConnector()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand Down Expand Up @@ -80,7 +80,7 @@ public void CanPinConnector()
/// <summary>
/// Check to see a pin can be removed from a connector
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUnPinFromConnector()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -107,7 +107,7 @@ public void CanUnPinFromConnector()
/// <summary>
/// Check to see if can select connected nodes.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanSelectConnectedNodes()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -122,7 +122,7 @@ public void CanSelectConnectedNodes()
/// <summary>
/// Check to see if can break connection.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanBreakConnection()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -138,7 +138,7 @@ public void CanBreakConnection()
/// a command that toggles between 'IsVisible' and '!IsVisible'. So this test
/// verifies that this works as expected.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanHideConnector()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -153,7 +153,7 @@ public void CanHideConnector()
/// Check to see if can unhide connector. 'HideConnection' toggles the visibility
/// of the wire, so calling it twice should unhide the connector.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUnhideConnector()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -170,7 +170,7 @@ public void CanUnhideConnector()
/// <summary>
/// Can place WatchNode where specified along a connector
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanPlaceWatchNode()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -192,7 +192,7 @@ public void CanPlaceWatchNode()
/// Assert that a watch node is placed on the correct connector,
/// and that it rewires new wires to the correct startind/ending ports.
/// </summary>
[Test]
[TestOnSeparateThread]
public void PlaceWatchNodeAndRewireCorrectly()
{
Open(@"UI/WatchNodePlacement.dyn");
Expand Down Expand Up @@ -244,7 +244,7 @@ public void PlaceWatchNodeAndRewireCorrectly()
/// This occurs when a watch node is placed. New connectors are created and the old pin
/// locations are used to place new pins where the old ones were.
/// </summary>
[Test]
[TestOnSeparateThread]
public void PinPlacedOnCorrectWireToWatchNode()
{
Open(@"UI/WatchNodePlacement.dyn");
Expand Down Expand Up @@ -293,7 +293,7 @@ public void PinPlacedOnCorrectWireToWatchNode()
/// <summary>
/// Can undo 'placepin' command.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUndoPin()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand All @@ -318,7 +318,7 @@ public void CanUndoPin()
/// <summary>
/// Can undo 'Unpin' command
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUndoUnpin()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand Down Expand Up @@ -347,7 +347,7 @@ public void CanUndoUnpin()
/// <summary>
/// Can undo 'delete pin' command.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUndoDeletePin()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand Down Expand Up @@ -376,7 +376,7 @@ public void CanUndoDeletePin()
/// <summary>
/// Can undo 'drag pin'.
/// </summary>
[Test]
[TestOnSeparateThread]
public void CanUndoDragPin()
{
Open(@"UI/ConnectorPinTests.dyn");
Expand Down Expand Up @@ -431,7 +431,7 @@ public void CanUndoDragPin()
Assert.AreEqual(initialY, connectorPin.CenterY);
}

[Test]
[TestOnSeparateThread]
public void CanUndoPlaceWatchNode()
{
Open(@"UI/WatchNodePlacement.dyn");
Expand All @@ -457,4 +457,4 @@ public void CanUndoPlaceWatchNode()
}
#endregion
}
}
}
8 changes: 4 additions & 4 deletions test/DynamoCoreWpfTests/ConverterViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using CoreNodeModels;
Expand Down Expand Up @@ -46,7 +46,7 @@ protected override void GetLibrariesToPreload(List<string> libraries)
/// public ConversionUnit SelectedToConversion
/// private void model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
/// </summary>
[Test]
[TestOnSeparateThread]
public void ConverterViewModel_LengthInchesToCentimetersTest()
{
float inchesToCmFactor = (float)2.54;
Expand Down Expand Up @@ -85,7 +85,7 @@ public void ConverterViewModel_LengthInchesToCentimetersTest()
/// public bool IsSelectionFromBoxEnabled
/// public string SelectionFromBoxToolTip
/// </summary>
[Test]
[TestOnSeparateThread]
public void ConverterViewModel_TooltipTest()
{
float inchesToCmFactor = (float)2.54;
Expand Down Expand Up @@ -128,7 +128,7 @@ public void ConverterViewModel_TooltipTest()
/// public List<ConversionUnit> SelectedFromConversionSource
/// public List<ConversionUnit> SelectedToConversionSource
/// </summary>
[Test]
[TestOnSeparateThread]
public void ConverterViewModel_ItemsSourceCentimetersToMetersTest()
{
Open(@"core\library\converterNodeTest.dyn");
Expand Down
34 changes: 32 additions & 2 deletions test/DynamoCoreWpfTests/DynamoTestUIBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,40 @@
using DynamoShapeManager;
using DynamoUtilities;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using TestServices;

namespace DynamoCoreWpfTests
{
internal class IsolatedTestCommand(TestCommand innerCommand) : DelegatingTestCommand(innerCommand)
{
public override TestResult Execute(TestExecutionContext context)
{
var t = new Thread(() =>
{
context.CurrentResult = innerCommand.Execute(context);
Dispatcher.CurrentDispatcher?.InvokeShutdown();
});

t.SetApartmentState(ApartmentState.STA);

t.Start();
t.Join();
return context.CurrentResult;
}
}

[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal class TestOnSeparateThreadAttribute : TestAttribute, IWrapSetUpTearDown
{
public TestCommand Wrap(TestCommand command)
{
return new IsolatedTestCommand(command);
}
}

Copy link
Contributor Author

@pinzart90 pinzart90 May 3, 2024

Choose a reason for hiding this comment

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

this is the main code change

Copy link
Contributor

Choose a reason for hiding this comment

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

Are those threads reused in this context?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. Each thread will be disposed after it is used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ugh, looks like there is cross thread access to the UI, probably some static UI resources or something similar…

Copy link
Member

Choose a reason for hiding this comment

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

there are lots of static events and static singletons now. It's good for all of us to keep in mind that even if we don't intend to create multiple instances of a type in a shipped product we might need to do so during testing.

public class DynamoTestUIBase
{
protected Preloader preloader;
Expand All @@ -50,6 +80,8 @@ public virtual void Start()
System.Console.WriteLine($"PID {Process.GetCurrentProcess().Id} Start test: {TestContext.CurrentContext.Test.Name}");
TestUtilities.WebView2Tag = TestContext.CurrentContext.Test.Name;

SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext());

var assemblyPath = Assembly.GetExecutingAssembly().Location;
preloader = new Preloader(Path.GetDirectoryName(assemblyPath));
preloader.Preload();
Expand Down Expand Up @@ -88,8 +120,6 @@ public virtual void Start()
View = new DynamoView(ViewModel);
View.Show();

SynchronizationContext.SetSynchronizationContext(new DispatcherSynchronizationContext());

if (!SkipDispatcherFlush)
{
Dispatcher.CurrentDispatcher.Hooks.OperationPosted += Hooks_OperationPosted;
Expand Down
8 changes: 4 additions & 4 deletions test/DynamoCoreWpfTests/DynamoViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected override void GetLibrariesToPreload(List<string> libraries)
libraries.Add("FFITarget.dll");
}

[Test]
[TestOnSeparateThread]
public void FooterNotificationControlTest()
{
// Arrange
Expand Down Expand Up @@ -95,7 +95,7 @@ public void FooterNotificationControlTest()
Assert.AreEqual((items[1] as FooterNotificationItem).NotificationCount, 0);
}

[Test]
[TestOnSeparateThread]
public void OpeningWorkspaceWithTclsrustWarning()
{
// Open workspace with test mode as false, to verify trust warning.
Expand All @@ -112,7 +112,7 @@ public void OpeningWorkspaceWithTclsrustWarning()
DynamoModel.IsTestMode = true;
}

[Test]
[TestOnSeparateThread]
public void ElementBinding_SaveAs()
{
var prebindingPathInTestDir = @"core\callsite\trace_test-prebinding.dyn";
Expand Down Expand Up @@ -155,7 +155,7 @@ public void ElementBinding_SaveAs()
File.Delete(saveAsPath);
}

[Test]
[TestOnSeparateThread]
public void TestToastNotificationClosingBehavior()
{
var preferencesWindow = new PreferencesView(View);
Expand Down
Loading
Loading