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