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.
[Test]\r
public void AsyncGetHostByName ()\r
{\r
[Test]\r
public void AsyncGetHostByName ()\r
{\r
- IAsyncResult r;\r
- r = Dns.BeginGetHostByName (site1Name, new AsyncCallback (GetHostByNameCallback), null);\r
-\r
IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);\r
IPHostEntry entry = Dns.EndGetHostByName (async);\r
SubTestValidIPHostEntry (entry);\r
Assert.IsTrue (entry.HostName == "google-public-dns-a.google.com");\r
}\r
\r
IAsyncResult async = Dns.BeginGetHostByName (site1Name, null, null);\r
IPHostEntry entry = Dns.EndGetHostByName (async);\r
SubTestValidIPHostEntry (entry);\r
Assert.IsTrue (entry.HostName == "google-public-dns-a.google.com");\r
}\r
\r
- void GetHostByNameCallback (IAsyncResult ar)\r
- {\r
- IPHostEntry h;\r
- h = Dns.EndGetHostByName (ar);\r
- SubTestValidIPHostEntry (h);\r
+ [Test]\r
+ public void AsyncGetHostByNameCallback ()\r
+ {\r
+ var evt = new ManualResetEvent (false);\r
+ Exception ex = null;\r
+ Dns.BeginGetHostByName (site1Name, new AsyncCallback ((IAsyncResult ar) =>\r
+ {\r
+ try {\r
+ IPHostEntry h;\r
+ h = Dns.EndGetHostByName (ar);\r
+ SubTestValidIPHostEntry (h);\r
+ } catch (Exception e) {\r
+ ex = e;\r
+ } finally {\r
+ evt.Set ();\r
+ }\r
+ }), null);\r
+\r
+ Assert.IsTrue (evt.WaitOne (TimeSpan.FromSeconds (60)), "Wait");\r
+ Assert.IsNull (ex, "Exception");\r
}\r
\r
[Test]\r
public void AsyncResolve ()\r
{\r
}\r
\r
[Test]\r
public void AsyncResolve ()\r
{\r
- IAsyncResult r;\r
- r = Dns.BeginResolve (site1Name, new AsyncCallback (ResolveCallback), null);\r
-\r
IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);\r
IPHostEntry entry = Dns.EndResolve (async);\r
SubTestValidIPHostEntry (entry);\r
IAsyncResult async = Dns.BeginResolve (site1Dot, null, null);\r
IPHostEntry entry = Dns.EndResolve (async);\r
SubTestValidIPHostEntry (entry);\r
Assert.AreEqual (site1Dot, ip.ToString ());\r
}\r
\r
Assert.AreEqual (site1Dot, ip.ToString ());\r
}\r
\r
- void ResolveCallback (IAsyncResult ar)\r
- {\r
- IPHostEntry h = Dns.EndResolve (ar);\r
- SubTestValidIPHostEntry (h);\r
+ [Test]\r
+ public void AsyncResolveCallback ()\r
+ {\r
+ var evt = new ManualResetEvent (false);\r
+ Exception ex = null;\r
+ Dns.BeginResolve (site1Name, new AsyncCallback ((IAsyncResult ar) =>\r
+ {\r
+ try {\r
+ IPHostEntry h = Dns.EndResolve (ar);\r
+ SubTestValidIPHostEntry (h);\r
+ } catch (Exception e) {\r
+ ex = e;\r
+ } finally {\r
+ evt.Set ();\r
+ }\r
+ }), null);\r
+\r
+ Assert.IsTrue (evt.WaitOne (TimeSpan.FromSeconds (60)), "Wait");\r
+ Assert.IsNull (ex, "Exception");\r
try {\r
Dns.BeginGetHostAddresses (\r
(string) null,\r
try {\r
Dns.BeginGetHostAddresses (\r
(string) null,\r
- new AsyncCallback (GetHostAddressesCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
try {\r
Dns.BeginGetHostAddresses (\r
"0.0.0.0",\r
try {\r
Dns.BeginGetHostAddresses (\r
"0.0.0.0",\r
- new AsyncCallback (GetHostAddressesCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#A1");\r
} catch (ArgumentException ex) {\r
null);\r
Assert.Fail ("#A1");\r
} catch (ArgumentException ex) {\r
try {\r
Dns.BeginGetHostAddresses (\r
"::0",\r
try {\r
Dns.BeginGetHostAddresses (\r
"::0",\r
- new AsyncCallback (GetHostAddressesCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#B1");\r
} catch (ArgumentException ex) {\r
null);\r
Assert.Fail ("#B1");\r
} catch (ArgumentException ex) {\r
- void GetHostAddressesCallback (IAsyncResult ar)\r
+ void ShouldntBeCalled (IAsyncResult ar)\r
- IPAddress [] addresses = Dns.EndGetHostAddresses (ar);\r
- Assert.IsNotNull (addresses);\r
+ Assert.Fail ("Should not be called");\r
{\r
try {\r
Dns.BeginResolve ((string) null,\r
{\r
try {\r
Dns.BeginResolve ((string) null,\r
- new AsyncCallback (ResolveCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
try {\r
Dns.BeginGetHostEntry (\r
(IPAddress) null,\r
try {\r
Dns.BeginGetHostEntry (\r
(IPAddress) null,\r
- new AsyncCallback (GetHostAddressesCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
try {\r
Dns.BeginGetHostEntry (\r
(string) null,\r
try {\r
Dns.BeginGetHostEntry (\r
(string) null,\r
- new AsyncCallback (GetHostAddressesCallback),\r
+ new AsyncCallback (ShouldntBeCalled),\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r
null);\r
Assert.Fail ("#1");\r
} catch (ArgumentNullException ex) {\r