ActivationTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // MonoTests.Remoting.TcpCalls.cs
  3. //
  4. // Author: Lluis Sanchez Gual ([email protected])
  5. //
  6. // 2003 (C) Copyright, Ximian, Inc.
  7. //
  8. using System;
  9. using System.Runtime.Remoting;
  10. using System.Runtime.Remoting.Channels;
  11. using System.Runtime.Remoting.Channels.Tcp;
  12. using System.Runtime.Remoting.Channels.Http;
  13. using NUnit.Framework;
  14. namespace MonoTests.Remoting
  15. {
  16. [TestFixture]
  17. public class ActivationTests
  18. {
  19. ActivationServer server;
  20. TcpChannel tcp;
  21. HttpChannel http;
  22. [TestFixtureSetUp]
  23. public void Run()
  24. {
  25. try
  26. {
  27. tcp = new TcpChannel (0);
  28. http = new HttpChannel (0);
  29. ChannelServices.RegisterChannel (tcp);
  30. ChannelServices.RegisterChannel (http);
  31. AppDomain domain = AppDomain.CreateDomain ("testdomain_activation");
  32. server = (ActivationServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.ActivationServer");
  33. RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject1), "tcp://localhost:9433");
  34. RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject2), "http://localhost:9434");
  35. RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall1), "tcp://localhost:9433/wkoSingleCall1");
  36. RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton1), "tcp://localhost:9433/wkoSingleton1");
  37. RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall2), "http://localhost:9434/wkoSingleCall2");
  38. RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton2), "http://localhost:9434/wkoSingleton2");
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine (ex);
  43. }
  44. }
  45. [Test]
  46. public void TestCreateTcpCao ()
  47. {
  48. CaObject1 ca = new CaObject1 ();
  49. CaObject1 ca2 = new CaObject1 ();
  50. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  51. RunTestCreateCao (ca, ca2);
  52. }
  53. [Test]
  54. public void TestCreateHttpCao ()
  55. {
  56. CaObject2 ca = new CaObject2 ();
  57. CaObject2 ca2 = new CaObject2 ();
  58. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  59. RunTestCreateCao (ca, ca2);
  60. }
  61. public void RunTestCreateCao (BaseObject ca, BaseObject ca2)
  62. {
  63. Assert.AreEqual (0, ca.counter, "#1");
  64. ca.counter++;
  65. Assert.AreEqual (1, ca.counter, "#2");
  66. Assert.AreEqual (0, ca2.counter, "#3");
  67. ca2.counter++;
  68. Assert.AreEqual (1, ca2.counter, "#4");
  69. Assert.AreEqual (1, ca.counter, "#5");
  70. }
  71. [Test]
  72. public void TestCreateTcpWkoSingleCall ()
  73. {
  74. WkObjectSinglecall1 ca = new WkObjectSinglecall1 ();
  75. WkObjectSinglecall1 ca2 = new WkObjectSinglecall1 ();
  76. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  77. RunTestCreateWkoSingleCall (ca, ca2);
  78. }
  79. [Test]
  80. public void TestCreateTcpWkoSingleton ()
  81. {
  82. WkObjectSingleton1 ca = new WkObjectSingleton1 ();
  83. WkObjectSingleton1 ca2 = new WkObjectSingleton1 ();
  84. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  85. RunTestCreateWkoSingleton (ca, ca2);
  86. }
  87. [Test]
  88. public void TestCreateHttpWkoSingleCall ()
  89. {
  90. WkObjectSinglecall2 ca = new WkObjectSinglecall2 ();
  91. WkObjectSinglecall2 ca2 = new WkObjectSinglecall2 ();
  92. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  93. RunTestCreateWkoSingleCall (ca, ca2);
  94. }
  95. [Test]
  96. public void TestCreateHttpWkoSingleton ()
  97. {
  98. WkObjectSingleton2 ca = new WkObjectSingleton2 ();
  99. WkObjectSingleton2 ca2 = new WkObjectSingleton2 ();
  100. Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
  101. RunTestCreateWkoSingleton (ca, ca2);
  102. }
  103. public void RunTestCreateWkoSingleCall (BaseObject ca, BaseObject ca2)
  104. {
  105. Assert.AreEqual (0, ca.counter, "#1");
  106. ca.counter++;
  107. Assert.AreEqual (0, ca.counter, "#2");
  108. Assert.AreEqual (0, ca2.counter, "#3");
  109. ca2.counter++;
  110. Assert.AreEqual (0, ca2.counter, "#4");
  111. }
  112. public void RunTestCreateWkoSingleton (BaseObject ca, BaseObject ca2)
  113. {
  114. Assert.AreEqual (0, ca.counter, "#1");
  115. ca.counter++;
  116. Assert.AreEqual (1, ca.counter, "#2");
  117. ca.counter++;
  118. Assert.AreEqual (2, ca2.counter, "#3");
  119. ca2.counter++;
  120. Assert.AreEqual (3, ca2.counter, "#4");
  121. }
  122. [TestFixtureTearDown]
  123. public void End ()
  124. {
  125. ChannelServices.UnregisterChannel (tcp);
  126. ChannelServices.UnregisterChannel (http);
  127. server.Stop ();
  128. }
  129. }
  130. public class ActivationServer: MarshalByRefObject
  131. {
  132. TcpChannel tcp;
  133. HttpChannel http;
  134. public ActivationServer ()
  135. {
  136. tcp = new TcpChannel (9433);
  137. http = new HttpChannel (9434);
  138. ChannelServices.RegisterChannel (tcp);
  139. ChannelServices.RegisterChannel (http);
  140. RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject1));
  141. RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject2));
  142. RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall1), "wkoSingleCall1", WellKnownObjectMode.SingleCall);
  143. RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton1), "wkoSingleton1", WellKnownObjectMode.Singleton);
  144. RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall2), "wkoSingleCall2", WellKnownObjectMode.SingleCall);
  145. RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton2), "wkoSingleton2", WellKnownObjectMode.Singleton);
  146. }
  147. public void Stop ()
  148. {
  149. ChannelServices.UnregisterChannel (tcp);
  150. ChannelServices.UnregisterChannel (http);
  151. }
  152. }
  153. public class BaseObject: MarshalByRefObject
  154. {
  155. public int counter;
  156. public static int CreationCount;
  157. public BaseObject ()
  158. {
  159. CreationCount++;
  160. }
  161. }
  162. public class CaObject1: BaseObject
  163. {
  164. }
  165. public class CaObject2: BaseObject
  166. {
  167. }
  168. public class WkObjectSinglecall1: BaseObject
  169. {
  170. }
  171. public class WkObjectSingleton1: BaseObject
  172. {
  173. }
  174. public class WkObjectSinglecall2: BaseObject
  175. {
  176. }
  177. public class WkObjectSingleton2: BaseObject
  178. {
  179. }
  180. }