From e0d21548fa0349b37431ea7fe652b38d009f66b6 Mon Sep 17 00:00:00 2001 From: Alan McGovern Date: Wed, 26 Sep 2018 11:53:41 +0100 Subject: [Tests] Ensure async void exceptions propagate correctly This should ensure that exceptions thrown inside async void methods do get rethrown as part of the Teardown of a given test. This removes the eventhandlers on AppDomain.UnhandledException and also removes the second SyncContext implementation which did not capture/propagate the exceptions. --- .../AddValueConverterViewModelTests.cs | 6 +++--- .../AsyncSynchronizationContext.cs | 17 ----------------- .../CollectionPropertyViewModelTests.cs | 6 +++--- .../CreateBindingViewModelTests.cs | 22 +++------------------- .../ObjectPropertyViewModelTests.cs | 6 +++--- .../PanelViewModelTests.cs | 7 ------- .../PropertiesViewModelTests.cs | 22 +++++++--------------- .../PropertyViewModelTests.cs | 6 +++--- .../Xamarin.PropertyEditing.Tests.csproj | 1 - 9 files changed, 22 insertions(+), 71 deletions(-) delete mode 100644 Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs (limited to 'Xamarin.PropertyEditing.Tests') diff --git a/Xamarin.PropertyEditing.Tests/AddValueConverterViewModelTests.cs b/Xamarin.PropertyEditing.Tests/AddValueConverterViewModelTests.cs index c323cc2..e107c19 100644 --- a/Xamarin.PropertyEditing.Tests/AddValueConverterViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/AddValueConverterViewModelTests.cs @@ -14,15 +14,15 @@ namespace Xamarin.PropertyEditing.Tests [SetUp] public void Setup () { - this.syncContext = new AsyncSynchronizationContext (); + this.syncContext = new TestContext (); SynchronizationContext.SetSynchronizationContext (this.syncContext); } [TearDown] public void TearDown () { - this.syncContext.WaitForPendingOperationsToComplete (); SynchronizationContext.SetSynchronizationContext (null); + this.syncContext.ThrowPendingExceptions (); } [Test] @@ -75,6 +75,6 @@ namespace Xamarin.PropertyEditing.Tests Assert.That (vm.ConverterName, Is.EqualTo (type.Name)); } - private AsyncSynchronizationContext syncContext; + private TestContext syncContext; } } diff --git a/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs b/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs deleted file mode 100644 index 34a7086..0000000 --- a/Xamarin.PropertyEditing.Tests/AsyncSynchronizationContext.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System.Threading; - -namespace Xamarin.PropertyEditing.Tests -{ - internal class AsyncSynchronizationContext - : SynchronizationContext - { - public override void Post (SendOrPostCallback d, object state) - { - d (state); - } - - public void WaitForPendingOperationsToComplete () - { - } - } -} \ No newline at end of file diff --git a/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs index 7f87448..daaa32d 100644 --- a/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/CollectionPropertyViewModelTests.cs @@ -16,20 +16,20 @@ namespace Xamarin.PropertyEditing.Tests [TestFixture] internal class CollectionPropertyViewModelTests { - private AsyncSynchronizationContext syncContext; + private TestContext syncContext; [SetUp] public void Setup () { - this.syncContext = new AsyncSynchronizationContext (); + this.syncContext = new TestContext (); SynchronizationContext.SetSynchronizationContext (this.syncContext); } [TearDown] public void TearDown () { - this.syncContext.WaitForPendingOperationsToComplete (); SynchronizationContext.SetSynchronizationContext (null); + this.syncContext.ThrowPendingExceptions (); } [Test] diff --git a/Xamarin.PropertyEditing.Tests/CreateBindingViewModelTests.cs b/Xamarin.PropertyEditing.Tests/CreateBindingViewModelTests.cs index 2cf707b..0c3b16b 100644 --- a/Xamarin.PropertyEditing.Tests/CreateBindingViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/CreateBindingViewModelTests.cs @@ -22,31 +22,15 @@ namespace Xamarin.PropertyEditing.Tests [SetUp] public void Setup () { - AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; - this.syncContext = new AsyncSynchronizationContext (); + this.syncContext = new TestContext (); SynchronizationContext.SetSynchronizationContext (this.syncContext); } - private Exception unhandled; - - private void CurrentDomainOnUnhandledException (object sender, UnhandledExceptionEventArgs e) - { - this.unhandled = e.ExceptionObject as Exception; - } - [TearDown] public void TearDown () { - this.syncContext.WaitForPendingOperationsToComplete (); SynchronizationContext.SetSynchronizationContext (null); - - AppDomain.CurrentDomain.UnhandledException -= CurrentDomainOnUnhandledException; - - if (this.unhandled != null) { - var ex = this.unhandled; - this.unhandled = null; - Assert.Fail ("Unhandled exception: {0}", ex); - } + this.syncContext.ThrowPendingExceptions (); } [Test] @@ -630,7 +614,7 @@ namespace Xamarin.PropertyEditing.Tests vm.BindingProperties.Cast ().Select (pvm => pvm.Property)); } - private AsyncSynchronizationContext syncContext; + private TestContext syncContext; private static readonly ResourceSource[] DefaultResourceSources = new[] { MockResourceProvider.SystemResourcesSource, MockResourceProvider.ApplicationResourcesSource }; private Mock GetBasicProperty (string name = "propertyName") diff --git a/Xamarin.PropertyEditing.Tests/ObjectPropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/ObjectPropertyViewModelTests.cs index 426cdd7..193bc09 100644 --- a/Xamarin.PropertyEditing.Tests/ObjectPropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/ObjectPropertyViewModelTests.cs @@ -17,15 +17,15 @@ namespace Xamarin.PropertyEditing.Tests [SetUp] public void Setup () { - this.syncContext = new AsyncSynchronizationContext(); + this.syncContext = new TestContext (); SynchronizationContext.SetSynchronizationContext (this.syncContext); } [TearDown] public void TearDown () { - this.syncContext.WaitForPendingOperationsToComplete (); SynchronizationContext.SetSynchronizationContext (null); + syncContext.ThrowPendingExceptions (); } [Test] @@ -255,7 +255,7 @@ namespace Xamarin.PropertyEditing.Tests Assert.That (vm.ValueType, Is.Null); } - private AsyncSynchronizationContext syncContext; + private TestContext syncContext; private Mock CreateProviderMock (object value, IObjectEditor editor) { diff --git a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs index 7b51e8f..723d86f 100644 --- a/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PanelViewModelTests.cs @@ -17,13 +17,6 @@ namespace Xamarin.PropertyEditing.Tests internal class PanelViewModelTests : PropertiesViewModelTests { - [SetUp] - public override void Setup () - { - base.Setup (); - SynchronizationContext.SetSynchronizationContext (this.context = new TestContext ()); - } - [Test] [Description ("We must be sure that if the selected objects list changes while the provider is still retrieving that we end up with the right result")] public async Task InteruptedEditorRetrievalResolvesCorrectlyItemAdded () diff --git a/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs index a7e98a9..6560aa9 100644 --- a/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PropertiesViewModelTests.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using Moq; using NUnit.Framework; @@ -35,28 +36,19 @@ namespace Xamarin.PropertyEditing.Tests } } - private Exception unhandled; + private TestContext syncContext; [SetUp] - public virtual void Setup () + public void Setup () { - AppDomain.CurrentDomain.UnhandledException += CurrentDomainOnUnhandledException; + this.syncContext = new TestContext (); + SynchronizationContext.SetSynchronizationContext (this.syncContext); } [TearDown] public void TearDown () { - AppDomain.CurrentDomain.UnhandledException -= CurrentDomainOnUnhandledException; - - if (this.unhandled != null) { - var ex = this.unhandled; - this.unhandled = null; - Assert.Fail ("Unhandled exception: {0}", ex); - } - } - - private void CurrentDomainOnUnhandledException (object sender, UnhandledExceptionEventArgs e) - { - this.unhandled = e.ExceptionObject as Exception; + SynchronizationContext.SetSynchronizationContext (null); + this.syncContext.ThrowPendingExceptions (); } [Test] diff --git a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs index bb719e8..2c49a80 100644 --- a/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs +++ b/Xamarin.PropertyEditing.Tests/PropertyViewModelTests.cs @@ -24,15 +24,15 @@ namespace Xamarin.PropertyEditing.Tests [SetUp] public void Setup () { - this.syncContext = new AsyncSynchronizationContext(); + this.syncContext = new TestContext (); SynchronizationContext.SetSynchronizationContext (this.syncContext); } [TearDown] public void TearDown () { - this.syncContext.WaitForPendingOperationsToComplete (); SynchronizationContext.SetSynchronizationContext (null); + this.syncContext.ThrowPendingExceptions (); } [Test] @@ -1467,6 +1467,6 @@ namespace Xamarin.PropertyEditing.Tests } private readonly Random rand = new Random (42); - private AsyncSynchronizationContext syncContext; + private TestContext syncContext; } } \ No newline at end of file diff --git a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj index c5419cd..1e0355f 100644 --- a/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj +++ b/Xamarin.PropertyEditing.Tests/Xamarin.PropertyEditing.Tests.csproj @@ -58,7 +58,6 @@ - -- cgit v1.2.3