Skip to content

Commit da605d8

Browse files
committed
Remove DNS lookups of the local hostname in tests (#18059)
This allows us to workaround issues we currently have in the macOS bot network where DNS lookup of the hostname fails. (cherry picked from commit 269d509)
1 parent a6e867f commit da605d8

File tree

7 files changed

+90
-46
lines changed

7 files changed

+90
-46
lines changed

mcs/class/Mono.Messaging.RabbitMQ/Mono.Messaging.RabbitMQ/RabbitMQMessagingProvider.cs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,33 +43,16 @@ namespace Mono.Messaging.RabbitMQ {
4343
public class RabbitMQMessagingProvider : IMessagingProvider {
4444

4545
private int txCounter = 0;
46-
private readonly uint localIp;
46+
private readonly Guid localId;
4747
private readonly MessagingContextPool contextPool;
4848

4949
public RabbitMQMessagingProvider ()
5050
{
51-
localIp = GetLocalIP ();
51+
localId = Guid.NewGuid ();
5252
contextPool = new MessagingContextPool (new MessageFactory (this),
5353
CreateConnection);
5454
}
5555

56-
private static uint GetLocalIP ()
57-
{
58-
String strHostName = Dns.GetHostName ();
59-
IPHostEntry ipEntry = Dns.GetHostByName (strHostName);
60-
foreach (IPAddress ip in ipEntry.AddressList) {
61-
if (AddressFamily.InterNetwork == ip.AddressFamily) {
62-
byte[] addr = ip.GetAddressBytes ();
63-
uint localIP = 0;
64-
for (int i = 0; i < 4 && i < addr.Length; i++) {
65-
localIP += (uint) (addr[i] << 8 * (3 - i));
66-
}
67-
return localIP;
68-
}
69-
}
70-
return 0;
71-
}
72-
7356
public IMessage CreateMessage ()
7457
{
7558
return new MessageBase ();
@@ -78,7 +61,7 @@ public IMessage CreateMessage ()
7861
public IMessageQueueTransaction CreateMessageQueueTransaction ()
7962
{
8063
Interlocked.Increment (ref txCounter);
81-
string txId = localIp.ToString () + txCounter.ToString ();
64+
string txId = localId.ToString () + "_" + txCounter.ToString ();
8265

8366
return new RabbitMQMessageQueueTransaction (txId, contextPool);
8467
}

mcs/class/System.Runtime.Remoting/Test/ActivationTests.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ public void Run()
3030
{
3131
try
3232
{
33-
tcp = new TcpChannel (0);
33+
Hashtable tcpOptions = new Hashtable ();
34+
tcpOptions ["port"] = 0;
35+
tcpOptions ["bindTo"] = "127.0.0.1";
36+
tcp = new TcpChannel (tcpOptions, null, null);
3437

3538
Hashtable options = new Hashtable ();
3639
options ["timeout"] = 10000; // 10s
@@ -42,8 +45,8 @@ public void Run()
4245
AppDomain domain = BaseCallTest.CreateDomain ("testdomain_activation");
4346
server = (ActivationServer) domain.CreateInstanceAndUnwrap(GetType().Assembly.FullName,"MonoTests.Remoting.ActivationServer");
4447

45-
var tcpUrlPrefix = $"tcp://localhost:{server.TcpPort}";
46-
var httpUrlPrefix = $"http://localhost:{server.HttpPort}";
48+
var tcpUrlPrefix = $"tcp://127.0.0.1:{server.TcpPort}";
49+
var httpUrlPrefix = $"http://127.0.0.1:{server.HttpPort}";
4750
RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject1), tcpUrlPrefix);
4851
RemotingConfiguration.RegisterActivatedClientType (typeof(CaObject2), httpUrlPrefix);
4952
RemotingConfiguration.RegisterWellKnownClientType (typeof(WkObjectSinglecall1), tcpUrlPrefix + "/wkoSingleCall1");
@@ -169,8 +172,14 @@ public ActivationServer ()
169172
{
170173
TcpPort = NetworkHelpers.FindFreePort ();
171174
HttpPort = NetworkHelpers.FindFreePort ();
172-
tcp = new TcpChannel (TcpPort);
173-
http = new HttpChannel (HttpPort);
175+
IDictionary tcpProps = new Hashtable ();
176+
IDictionary httpProps = new Hashtable ();
177+
tcpProps ["port"] = TcpPort;
178+
tcpProps ["bindTo"] = "127.0.0.1";
179+
httpProps ["port"] = HttpPort;
180+
httpProps ["bindTo"] = "127.0.0.1";
181+
tcp = new TcpChannel (tcpProps, null, null);
182+
http = new HttpChannel (httpProps, null, null);
174183

175184
ChannelServices.RegisterChannel (tcp);
176185
ChannelServices.RegisterChannel (http);

mcs/class/System.Runtime.Remoting/Test/GenericTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ public void TestTcpChannel ()
172172
IDictionary props = new Hashtable ();
173173
props ["name"] = Guid.NewGuid ().ToString("N");
174174
props ["port"] = port;
175+
props ["bindTo"] = "127.0.0.1";
175176
TcpChannel chan = new TcpChannel (props, null, null);
176177
ChannelServices.RegisterChannel (chan);
177178

mcs/class/System.Runtime.Remoting/Test/HttpBugTests.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,15 @@ public class Bug324362
2020
[Ignore ("This test somehow keeps http channel registered and then blocks any further http tests working. This also happens under .NET, so this test itself is wrong with nunit 2.4.8.")]
2121
public void Test ()
2222
{
23-
new HttpChannel (8086);
23+
var port = NetworkHelpers.FindFreePort ();
24+
Hashtable props = new Hashtable ();
25+
props["port"] = port;
26+
props["bindTo"] = "127.0.0.1";
27+
new HttpChannel (props, null, null);
2428
RemotingServices.Marshal (new Service (), "test");
2529

2630
Service remObj = (Service) RemotingServices.Connect (
27-
typeof (Service), "http://localhost:8086/test");
31+
typeof (Service), $"http://127.0.0.1:{port}/test");
2832

2933
ArrayList list;
3034
remObj.Test (out list);
@@ -70,13 +74,16 @@ public void Method (string p1, string p2)
7074
public void Main ()
7175
{
7276
var port = NetworkHelpers.FindFreePort ();
73-
channel = new HttpChannel (port);
77+
Hashtable props = new Hashtable ();
78+
props["port"] = port;
79+
props["bindTo"] = "127.0.0.1";
80+
channel = new HttpChannel (props, null, null);
7481
ChannelServices.RegisterChannel (channel);
7582
RemotingConfiguration.RegisterWellKnownServiceType
7683
(typeof (Bug321420),"Server.soap", WellKnownObjectMode.Singleton);
7784

7885
Bug321420 s = (Bug321420) Activator.GetObject (typeof
79-
(Bug321420), $"http://localhost:{port}/Server.soap");
86+
(Bug321420), $"http://127.0.0.1:{port}/Server.soap");
8087

8188
// this works: s.Method ("a", "b");
8289
s.Method ("a", "a");
@@ -101,7 +108,7 @@ public class Bug315570
101108
public void Main ()
102109
{
103110
Foo foo = (Foo) Activator.GetObject (typeof (Foo),
104-
$"http://localhost:{server.HttpPort}/Test");
111+
$"http://127.0.0.1:{server.HttpPort}/Test");
105112

106113
Bar bar = foo.Login ();
107114
if (bar != null)
@@ -146,7 +153,10 @@ public class Server : MarshalByRefObject
146153
public void Start ()
147154
{
148155
HttpPort = NetworkHelpers.FindFreePort ();
149-
c = new HttpChannel (HttpPort);
156+
Hashtable props = new Hashtable ();
157+
props["port"] = HttpPort;
158+
props["bindTo"] = "127.0.0.1";
159+
c = new HttpChannel (props, null, null);
150160
ChannelServices.RegisterChannel (c);
151161

152162
Type t = typeof(Foo);
@@ -174,11 +184,14 @@ public class Bug315170
174184
[Test]
175185
public void Main ()
176186
{
177-
channel = new HttpChannel (0);
187+
Hashtable props = new Hashtable ();
188+
props["port"] = 0;
189+
props["bindTo"] = "127.0.0.1";
190+
channel = new HttpChannel (props, null, null);
178191
ChannelServices.RegisterChannel (channel);
179192
MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect (
180193
typeof (IFactorial),
181-
$"http://localhost:{server.HttpPort}/MyEndPoint");
194+
$"http://127.0.0.1:{server.HttpPort}/MyEndPoint");
182195
IFactorial cal = (IFactorial) obj;
183196
Assert.AreEqual (cal.CalculateFactorial (4), 24);
184197
}
@@ -216,7 +229,10 @@ public class Server : MarshalByRefObject
216229
public void Start ()
217230
{
218231
HttpPort = NetworkHelpers.FindFreePort ();
219-
c = new HttpChannel (HttpPort);
232+
Hashtable props = new Hashtable ();
233+
props["port"] = HttpPort;
234+
props["bindTo"] = "127.0.0.1";
235+
c = new HttpChannel (props, null, null);
220236
ChannelServices.RegisterChannel (c);
221237

222238
Type t = typeof(Calculator);

mcs/class/System.Runtime.Remoting/Test/RemotingServicesTest.cs

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ public void Connect ()
294294
IDictionary props = new Hashtable ();
295295
props ["name"] = objMarshal.Uri;
296296
props ["port"] = port;
297+
props ["bindTo"] = "127.0.0.1";
297298
TcpChannel chn = new TcpChannel (props, null, null);
298299
ChannelServices.RegisterChannel (chn);
299300

@@ -317,6 +318,7 @@ public void MarshalThrowException ()
317318
IDictionary props = new Hashtable ();
318319
props ["name"] = objMarshal.Uri;
319320
props ["port"] = port;
321+
props ["bindTo"] = "127.0.0.1";
320322
TcpChannel chn = new TcpChannel (props, null, null);
321323
ChannelServices.RegisterChannel (chn);
322324

@@ -346,7 +348,10 @@ public void MarshalThrowException ()
346348
public void ExecuteMessage ()
347349
{
348350
var port = NetworkHelpers.FindFreePort ();
349-
TcpChannel chn = new TcpChannel (port);
351+
IDictionary props = new Hashtable ();
352+
props ["port"] = port;
353+
props ["bindTo"] = "127.0.0.1";
354+
TcpChannel chn = new TcpChannel (props, null, null);
350355
ChannelServices.RegisterChannel (chn);
351356
try {
352357
MarshalObject objMarshal = NewMarshalObject ();
@@ -379,7 +384,10 @@ public void ExecuteMessage ()
379384
public void IsOneWay ()
380385
{
381386
var port = NetworkHelpers.FindFreePort ();
382-
TcpChannel chn = new TcpChannel (port);
387+
IDictionary props = new Hashtable ();
388+
props ["port"] = port;
389+
props ["bindTo"] = "127.0.0.1";
390+
TcpChannel chn = new TcpChannel (props, null, null);
383391
ChannelServices.RegisterChannel (chn);
384392
try {
385393
RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject.rem", WellKnownObjectMode.Singleton);
@@ -403,7 +411,10 @@ public void IsOneWay ()
403411
public void GetObjRefForProxy ()
404412
{
405413
var port = NetworkHelpers.FindFreePort ();
406-
TcpChannel chn = new TcpChannel (port);
414+
IDictionary props = new Hashtable ();
415+
props ["port"] = port;
416+
props ["bindTo"] = "127.0.0.1";
417+
TcpChannel chn = new TcpChannel (props, null, null);
407418
ChannelServices.RegisterChannel (chn);
408419
try {
409420
// Register le factory as a SAO
@@ -427,7 +438,10 @@ public void GetObjRefForProxy ()
427438
public void GetRealProxy ()
428439
{
429440
var port = NetworkHelpers.FindFreePort ();
430-
TcpChannel chn = new TcpChannel (port);
441+
IDictionary props = new Hashtable ();
442+
props ["port"] = port;
443+
props ["bindTo"] = "127.0.0.1";
444+
TcpChannel chn = new TcpChannel (props, null, null);
431445
ChannelServices.RegisterChannel (chn);
432446
try {
433447
RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MonoTests.System.Runtime.Remoting.RemotingServicesTest.MarshalObject.soap", WellKnownObjectMode.Singleton);
@@ -449,7 +463,10 @@ public void GetRealProxy ()
449463
public void SetObjectUriForMarshal ()
450464
{
451465
var port = NetworkHelpers.FindFreePort ();
452-
TcpChannel chn = new TcpChannel (port);
466+
IDictionary props = new Hashtable ();
467+
props ["port"] = port;
468+
props ["bindTo"] = "127.0.0.1";
469+
TcpChannel chn = new TcpChannel (props, null, null);
453470
ChannelServices.RegisterChannel (chn);
454471
try {
455472
MarshalObject objRem = NewMarshalObject ();
@@ -469,7 +486,10 @@ public void SetObjectUriForMarshal ()
469486
public void GetServeurTypeForUri ()
470487
{
471488
var port = NetworkHelpers.FindFreePort ();
472-
TcpChannel chn = new TcpChannel (port);
489+
IDictionary props = new Hashtable ();
490+
props ["port"] = port;
491+
props ["bindTo"] = "127.0.0.1";
492+
TcpChannel chn = new TcpChannel (props, null, null);
473493
Type type = typeof (MarshalObject);
474494
ChannelServices.RegisterChannel (chn);
475495
try {
@@ -491,7 +511,10 @@ public void GetServeurTypeForUri ()
491511
public void IsObjectOutOf ()
492512
{
493513
var port = NetworkHelpers.FindFreePort ();
494-
TcpChannel chn = new TcpChannel (port);
514+
IDictionary props = new Hashtable ();
515+
props ["port"] = port;
516+
props ["bindTo"] = "127.0.0.1";
517+
TcpChannel chn = new TcpChannel (props, null, null);
495518
ChannelServices.RegisterChannel (chn);
496519
try {
497520
RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "MarshalObject2.rem", WellKnownObjectMode.Singleton);
@@ -514,7 +537,10 @@ public void ApplicationNameTest ()
514537
{
515538
var port = NetworkHelpers.FindFreePort ();
516539
RemotingConfiguration.ApplicationName = "app";
517-
TcpChannel chn = new TcpChannel (port);
540+
IDictionary props = new Hashtable ();
541+
props ["port"] = port;
542+
props ["bindTo"] = "127.0.0.1";
543+
TcpChannel chn = new TcpChannel (props, null, null);
518544
ChannelServices.RegisterChannel (chn);
519545
try {
520546
RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "obj3.rem", WellKnownObjectMode.Singleton);
@@ -541,7 +567,10 @@ public void ApplicationNameTest ()
541567
public void GetObjectWithChannelDataTest ()
542568
{
543569
var port = NetworkHelpers.FindFreePort ();
544-
TcpChannel chn = new TcpChannel (port);
570+
IDictionary props = new Hashtable ();
571+
props ["port"] = port;
572+
props ["bindTo"] = "127.0.0.1";
573+
TcpChannel chn = new TcpChannel (props, null, null);
545574
ChannelServices.RegisterChannel (chn);
546575
try {
547576
RemotingConfiguration.RegisterWellKnownServiceType (typeof (MarshalObject), "getobjectwithchanneldata.rem", WellKnownObjectMode.Singleton);

mcs/class/System.Runtime.Remoting/Test/TcpCalls.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
using System;
10+
using System.Collections;
1011
using System.Runtime.Remoting;
1112
using System.Runtime.Remoting.Channels;
1213
using System.Runtime.Remoting.Channels.Tcp;
@@ -55,12 +56,18 @@ public class TcpChannelManager : ChannelManager
5556
{
5657
public override IChannelSender CreateClientChannel ()
5758
{
58-
return new TcpChannel (0);
59+
Hashtable props = new Hashtable ();
60+
props["port"] = 0;
61+
props["bindTo"] = "127.0.0.1";
62+
return new TcpChannel (props, null, null);
5963
}
6064

6165
public override IChannelReceiver CreateServerChannel ()
6266
{
63-
return new TcpChannel (0);
67+
Hashtable props = new Hashtable ();
68+
props["port"] = 0;
69+
props["bindTo"] = "127.0.0.1";
70+
return new TcpChannel (props, null, null);
6471
}
6572
}
6673
}

sdks/ios/harness/harness.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ void RunSim () {
153153
//
154154
// Test results are returned using an socket connection.
155155
//
156-
var host = Dns.GetHostEntry (Dns.GetHostName ());
157156
var server = new TcpListener (System.Net.IPAddress.Loopback, 0);
158157
server.Start ();
159158
int port = ((IPEndPoint)server.LocalEndpoint).Port;

0 commit comments

Comments
 (0)