ServicePointManagerTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //
  2. // ServicePointManagerTest.cs - NUnit Test Cases for System.Net.ServicePointManager
  3. //
  4. // Authors:
  5. // Lawrence Pit ([email protected])
  6. // Martin Willemoes Hansen ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. using NUnit.Framework;
  11. using System;
  12. using System.Collections;
  13. using System.IO;
  14. using System.Net;
  15. using System.Threading;
  16. namespace MonoTests.System.Net
  17. {
  18. [TestFixture]
  19. public class ServicePointManagerTest
  20. {
  21. private Uri googleUri;
  22. private Uri yahooUri;
  23. private Uri apacheUri;
  24. private int maxIdle;
  25. [SetUp]
  26. public void GetReady ()
  27. {
  28. maxIdle = ServicePointManager.MaxServicePointIdleTime;
  29. ServicePointManager.MaxServicePointIdleTime = 10;
  30. googleUri = new Uri ("http://www.google.com");
  31. yahooUri = new Uri ("http://www.yahoo.com");
  32. apacheUri = new Uri ("http://www.apache.org");
  33. }
  34. [TearDown]
  35. public void Finish ()
  36. {
  37. ServicePointManager.MaxServicePointIdleTime = maxIdle;
  38. }
  39. [Test, ExpectedException (typeof (InvalidOperationException))]
  40. [Category ("InetAccess")]
  41. #if TARGET_JVM
  42. [Ignore ("Unsupported property - ServicePointManager.MaxServicePoints")]
  43. #endif
  44. public void MaxServicePointManagers ()
  45. {
  46. Assert.AreEqual (0, ServicePointManager.MaxServicePoints, "#1");
  47. DoWebRequest (googleUri);
  48. Thread.Sleep (100);
  49. DoWebRequest (yahooUri);
  50. Thread.Sleep (100);
  51. DoWebRequest (apacheUri);
  52. Thread.Sleep (100);
  53. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  54. //WriteServicePoint (sp);
  55. sp = ServicePointManager.FindServicePoint (yahooUri);
  56. //WriteServicePoint (sp);
  57. sp = ServicePointManager.FindServicePoint (apacheUri);
  58. //WriteServicePoint (sp);
  59. ServicePointManager.MaxServicePoints = 1;
  60. sp = ServicePointManager.FindServicePoint (googleUri);
  61. //WriteServicePoint (sp);
  62. sp = ServicePointManager.FindServicePoint (yahooUri);
  63. //WriteServicePoint (sp);
  64. sp = ServicePointManager.FindServicePoint (apacheUri);
  65. //WriteServicePoint (sp);
  66. GC.Collect ();
  67. // hmm... aparently ms.net still has the service points even
  68. // though I set it to a max of 1.
  69. // this should force an exception then...
  70. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  71. //WriteServicePoint (sp);
  72. }
  73. [Test]
  74. public void FindServicePoint ()
  75. {
  76. ServicePointManager.MaxServicePoints = 0;
  77. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  78. Assert.AreEqual (apacheUri, sp.Address, "#1");
  79. #if NET_2_1
  80. Assert.AreEqual (10, sp.ConnectionLimit, "#2");
  81. #else
  82. Assert.AreEqual (2, sp.ConnectionLimit, "#2");
  83. #endif
  84. Assert.AreEqual ("http", sp.ConnectionName, "#3");
  85. }
  86. private void DoWebRequest (Uri uri)
  87. {
  88. WebRequest.Create (uri).GetResponse ().Close ();
  89. }
  90. /* Unused code for now, but might be useful later for debugging
  91. private void WriteServicePoint (ServicePoint sp)
  92. {
  93. Console.WriteLine ("\nAddress: " + sp.Address);
  94. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  95. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  96. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  97. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  98. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  99. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  100. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  101. }
  102. */
  103. }
  104. }