Merge pull request #5444 from hifi/fix-tds-inputoutput
[mono.git] / mcs / class / System.Runtime.Remoting / Test / ActivationTests.cs
1 //
2 // MonoTests.Remoting.TcpCalls.cs
3 //
4 // Author: Lluis Sanchez Gual (lluis@ximian.com)
5 //
6 // 2003 (C) Copyright, Ximian, Inc.
7 //
8
9 using System;
10 using System.Collections;
11 using System.Runtime.Remoting;
12 using System.Runtime.Remoting.Channels;
13 using System.Runtime.Remoting.Channels.Tcp;
14 using System.Runtime.Remoting.Channels.Http;
15 using NUnit.Framework;
16
17 using MonoTests.Helpers;
18
19 namespace MonoTests.Remoting
20 {
21         [TestFixture]
22         public class ActivationTests
23         {
24                 ActivationServer server;
25                 TcpChannel tcp;
26                 HttpClientChannel http;
27                         
28                 [TestFixtureSetUp]
29                 public void Run()
30                 {
31                         try
32                         {
33                                 tcp =  new TcpChannel (0);
34                                 
35                                 Hashtable options = new Hashtable ();
36                                 options ["timeout"] = 10000; // 10s
37                                 http = new HttpClientChannel (options, null);
38                         
39                                 ChannelServices.RegisterChannel (tcp);
40                                 ChannelServices.RegisterChannel (http);
41                         
42                                 AppDomain domain = BaseCallTest.CreateDomain ("testdomain_activation");
43                                 server = (ActivationServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.ActivationServer");
44                                 
45                                 var tcpUrlPrefix = $"tcp://localhost:{server.TcpPort}";
46                                 var httpUrlPrefix = $"http://localhost:{server.HttpPort}";
47                                 RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject1), tcpUrlPrefix);
48                                 RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject2), httpUrlPrefix);
49                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall1), tcpUrlPrefix + "/wkoSingleCall1");
50                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton1), tcpUrlPrefix + "/wkoSingleton1");
51                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall2), httpUrlPrefix + "/wkoSingleCall2");
52                                 RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSingleton2), httpUrlPrefix + "/wkoSingleton2");
53                         }
54                         catch (Exception ex)
55                         {
56                                 Console.WriteLine (ex);
57                         }
58                 }
59                 
60                 [Test]
61                 public void TestCreateTcpCao ()
62                 {
63                         CaObject1 ca = new CaObject1 ();
64                         CaObject1 ca2 = new CaObject1 ();
65                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
66                         RunTestCreateCao (ca, ca2);
67                 }
68                 
69                 [Test]
70                 public void TestCreateHttpCao ()
71                 {
72                         CaObject2 ca = new CaObject2 ();
73                         CaObject2 ca2 = new CaObject2 ();
74                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
75                         RunTestCreateCao (ca, ca2);
76                 }
77                 
78                 public void RunTestCreateCao (BaseObject ca, BaseObject ca2)
79                 {
80                         Assert.AreEqual (0, ca.counter, "#1");
81                         
82                         ca.counter++;
83                         Assert.AreEqual (1, ca.counter, "#2");
84                         
85                         Assert.AreEqual (0, ca2.counter, "#3");
86                         
87                         ca2.counter++;
88                         Assert.AreEqual (1, ca2.counter, "#4");
89                         
90                         Assert.AreEqual (1, ca.counter, "#5");
91                 }
92
93                 [Test]
94                 public void TestCreateTcpWkoSingleCall ()
95                 {
96                         WkObjectSinglecall1 ca = new WkObjectSinglecall1 ();
97                         WkObjectSinglecall1 ca2 = new WkObjectSinglecall1 ();
98                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
99                         RunTestCreateWkoSingleCall (ca, ca2);
100                 }
101                 
102                 [Test]
103                 public void TestCreateTcpWkoSingleton ()
104                 {
105                         WkObjectSingleton1 ca = new WkObjectSingleton1 ();
106                         WkObjectSingleton1 ca2 = new WkObjectSingleton1 ();
107                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
108                         RunTestCreateWkoSingleton (ca, ca2);
109                 }
110
111                 [Test]
112                 [Category ("NotWorking")]
113                 public void TestCreateHttpWkoSingleCall ()
114                 {
115                         WkObjectSinglecall2 ca = new WkObjectSinglecall2 ();
116                         WkObjectSinglecall2 ca2 = new WkObjectSinglecall2 ();
117                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
118                         RunTestCreateWkoSingleCall (ca, ca2);
119                 }
120                 
121                 [Test]
122                 [Category ("NotWorking")]
123                 public void TestCreateHttpWkoSingleton ()
124                 {
125                         WkObjectSingleton2 ca = new WkObjectSingleton2 ();
126                         WkObjectSingleton2 ca2 = new WkObjectSingleton2 ();
127                         Assert.IsTrue (BaseObject.CreationCount == 0, "Objects created locally");
128                         RunTestCreateWkoSingleton (ca, ca2);
129                 }
130                 
131                 public void RunTestCreateWkoSingleCall (BaseObject ca, BaseObject ca2)
132                 {
133                         Assert.AreEqual (0, ca.counter, "#1");
134                         ca.counter++;
135                         Assert.AreEqual (0, ca.counter, "#2");
136
137                         Assert.AreEqual (0, ca2.counter, "#3");
138                         ca2.counter++;
139                         Assert.AreEqual (0, ca2.counter, "#4");
140                 }
141
142                 public void RunTestCreateWkoSingleton (BaseObject ca, BaseObject ca2)
143                 {
144                         Assert.AreEqual (0, ca.counter, "#1");
145                         ca.counter++;
146                         Assert.AreEqual (1, ca.counter, "#2");
147                         ca.counter++;
148                         Assert.AreEqual (2, ca2.counter, "#3");
149                         ca2.counter++;
150                         Assert.AreEqual (3, ca2.counter, "#4");
151                 }
152
153                 [TestFixtureTearDown]
154                 public void End ()
155                 {
156                         ChannelServices.UnregisterChannel (tcp);
157                         ChannelServices.UnregisterChannel (http);
158                         if (server != null)
159                                 server.Stop ();
160                 }
161         }
162         
163         public class ActivationServer: MarshalByRefObject
164         {
165                 TcpChannel tcp;
166                 HttpChannel http;
167                 
168                 public ActivationServer ()
169                 {
170                         TcpPort = NetworkHelpers.FindFreePort ();
171                         HttpPort = NetworkHelpers.FindFreePort ();
172                         tcp =  new TcpChannel (TcpPort);
173                         http =  new HttpChannel (HttpPort);
174                         
175                         ChannelServices.RegisterChannel (tcp);
176                         ChannelServices.RegisterChannel (http);
177                         
178                         RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject1));
179                         RemotingConfiguration.RegisterActivatedServiceType (typeof(CaObject2));
180                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall1), "wkoSingleCall1", WellKnownObjectMode.SingleCall);
181                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton1), "wkoSingleton1", WellKnownObjectMode.Singleton);
182                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSinglecall2), "wkoSingleCall2", WellKnownObjectMode.SingleCall);
183                         RemotingConfiguration.RegisterWellKnownServiceType (typeof(WkObjectSingleton2), "wkoSingleton2", WellKnownObjectMode.Singleton);
184                 }
185                 
186                 public void Stop ()
187                 {
188                         ChannelServices.UnregisterChannel (tcp);
189                         ChannelServices.UnregisterChannel (http);
190                 }
191
192                 public int TcpPort { get; private set; }
193                 public int HttpPort { get; private set; }
194         }
195         
196         public class BaseObject: MarshalByRefObject
197         {
198                 public int counter;
199                 public static int CreationCount;
200                 
201                 public BaseObject ()
202                 {
203                         CreationCount++;
204                 }
205         }
206         
207         public class CaObject1: BaseObject
208         {
209         }
210         
211         public class CaObject2: BaseObject
212         {
213         }
214         
215         public class WkObjectSinglecall1: BaseObject
216         {
217         }
218         
219         public class WkObjectSingleton1: BaseObject
220         {
221         }
222         
223         public class WkObjectSinglecall2: BaseObject
224         {
225         }
226         
227         public class WkObjectSingleton2: BaseObject
228         {
229         }
230 }