Merge pull request #1659 from alexanderkyte/stringbuilder-referencesource
[mono.git] / mcs / class / System / Test / System.Net / ServicePointTest.cs
index 7e8eaeac8fff743f8d7677f3a74d83498d873ce3..bb12a4c28f3eb93112b9ac5e1952a68b9c80fa4a 100644 (file)
@@ -21,128 +21,186 @@ namespace MonoTests.System.Net
 [TestFixture]\r
 public class ServicePointTest\r
 {\r
+       static private int max;\r
+       [SetUp]\r
+       public void SaveMax () {\r
+               max = ServicePointManager.MaxServicePoints;\r
+               ServicePointManager.MaxServicePoints = 0;\r
+       }\r
+\r
+       [TearDown]\r
+       public void RestoreMax () {\r
+               ServicePointManager.MaxServicePoints = max;\r
+       }\r
+\r
         [Test]\r
+               [Category ("InetAccess")]\r
         public void All ()\r
         {\r
-               try {\r
-                       ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
-                       WriteServicePoint ("A servicepoint that isn't really", p);                      \r
-                       \r
-                       ServicePointManager.MaxServicePoints = 2;\r
-                       ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
-                       try {                   \r
-                               ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
-                               Assertion.Fail ("#1");\r
-                       } catch (InvalidOperationException) { }\r
-                       ServicePointManager.MaxServicePoints = 0;\r
-                       \r
-                       WriteServicePoint ("google before getting a webrequest", google);\r
-                       \r
-                       HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
-                       HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
-                       \r
-                       WriteServicePoint ("google after getting a response", google);\r
-                       ServicePoint google2 = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com/dilbert.html"));\r
-                       Assertion.AssertEquals ("#equals", google, google2);\r
-                       res.Close ();\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
-                       \r
-                       HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
-                       req2.Method = "PUT";\r
-                       IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
-                       WriteServicePoint ("after async BeginGetRequestStream", google);\r
-                       // CurrentConnections: 1\r
-                       Stream stream2 = req2.EndGetRequestStream (async);\r
-                       WriteServicePoint ("after async EndGetRequestStream", google);\r
-                       // CurrentConnections: 1\r
-                       stream2.Close ();\r
-                       \r
-                       req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
-                       async = req2.BeginGetResponse (null, null);\r
-                       WriteServicePoint ("after async BeginGetResponse", google);\r
-                       // CurrentConnections: 2\r
-                       WebResponse res2 = req2.EndGetResponse (async);\r
-                       WriteServicePoint ("after async EndGetResponse", google);\r
-                       // CurrentConnections: 0                        \r
-                       // curious that after you get the webresponse object CurrentConnections is set to 0.\r
-                       // you'd think that you'd still be connected until you close the webresponse..\r
-                       Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
-                       res2.Close ();\r
-                       \r
-                       \r
-                       // unless of course some buffering is taking place.. let's check\r
-                       Uri uri2 = new Uri ("http://www.apache.org/dist/httpd/httpd-2.0.36.tar.gz");\r
-                       ServicePoint 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
-                       // CurrentConnections: 1\r
-                       res2 = req2.EndGetResponse (async);\r
-                       WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
-                       // CurrentConnections: 1\r
-                       // and so it shows\r
-                       Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
-                       res2.Close ();\r
-                       \r
-                       \r
-                       // what's the limit of the cache?\r
-                       req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
-                       res2 = req2.GetResponse ();\r
-                       sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
-                       WriteServicePoint ("apache", sp2);\r
-                       Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
-                       // CurrentConnections: 1\r
-                       res2.Close ();\r
-                       // curious other effect: address is actually the full Uri of the previous request\r
-                       // anyways, buffer is probably 4096 bytes\r
-\r
-                       \r
-               } catch (WebException e) {\r
-                       Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);\r
-               }\r
+               ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));\r
+               //WriteServicePoint ("A servicepoint that isn't really", p);                    \r
+               \r
+               ServicePointManager.MaxServicePoints = 2;\r
+               ServicePoint google = ServicePointManager.FindServicePoint (new Uri ("http://www.google.com"));\r
+               try {                   \r
+                       ServicePoint slashdot = ServicePointManager.FindServicePoint (new Uri ("http://www.slashdot.org"));\r
+                       Assert.Fail ("#1");\r
+               } catch (InvalidOperationException) { }\r
+               ServicePointManager.MaxServicePoints = 0;\r
+               \r
+               //WriteServicePoint ("google before getting a webrequest", google);\r
+               \r
+               HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
+               HttpWebResponse res = (HttpWebResponse) req.GetResponse ();                     \r
+               \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
+               \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
+               \r
+               HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
+               req2.Method = "PUT";\r
+               IAsyncResult async = req2.BeginGetRequestStream (null, null);\r
+               //WriteServicePoint ("after async BeginGetRequestStream", google);\r
+               // CurrentConnections: 1\r
+               Stream stream2 = req2.EndGetRequestStream (async);\r
+               //WriteServicePoint ("after async EndGetRequestStream", google);\r
+               // CurrentConnections: 1\r
+               stream2.Close ();\r
+               \r
+               req2 = (HttpWebRequest) WebRequest.Create ("http://www.google.com");\r
+               async = req2.BeginGetResponse (null, null);\r
+               //WriteServicePoint ("after async BeginGetResponse", google);\r
+               // CurrentConnections: 2\r
+               WebResponse res2 = req2.EndGetResponse (async);\r
+               //WriteServicePoint ("after async EndGetResponse", google);\r
+               // CurrentConnections: 0                        \r
+               // curious that after you get the webresponse object CurrentConnections is set to 0.\r
+               // you'd think that you'd still be connected until you close the webresponse..\r
+               //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
+               res2.Close ();\r
+               \r
+               \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
+               req2 = (HttpWebRequest) WebRequest.Create (uri2);\r
+               async = req2.BeginGetResponse (null, null);\r
+               //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);\r
+               // CurrentConnections: 1\r
+               res2 = req2.EndGetResponse (async);\r
+               //WriteServicePoint ("Large file: after async EndGetResponse", sp2);\r
+               // CurrentConnections: 1\r
+               // and so it shows\r
+               //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
+               res2.Close ();\r
+               \r
+               \r
+               // what's the limit of the cache?\r
+               req2 = (HttpWebRequest) WebRequest.Create ("http://www.apache.org/");\r
+               res2 = req2.GetResponse ();\r
+               sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.apache.org/"));\r
+               //WriteServicePoint ("apache", sp2);\r
+               //Console.WriteLine ("ContentLength: " + res2.ContentLength);\r
+               // CurrentConnections: 1\r
+               res2.Close ();\r
+               // curious other effect: address is actually the full Uri of the previous request\r
+               // anyways, buffer is probably 4096 bytes\r
        }\r
 \r
        // try getting the stream to 5 web response objects     \r
        // while ConnectionLimit equals 2\r
-       /*\r
+\r
        [Test]\r
+       [Category ("InetAccess")]\r
        public void ConnectionLimit ()\r
        {               \r
-               try {\r
-                       // the default is already 2, just in case it isn't..\r
-                       ServicePointManager.DefaultConnectionLimit = 2;\r
-                       \r
-                       Uri uri = new Uri ("http://www.apache.org/dist/httpd/httpd-2.0.36.tar.gz");\r
-                       ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
-                       WebResponse [] res = new WebResponse [5];\r
-                       for (int i = 0; i < 5; i++) {\r
-                               Console.WriteLine ("GOT1 : " + i);\r
-                               HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
-                               Console.WriteLine ("GOT2 : " + i);\r
-                               res [i] = req.GetResponse ();\r
-                               WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
-                       }\r
-                       \r
-                       for (int i = 0; i < 5; i++) {\r
-                               Stream stream = res [i].GetResponseStream();\r
-                               Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
-                               int len = 0;\r
-                               while (stream.ReadByte () != -1)\r
-                                       len++;\r
-                               Console.WriteLine ("Finished reading: " + len + " bytes");\r
-                       }\r
-                       \r
-                       for (int i = 0; i < 5; i++) {\r
-                               res [i].Close ();\r
-                       }\r
-               } catch (WebException e) {\r
-                       Console.WriteLine("\nThe following Exception was raised : {0}", e.Message);\r
-               }               \r
+               // the default is already 2, just in case it isn't..\r
+               ServicePointManager.DefaultConnectionLimit = 5;\r
+               \r
+               Uri uri = new Uri ("http://www.go-mono.com/");\r
+               ServicePoint sp = ServicePointManager.FindServicePoint (uri);                   \r
+               WebResponse [] res = new WebResponse [5];\r
+               for (int i = 0; i < 5; i++) {\r
+                       //Console.WriteLine ("GOT1 : " + i);\r
+                       HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
+                       //Console.WriteLine ("GOT2 : " + i);\r
+                       res [i] = req.GetResponse ();\r
+                       //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);\r
+               }\r
+               \r
+               for (int i = 0; i < 5; i++) {\r
+                       Stream stream = res [i].GetResponseStream();\r
+                       //Console.WriteLine ("Reading stream: " + i + " : " + stream);\r
+                       int len = 0;\r
+                       while (stream.ReadByte () != -1)\r
+                               len++;\r
+                       //Console.WriteLine ("Finished reading: " + len + " bytes");\r
+               }\r
+               \r
+               for (int i = 0; i < 5; i++) {\r
+                       res [i].Close ();\r
+               }\r
+       }\r
+\r
+       [Test]\r
+       [Category ("InetAccess")]\r
+       public void EndPointBind ()\r
+       {\r
+               Uri uri = new Uri ("http://www.go-mono.com/");\r
+               ServicePoint sp = ServicePointManager.FindServicePoint (uri);\r
+\r
+               HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);\r
+\r
+               bool called = false;\r
+               sp.BindIPEndPointDelegate = delegate {\r
+                       Assert.IsTrue (!called);\r
+                       called = true;\r
+                       return null;\r
+               };\r
+               req.GetResponse ().Close ();\r
+\r
+               Assert.IsTrue (called);\r
+\r
+               req = (HttpWebRequest) WebRequest.Create (uri);\r
+               called = false;\r
+               sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {\r
+                       Assert.IsTrue (times < 5);\r
+                       called = true;\r
+                       return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);\r
+               };\r
+               req.GetResponse ().Close ();\r
+\r
+               Assert.IsTrue (called);\r
+       }\r
+\r
+       public static void GetRequestStreamCallback (IAsyncResult asynchronousResult)\r
+       {\r
        }\r
-       */\r
-       \r
+\r
+       [Test] //Covers #19823\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
+// Debug code not used now, but could be useful later\r
+/*\r
        private void WriteServicePoint (string label, ServicePoint sp)\r
        {\r
                Console.WriteLine ("\n" + label);\r
@@ -155,7 +213,7 @@ public class ServicePointTest
                Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);\r
                Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);             \r
        }\r
+*/\r
 }\r
-\r
 }\r
 \r