[System] Rewrite Dns tests so that failures don't cause the process to crash.
[mono.git] / mcs / class / System / Test / System.Net / DnsTest.cs
index dcc82f61678777cc202e1b94417d4b7a82c0d114..e06ca4bf5d6ae6e16ca43665f144953ba1d2b1c0 100644 (file)
@@ -32,28 +32,37 @@ namespace MonoTests.System.Net
                [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
-               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
-                       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
@@ -61,10 +70,25 @@ namespace MonoTests.System.Net
                        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
                }\r
 \r
                [Test]\r
@@ -73,7 +97,7 @@ namespace MonoTests.System.Net
                        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
@@ -91,7 +115,7 @@ namespace MonoTests.System.Net
                        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
@@ -108,7 +132,7 @@ namespace MonoTests.System.Net
                        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
@@ -122,10 +146,9 @@ namespace MonoTests.System.Net
                        }\r
                }\r
 \r
-               void GetHostAddressesCallback (IAsyncResult ar)\r
+               void ShouldntBeCalled (IAsyncResult ar)\r
                {\r
-                       IPAddress [] addresses = Dns.EndGetHostAddresses (ar);\r
-                       Assert.IsNotNull (addresses);\r
+                       Assert.Fail ("Should not be called");\r
                }\r
 \r
                [Test]\r
@@ -317,7 +340,7 @@ namespace MonoTests.System.Net
                {\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
@@ -363,7 +386,7 @@ namespace MonoTests.System.Net
                        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
@@ -380,7 +403,7 @@ namespace MonoTests.System.Net
                        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