lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ben Martz" <benma...@gmail.com>
Subject Re: TestRemoteSearchable and random port
Date Mon, 12 Jan 2009 21:50:22 GMT
I seem to recall that most methods that open endpoints have the option to
pass 0 as the port number and have the stack assign an available port to
you. Have you tried that with the HttpChannel constructor instead of just
choosing a random number? You could then save the value of port after the
channel is constructed. I don't know for sure if this is applicable to
HttpChannel or not but retry loops always make me nervous for some reason
and it may be a valid solution. Sorry I don't have time to look it up right
now.

Cheers,
Ben

On Mon, Jan 12, 2009 at 1:12 PM, TJ Kolev <tjkolev@gmail.com> wrote:

> 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();
>                }
>

-- 
13:37 - Someone stole the precinct toilet. The cops have nothing to go on.
14:37 - Officers dispatched to a daycare where a three-year-old was
resisting a rest.
21:11 - Hole found in nudist camp wall. Officers are looking into it.

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message