From c213a51b7055d2e43e963664fe00fe6621430732 Mon Sep 17 00:00:00 2001 From: Rolf Bjarne Kvinge Date: Wed, 14 Sep 2016 17:33:12 +0200 Subject: [PATCH] [System] Rewrite Dns tests so that failures don't cause the process to crash. Previously the tests were written so that some failures might occur on the threadpool in methods that did not catch exceptions. In that case the process would terminate due to an unhandled exception. So rewrite the tests to catch exceptions in any code that doesn't run on the main thread, and forward those exceptions to the main thread. Also wait for async operations to complete on the main thread, and remove asserts that are not supposed to be hit. --- mcs/class/System/Test/System.Net/DnsTest.cs | 71 ++++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/mcs/class/System/Test/System.Net/DnsTest.cs b/mcs/class/System/Test/System.Net/DnsTest.cs index dcc82f61678..e06ca4bf5d6 100644 --- a/mcs/class/System/Test/System.Net/DnsTest.cs +++ b/mcs/class/System/Test/System.Net/DnsTest.cs @@ -32,28 +32,37 @@ namespace MonoTests.System.Net [Test] public void AsyncGetHostByName () { - IAsyncResult r; - r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null); - IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null); IPHostEntry entry = Dns.EndGetHostByName (async); SubTestValidIPHostEntry (entry); Assert.IsTrue (entry.HostName == "google-public-dns-a.google.com"); } - void GetHostByNameCallback (IAsyncResult ar) - { - IPHostEntry h; - h = Dns.EndGetHostByName (ar); - SubTestValidIPHostEntry (h); + [Test] + public void AsyncGetHostByNameCallback () + { + var evt = new ManualResetEvent (false); + Exception ex = null; + Dns.BeginGetHostByName (site1Name, new AsyncCallback ((IAsyncResult ar) => + { + try { + IPHostEntry h; + h = Dns.EndGetHostByName (ar); + SubTestValidIPHostEntry (h); + } catch (Exception e) { + ex = e; + } finally { + evt.Set (); + } + }), null); + + Assert.IsTrue (evt.WaitOne (TimeSpan.FromSeconds (60)), "Wait"); + Assert.IsNull (ex, "Exception"); } [Test] public void AsyncResolve () { - IAsyncResult r; - r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null); - IAsyncResult async = Dns.BeginResolve (site1Dot, null, null); IPHostEntry entry = Dns.EndResolve (async); SubTestValidIPHostEntry (entry); @@ -61,10 +70,25 @@ namespace MonoTests.System.Net Assert.AreEqual (site1Dot, ip.ToString ()); } - void ResolveCallback (IAsyncResult ar) - { - IPHostEntry h = Dns.EndResolve (ar); - SubTestValidIPHostEntry (h); + [Test] + public void AsyncResolveCallback () + { + var evt = new ManualResetEvent (false); + Exception ex = null; + Dns.BeginResolve (site1Name, new AsyncCallback ((IAsyncResult ar) => + { + try { + IPHostEntry h = Dns.EndResolve (ar); + SubTestValidIPHostEntry (h); + } catch (Exception e) { + ex = e; + } finally { + evt.Set (); + } + }), null); + + Assert.IsTrue (evt.WaitOne (TimeSpan.FromSeconds (60)), "Wait"); + Assert.IsNull (ex, "Exception"); } [Test] @@ -73,7 +97,7 @@ namespace MonoTests.System.Net try { Dns.BeginGetHostAddresses ( (string) null, - new AsyncCallback (GetHostAddressesCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#1"); } catch (ArgumentNullException ex) { @@ -91,7 +115,7 @@ namespace MonoTests.System.Net try { Dns.BeginGetHostAddresses ( "0.0.0.0", - new AsyncCallback (GetHostAddressesCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#A1"); } catch (ArgumentException ex) { @@ -108,7 +132,7 @@ namespace MonoTests.System.Net try { Dns.BeginGetHostAddresses ( "::0", - new AsyncCallback (GetHostAddressesCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#B1"); } catch (ArgumentException ex) { @@ -122,10 +146,9 @@ namespace MonoTests.System.Net } } - void GetHostAddressesCallback (IAsyncResult ar) + void ShouldntBeCalled (IAsyncResult ar) { - IPAddress [] addresses = Dns.EndGetHostAddresses (ar); - Assert.IsNotNull (addresses); + Assert.Fail ("Should not be called"); } [Test] @@ -317,7 +340,7 @@ namespace MonoTests.System.Net { try { Dns.BeginResolve ((string) null, - new AsyncCallback (ResolveCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#1"); } catch (ArgumentNullException ex) { @@ -363,7 +386,7 @@ namespace MonoTests.System.Net try { Dns.BeginGetHostEntry ( (IPAddress) null, - new AsyncCallback (GetHostAddressesCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#1"); } catch (ArgumentNullException ex) { @@ -380,7 +403,7 @@ namespace MonoTests.System.Net try { Dns.BeginGetHostEntry ( (string) null, - new AsyncCallback (GetHostAddressesCallback), + new AsyncCallback (ShouldntBeCalled), null); Assert.Fail ("#1"); } catch (ArgumentNullException ex) { -- 2.25.1