harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Zhang" <zhanghuang...@gmail.com>
Subject [classlib][LUNI | NIO] How to write stable tests for blocking write/read operations of Socket and SocketChannel
Date Sat, 24 Jun 2006 00:11:18 GMT
Hi everybody,

I'm struggling to write a stable test for blocking write/read operation of
Socket and SocketChannel.

Could you anybody help me out?


On 6/23/06, Andrew Zhang <zhanghuangzhu@gmail.com> wrote:
>
>  Hi Alexander,
>
> Thanks for your kind reminder.
>
> Certainly I'll use sth. like Support_PortManager.getNextPort() to avoid
> such port conflict issue.
>
> Here I just want to describe the problem.
>
> The test still fails on my machine sometimes.  Could anyone tell me how to
> write a stable test on this issue?
>
> Or is it a bug of RI? Or is it possible to write a theoretically stable
> test?
>
> Thanks a lot in advance!
>
>      public void test_SocketChannel_BlockWriteRead() throws IOException {
>         final int CAPACITY_NORMAL = 200;
>         InetSocketAddress localAddr1 = new InetSocketAddress("127.0.0.1
> ",1234);
>
>         ServerSocket server = new ServerSocket(1234);
>
>         SocketChannel channel = SocketChannel.open();
>         channel.connect(localAddr1);
>         Socket client = server.accept ();
>         client.setTcpNoDelay(true);
>         client.setSendBufferSize(1);
>
>         OutputStream out = client.getOutputStream();
>
>         byte[] sendBuf = new byte[CAPACITY_NORMAL * 2];
>         for (int i = 0; i < CAPACITY_NORMAL * 2; i++) {
>             sendBuf[i] = (byte) i;
>         }
>         // send CAPACITY_NORMAL * 2 bytes data
>         out.write(sendBuf);
>         out.flush();
>
>         // no matter out.close() or client.shutdownOutput () is used
>         // the test still fails sometimes.
>         // out.close();
>         client.shutdownOutput();
>
>
>         ByteBuffer buf1 = ByteBuffer.allocate(CAPACITY_NORMAL);
>         ByteBuffer buf2 = ByteBuffer.allocate(CAPACITY_NORMAL);
>         ByteBuffer[] buf ={buf1, buf2};
>
>         // should receive CAPACITY_NORMAL * 2 bytes data
>         long count = 0;
>         do{
>             long ret = channel.read(buf);
>             if(ret == -1){
>                 break;
>             }
>             count += ret;
>         }while(count < CAPACITY_NORMAL*2);
>         assertEquals(CAPACITY_NORMAL * 2, count);
>     }
>
>
>  On 6/23/06, Alexander Kleymenov <kleymenov@gmail.com> wrote:
> >
> > Hello Andrew,
> >
> > I ran the test on RI (1.4.2_04-b05 and 1.5.0-b64)  and could not
> > reproduce the failure. Is it still an issue?
> >
> > Can I suggest a little improvement for your test? Try do not use
> > hardcoded port numbers in the test.
> > Let the ServerSocket choose the available port by specifying 0 as
> > parameter.
> > I.e. write the test as:
> >
> >       ServerSocket server = new ServerSocket(0);
> >       InetSocketAddress localAddr1 = new InetSocketAddress("127.0.0.1",
> >               server.getLocalPort());
> >
> > Such a way prevents the test from "Address in use" exception.
> >
> > Thank You,
> > Alexander
> >
> > ---------------------------------------------------------------------
> > Terms of use : http://incubator.apache.org/harmony/mailing.html
> > To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
> > For additional commands, e-mail: harmony-dev-help@incubator.apache.org
> >
> >
>
>
> --
> Andrew Zhang
> China Software Development Lab, IBM
>



-- 
Andrew Zhang
China Software Development Lab, IBM

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