[squeak-dev] Re: Port numbers in network tests

Frank Shearar frank.shearar at gmail.com
Thu Apr 4 17:18:42 UTC 2013


On 4 April 2013 17:34, Colin Putney <colin at wiresong.com> wrote:
>
>
>
> On Thu, Apr 4, 2013 at 5:30 AM, Frank Shearar <frank.shearar at gmail.com>
> wrote:
>
>>
>> In particular, this doesn't break any tests (but haven't yet verified
>> that it works for the above use case!):
>>
>> port
>>     | freePort socket |
>>     port ifNotNil: [^ port].
>>     freePort := 7766.
>>     socket := Socket newTCP.
>>     [[socket listenOn: freePort.
>>     freePort := freePort + 1.
>>     socket isWaitingForConnection] whileFalse]
>>         ensure: [socket destroy].
>>     ^ port := freePort.
>>
>> where port is a new instvar that allows us to remember the free port
>> we just found. Note that this only works because of TestCase's (too)
>> broad remit; one TestCase instance runs one test.
>
>
> I suggest trying ports randomly, rather than sequentially, as there's lower
> chance of collision.

Sure, so we'd say

    freePort := (10000 to: 40000) atRandom.

instead.

> Also, TestCase's broad remit is designed for exactly this sort of situation.
> It's a feature! :-)

That's true today, certainly :)

OK, so it's not a crazy implementation? It's been a long time since I
worked with sockets, and pretty much the first time I've worked with
sockets in Squeak.

frank

> Colin


More information about the Squeak-dev mailing list