Hi TJ,

You can open an issue on JIRA
(https://issues.apache.org/jira/browse/LUCENENET) and submit your patches. 
But as far as I can see, your patch reduces the probability of a port
conflict but does not solve the real problem. I think, the real problem lies
in some previously running test cases not closing the ports appropriately.

DIGY

-----Original Message-----
From: TJ Kolev [mailto:tjko...@gmail.com] 
Sent: Monday, January 12, 2009 11:13 PM
To: lucene-net-dev@incubator.apache.org
Subject: TestRemoteSearchable and random port

Hello!

I was getting sporadic (more often then not) port conflicts on this
unit test (/Test/Search/TestRemoteSearchable.cs). I rewrote the test a
bit, and I have not had problems since. If this looks good, how do I
submit the patch?

tjk :)

Index: TestRemoteSearchable.cs
===================================================================
--- TestRemoteSearchable.cs     (revision 732813)
+++ TestRemoteSearchable.cs     (working copy)
@@ -16,7 +16,7 @@
  */

 using System;
-
+using System.Net.Sockets;
 using NUnit.Framework;

 using Lucene.Net.Documents;
@@ -38,13 +38,29 @@
                private static int port;
                private static bool serverStarted;

+               private const int MAX_PORT_TRIES = 10;
+
                [SetUp]
                public override void SetUp()
                {
                        base.SetUp();
                        Random rnd = new Random((int)(DateTime.Now.Ticks &
0x7fffffff));
-                       port = rnd.Next(System.Net.IPEndPoint.MinPort,
System.Net.IPEndPoint.MaxPort);
-                       httpChannel = new
System.Runtime.Remoting.Channels.Http.HttpChannel(port);
+
+                       int portTry = 0;
+                       while (true)
+                       {
+                               try
+                               {
+                                       port =
rnd.Next(System.Net.IPEndPoint.MinPort,
System.Net.IPEndPoint.MaxPort);
+                                       httpChannel = new
System.Runtime.Remoting.Channels.Http.HttpChannel(port);
+                                       break;
+                               }
+                               catch (SocketException)
+                               {
+                                       if (++portTry > MAX_PORT_TRIES)
+                                               throw;
+                               }
+                       }
                        if (!serverStarted)
                                StartServer();
                }

Reply via email to