[System*] Throw a PlatformNotSupported exception when using the managed networking...
[mono.git] / mcs / class / System / Test / System.Net / ServicePointTest.cs
index da4a0f5dd4df0e2ec06134d0910c127851a7f4a1..6171a2fd25587d85a535b496111aa114628536e0 100644 (file)
@@ -13,6 +13,7 @@ using System;
 using System.Collections;\r
 using System.IO;\r
 using System.Net;\r
+using System.Reflection;\r
 using System.Threading;\r
 \r
 namespace MonoTests.System.Net\r
@@ -22,6 +23,8 @@ namespace MonoTests.System.Net
 public class ServicePointTest\r
 {\r
        static private int max;\r
+\r
+#if !FEATURE_NO_BSD_SOCKETS\r
        [SetUp]\r
        public void SaveMax () {\r
                max = ServicePointManager.MaxServicePoints;\r
@@ -32,6 +35,7 @@ public class ServicePointTest
        public void RestoreMax () {\r
                ServicePointManager.MaxServicePoints = max;\r
        }\r
+#endif\r
 \r
         [Test]\r
                [Category ("InetAccess")]\r
@@ -53,10 +57,13 @@ public class ServicePointTest
                HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
                HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
                \r
+#if FOUND_SOME_OTHER_URL\r
+               // URL is no longer found, disabled the test until a more reliable URL is found :P\r
                //WriteServicePoint ("google after getting a response", google);\r
                ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
                Assert.AreEqual (google, google2, "#equals");\r
                res.Close ();\r
+#endif\r
                \r
                // in both instances property CurrentConnections is 0 according to ms.net.\r
                // let's see what it says when we do async operations...\r
@@ -83,10 +90,11 @@ public class ServicePointTest
                //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
                res2.Close ();\r
                \r
-               \r
+               ServicePoint sp2;\r
+#if FOUND_SOME_OTHER_URL\r
                // unless of course some buffering is taking place.. let's check\r
                Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");\r
-               ServicePoint sp2 = ServicePointManager.FindServicePoint (uri2);\r
+               sp2 = ServicePointManager.FindServicePoint (uri2);\r
                req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
                async = req2.BeginGetResponse (null, null);\r
                //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
@@ -97,6 +105,7 @@ public class ServicePointTest
                // and so it shows\r
                //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
                res2.Close ();\r
+#endif\r
                \r
                \r
                // what's the limit of the cache?\r
@@ -148,6 +157,7 @@ public class ServicePointTest
 \r
        [Test]\r
        [Category ("InetAccess")]\r
+       [Category ("AndroidNotWorking")] // #A1 fails\r
        public void EndPointBind ()\r
        {\r
                Uri uri = new Uri ("http://www.go-mono.com/");\r
@@ -163,7 +173,7 @@ public class ServicePointTest
                };\r
                req.GetResponse ().Close ();\r
 \r
-               Assert.IsTrue (called);\r
+               Assert.IsTrue (called, "#A1");\r
 \r
                req = (HttpWebRequest) WebRequest.Create (uri);\r
                called = false;\r
@@ -174,7 +184,64 @@ public class ServicePointTest
                };\r
                req.GetResponse ().Close ();\r
 \r
-               Assert.IsTrue (called);\r
+               Assert.IsTrue (called, "#A2");\r
+       }\r
+\r
+       public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)\r
+       {\r
+       }\r
+\r
+       [Test] //Covers #19823\r
+#if FEATURE_NO_BSD_SOCKETS\r
+       // This test uses HttpWebRequest\r
+       [ExpectedException (typeof (PlatformNotSupportedException))]\r
+#endif\r
+       public void CloseConnectionGroupConcurency ()\r
+       {\r
+               // Try with multiple service points\r
+               for (var i = 0; i < 10; i++) {\r
+                       Uri targetUri = new Uri ("http://" + i + ".mono-project.com");\r
+                       var req = (HttpWebRequest) HttpWebRequest.Create (targetUri);\r
+                       req.ContentType = "application/x-www-form-urlencoded";\r
+                       req.Method = "POST";\r
+                       req.ConnectionGroupName = "" + i;\r
+                       req.ServicePoint.MaxIdleTime = 1;\r
+\r
+                       req.BeginGetRequestStream (new AsyncCallback (GetRequestStreamCallback), req);\r
+                       Thread.Sleep (1);\r
+                       req.ServicePoint.CloseConnectionGroup (req.ConnectionGroupName);\r
+               }\r
+       }\r
+\r
+\r
+       [Test]\r
+       [Category ("RequiresBSDSockets")] // Tests internals, so it doesn't make sense to assert that PlatformNotSupportedExceptions are thrown.\r
+       public void DnsRefreshTimeout ()\r
+       {\r
+               const int dnsRefreshTimeout = 2000;\r
+\r
+               ServicePoint sp;\r
+               IPHostEntry host0, host1, host2;\r
+               Uri uri;\r
+               PropertyInfo hostEntryProperty;\r
+\r
+               ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;\r
+\r
+               uri = new Uri ("http://localhost/");\r
+               sp = ServicePointManager.FindServicePoint (uri);\r
+\r
+               hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);\r
+\r
+               host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;\r
+               host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;\r
+\r
+               Assert.AreSame (host0, host1, "HostEntry should result in the same IPHostEntry object.");\r
+\r
+               Thread.Sleep (dnsRefreshTimeout * 2);\r
+               host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;\r
+\r
+               Assert.AreNotSame(host0, host2, "HostEntry should result in a new IPHostEntry " +\r
+                               "object when DnsRefreshTimeout is reached.");\r
        }\r
 \r
 // Debug code not used now, but could be useful later\r