[LoongArch64] Part-5:add loongarch support in some files for LoongArch64. (#21769)
[mono-project.git] / mcs / class / System / Test / System.Net / ServicePointTest.cs
blob1a1a89e012336c095e08faa38852eb2061b60bfa
1 //
2 // ServicePointTest.cs - NUnit Test Cases for System.Net.ServicePoint
3 //
4 // Authors:
5 // Lawrence Pit (loz@cable.a2000.nl)
6 // Martin Willemoes Hansen (mwh@sysrq.dk)
7 //
8 // (C) 2003 Martin Willemoes Hansen
9 //
11 using NUnit.Framework;
12 using System;
13 using System.Collections;
14 using System.IO;
15 using System.Net;
16 using System.Reflection;
17 using System.Threading;
19 namespace MonoTests.System.Net
22 [TestFixture]
23 public class ServicePointTest
25 static private int max;
27 #if !FEATURE_NO_BSD_SOCKETS
28 [SetUp]
29 public void SaveMax () {
30 max = ServicePointManager.MaxServicePoints;
31 ServicePointManager.MaxServicePoints = 0;
34 [TearDown]
35 public void RestoreMax () {
36 ServicePointManager.MaxServicePoints = max;
38 #endif
40 [Test]
41 [Category ("NotWorking")]
42 public void All ()
44 ServicePoint p = ServicePointManager.FindServicePoint (new Uri ("mailto:xx@yyy.com"));
45 //WriteServicePoint ("A servicepoint that isn't really", p);
47 ServicePointManager.MaxServicePoints = 2;
48 ServicePoint exampleCom = ServicePointManager.FindServicePoint (new Uri ("http://www.example.com"));
49 try {
50 ServicePoint exampleOrg = ServicePointManager.FindServicePoint (new Uri ("http://www.example.org"));
51 Assert.Fail ("#1");
52 } catch (InvalidOperationException) { }
53 ServicePointManager.MaxServicePoints = 0;
55 //WriteServicePoint ("example before getting a webrequest", example);
57 HttpWebRequest req = (HttpWebRequest) WebRequest.Create ("http://www.example.com");
58 HttpWebResponse res = (HttpWebResponse) req.GetResponse ();
60 #if FOUND_SOME_OTHER_URL
61 // URL is no longer found, disabled the test until a more reliable URL is found :P
62 //WriteServicePoint ("example after getting a response", example);
63 ServicePoint example2 = ServicePointManager.FindServicePoint (new Uri ("http://www.example.com/dilbert.html"));
64 Assert.AreEqual (example, example2, "#equals");
65 res.Close ();
66 #endif
68 // in both instances property CurrentConnections is 0 according to ms.net.
69 // let's see what it says when we do async operations...
71 HttpWebRequest req2 = (HttpWebRequest) WebRequest.Create ("http://www.example.com");
72 req2.Method = "PUT";
73 IAsyncResult async = req2.BeginGetRequestStream (null, null);
74 //WriteServicePoint ("after async BeginGetRequestStream", example);
75 // CurrentConnections: 1
76 Stream stream2 = req2.EndGetRequestStream (async);
77 //WriteServicePoint ("after async EndGetRequestStream", example);
78 // CurrentConnections: 1
79 stream2.Close ();
81 req2 = (HttpWebRequest) WebRequest.Create ("http://www.example.com");
82 async = req2.BeginGetResponse (null, null);
83 //WriteServicePoint ("after async BeginGetResponse", example);
84 // CurrentConnections: 2
85 WebResponse res2 = req2.EndGetResponse (async);
86 //WriteServicePoint ("after async EndGetResponse", example);
87 // CurrentConnections: 0
88 // curious that after you get the webresponse object CurrentConnections is set to 0.
89 // you'd think that you'd still be connected until you close the webresponse..
90 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
91 res2.Close ();
93 ServicePoint sp2;
94 #if FOUND_SOME_OTHER_URL
95 // unless of course some buffering is taking place.. let's check
96 Uri uri2 = new Uri ("http://freedesktop.org/Software/pkgconfig/releases/pkgconfig-0.15.0.tar.gz");
97 sp2 = ServicePointManager.FindServicePoint (uri2);
98 req2 = (HttpWebRequest) WebRequest.Create (uri2);
99 async = req2.BeginGetResponse (null, null);
100 //WriteServicePoint ("Large file: after async BeginGetResponse", sp2);
101 // CurrentConnections: 1
102 res2 = req2.EndGetResponse (async);
103 //WriteServicePoint ("Large file: after async EndGetResponse", sp2);
104 // CurrentConnections: 1
105 // and so it shows
106 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
107 res2.Close ();
108 #endif
111 // what's the limit of the cache?
112 req2 = (HttpWebRequest) WebRequest.Create ("http://www.example.org/");
113 res2 = req2.GetResponse ();
114 sp2 = ServicePointManager.FindServicePoint (new Uri("http://www.example.org/"));
115 //WriteServicePoint ("example", sp2);
116 //Console.WriteLine ("ContentLength: " + res2.ContentLength);
117 // CurrentConnections: 1
118 res2.Close ();
119 // curious other effect: address is actually the full Uri of the previous request
120 // anyways, buffer is probably 4096 bytes
123 // try getting the stream to 5 web response objects
124 // while ConnectionLimit equals 2
126 [Test]
127 [Category ("NotWorking")]
128 public void ConnectionLimit ()
130 // the default is already 2, just in case it isn't..
131 ServicePointManager.DefaultConnectionLimit = 5;
133 Uri uri = new Uri ("http://www.example.com/");
134 ServicePoint sp = ServicePointManager.FindServicePoint (uri);
135 WebResponse [] res = new WebResponse [5];
136 for (int i = 0; i < 5; i++) {
137 //Console.WriteLine ("GOT1 : " + i);
138 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
139 //Console.WriteLine ("GOT2 : " + i);
140 res [i] = req.GetResponse ();
141 //WriteServicePoint ("after getting " + (i + 1) + " web response objects", sp);
144 for (int i = 0; i < 5; i++) {
145 Stream stream = res [i].GetResponseStream();
146 //Console.WriteLine ("Reading stream: " + i + " : " + stream);
147 int len = 0;
148 while (stream.ReadByte () != -1)
149 len++;
150 //Console.WriteLine ("Finished reading: " + len + " bytes");
153 for (int i = 0; i < 5; i++) {
154 res [i].Close ();
158 [Test]
159 [Category ("NotWorking")] // #A1 fails
160 public void EndPointBind ()
162 Uri uri = new Uri ("http://www.example.com/");
163 ServicePoint sp = ServicePointManager.FindServicePoint (uri);
165 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri);
167 bool called = false;
168 sp.BindIPEndPointDelegate = delegate {
169 Assert.IsTrue (!called);
170 called = true;
171 return null;
173 req.GetResponse ().Close ();
175 Assert.IsTrue (called, "#A1");
177 req = (HttpWebRequest) WebRequest.Create (uri);
178 called = false;
179 sp.BindIPEndPointDelegate = delegate(ServicePoint point, IPEndPoint remote, int times) {
180 Assert.IsTrue (times < 5);
181 called = true;
182 return new IPEndPoint(IPAddress.Parse("0.0.0.0"), 12345 + times);
184 req.GetResponse ().Close ();
186 Assert.IsTrue (called, "#A2");
189 [Test]
190 [Category ("RequiresBSDSockets")] // Tests internals, so it doesn't make sense to assert that PlatformNotSupportedExceptions are thrown.
191 public void DnsRefreshTimeout ()
193 const int dnsRefreshTimeout = 2000;
195 ServicePoint sp;
196 IPHostEntry host0, host1, host2;
197 Uri uri;
198 PropertyInfo hostEntryProperty;
200 ServicePointManager.DnsRefreshTimeout = dnsRefreshTimeout;
202 uri = new Uri ("http://localhost/");
203 sp = ServicePointManager.FindServicePoint (uri);
205 hostEntryProperty = typeof (ServicePoint).GetProperty ("HostEntry", BindingFlags.NonPublic | BindingFlags.Instance);
207 host0 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
208 host1 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
210 Assert.AreSame (host0, host1, "HostEntry should result in the same IPHostEntry object.");
212 #if !WASM
213 Thread.Sleep (dnsRefreshTimeout * 2);
214 host2 = hostEntryProperty.GetValue (sp, null) as IPHostEntry;
216 Assert.AreNotSame(host0, host2, "HostEntry should result in a new IPHostEntry " +
217 "object when DnsRefreshTimeout is reached.");
218 #endif
221 // Debug code not used now, but could be useful later
223 private void WriteServicePoint (string label, ServicePoint sp)
225 Console.WriteLine ("\n" + label);
226 Console.WriteLine ("Address: " + sp.Address);
227 Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
228 Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
229 Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
230 Console.WriteLine ("IdleSince: " + sp.IdleSince);
231 Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
232 Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
233 Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);