ServicePointManagerTest.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 : Assertion
  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. public void MaxServicePointManagers ()
  42. {
  43. AssertEquals ("#1", 0, ServicePointManager.MaxServicePoints);
  44. DoWebRequest (googleUri);
  45. Thread.Sleep (100);
  46. DoWebRequest (yahooUri);
  47. Thread.Sleep (100);
  48. DoWebRequest (apacheUri);
  49. Thread.Sleep (100);
  50. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri);
  51. //WriteServicePoint (sp);
  52. sp = ServicePointManager.FindServicePoint (yahooUri);
  53. //WriteServicePoint (sp);
  54. sp = ServicePointManager.FindServicePoint (apacheUri);
  55. //WriteServicePoint (sp);
  56. ServicePointManager.MaxServicePoints = 1;
  57. sp = ServicePointManager.FindServicePoint (googleUri);
  58. //WriteServicePoint (sp);
  59. sp = ServicePointManager.FindServicePoint (yahooUri);
  60. //WriteServicePoint (sp);
  61. sp = ServicePointManager.FindServicePoint (apacheUri);
  62. //WriteServicePoint (sp);
  63. GC.Collect ();
  64. // hmm... aparently ms.net still has the service points even
  65. // though I set it to a max of 1.
  66. // this should force an exception then...
  67. sp = ServicePointManager.FindServicePoint (new Uri ("http://www.microsoft.com"));
  68. //WriteServicePoint (sp);
  69. }
  70. [Test]
  71. public void FindServicePoint ()
  72. {
  73. ServicePointManager.MaxServicePoints = 0;
  74. ServicePoint sp = ServicePointManager.FindServicePoint (googleUri, new WebProxy (apacheUri));
  75. AssertEquals ("#1", apacheUri, sp.Address);
  76. AssertEquals ("#2", 2, sp.ConnectionLimit);
  77. AssertEquals ("#3", "http", sp.ConnectionName);
  78. }
  79. private void DoWebRequest (Uri uri)
  80. {
  81. WebRequest.Create (uri).GetResponse ().Close ();
  82. }
  83. /* Unused code for now, but might be useful later for debugging
  84. private void WriteServicePoint (ServicePoint sp)
  85. {
  86. Console.WriteLine ("\nAddress: " + sp.Address);
  87. Console.WriteLine ("ConnectionLimit: " + sp.ConnectionLimit);
  88. Console.WriteLine ("ConnectionName: " + sp.ConnectionName);
  89. Console.WriteLine ("CurrentConnections: " + sp.CurrentConnections);
  90. Console.WriteLine ("IdleSince: " + sp.IdleSince);
  91. Console.WriteLine ("MaxIdletime: " + sp.MaxIdleTime);
  92. Console.WriteLine ("ProtocolVersion: " + sp.ProtocolVersion);
  93. Console.WriteLine ("SupportsPipelining: " + sp.SupportsPipelining);
  94. }
  95. */
  96. }
  97. }