You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@harmony.apache.org by Mark Hindess <ma...@googlemail.com> on 2010/06/08 09:18:24 UTC

[classlib][luni] Confusing DatagramPacketTest

I was looking at failing tests on FreeBSD and I spotted the following
test in DatagramPacketTest.java:

    public void test_getPort() throws IOException {
        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
                InetAddress.getLocalHost(), 1000);
        assertEquals("Incorrect port returned", 1000, dp.getPort());

        final InetAddress localhost = InetAddress.getLocalHost();
        DatagramSocket socket = new DatagramSocket(0, localhost);
        final int port = socket.getLocalPort();

        final Object lock = new Object();

        Thread thread = new Thread(new Runnable() {
            public void run() {
                DatagramSocket socket = null;
                try {
                    socket = new DatagramSocket(0, localhost);
                    synchronized (lock) {
                        started = true;
                        lock.notifyAll();
                    }
                    socket.setSoTimeout(3000);
                    DatagramPacket packet = new DatagramPacket(new byte[256],
                            256);
                    socket.receive(packet);
                    socket.send(packet);
                    socket.close();
                } catch (IOException e) {
                    System.out.println("thread exception: " + e);
                    if (socket != null)
                        socket.close();
                }
            }
        });
        thread.start();

        socket.setSoTimeout(3000);
        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5,
                6 }, 6, localhost, port);
        synchronized (lock) {
            while (!started) {
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                }
            }
        }
        socket.send(packet);
        socket.receive(packet);
        socket.close();
        assertTrue("datagram received wrong port: " + packet.getPort(), packet
                .getPort() == port);
    }

Notice that main threads socket is being used to send to itself - not to
the other socket which is left waiting until timeout for a packet from 
itself that it hasn't sent yet!  I think this can be more simply written
as:

    public void test_getPort() throws IOException {
        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
                InetAddress.getLocalHost(), 1000);
        assertEquals("Incorrect port returned", 1000, dp.getPort());

        final InetAddress localhost = InetAddress.getLocalHost();
        DatagramSocket socket = new DatagramSocket(0, localhost);
        final int port = socket.getLocalPort();

        socket.setSoTimeout(3000);
        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5,
                6 }, 6, localhost, port);
        socket.send(packet);
        socket.receive(packet);
        socket.close();
        assertTrue("datagram received wrong port: " + packet.getPort(), packet
                .getPort() == port);
    }

Does that make sense or am I missing something?

Sadly they both fail on FreeBSD.

Regards,
 Mark.



Re: [classlib][luni] Confusing DatagramPacketTest

Posted by Charles Lee <li...@gmail.com>.
Got it. Thanks Mark.

On Wed, Jun 9, 2010 at 1:55 PM, Mark Hindess <ma...@googlemail.com>wrote:

>
> In message <AA...@mail.gmail.com>,
> Charles Lee writes:
> >
> > On Tue, Jun 8, 2010 at 3:18 PM, Mark Hindess <
> mark.hindess@googlemail.com>
> > wrote:
> > >
> > > I was looking at failing tests on FreeBSD and I spotted the following
> > > test in DatagramPacketTest.java:
> > >
> > >    public void test_getPort() throws IOException {
> > >        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
> > >                InetAddress.getLocalHost(), 1000);
> > >        assertEquals("Incorrect port returned", 1000, dp.getPort());
> > >
> > >        final InetAddress localhost = InetAddress.getLocalHost();
> > >        DatagramSocket socket = new DatagramSocket(0, localhost);
> > >        final int port = socket.getLocalPort();
> > >
> > >        final Object lock = new Object();
> > >
> > >        Thread thread = new Thread(new Runnable() {
> > >            public void run() {
> > >                DatagramSocket socket = null;
> > >                try {
> > >                    socket = new DatagramSocket(0, localhost);
> > >                    synchronized (lock) {
> > >                        started = true;
> > >                        lock.notifyAll();
> > >                    }
> > >                    socket.setSoTimeout(3000);
> > >                    DatagramPacket packet = new DatagramPacket(new
> byte[256],
> > >                            256);
> > >                    socket.receive(packet);
> > >                    socket.send(packet);
> > >                    socket.close();
> > >                } catch (IOException e) {
> > >                    System.out.println("thread exception: " + e);
> > >                    if (socket != null)
> > >                        socket.close();
> > >                }
> > >            }
> > >        });
> > >        thread.start();
> > >
> > >        socket.setSoTimeout(3000);
> > >        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3,
> 4, 5,
> > >                6 }, 6, localhost, port);
> > >        synchronized (lock) {
> > >            while (!started) {
> > >                try {
> > >                    lock.wait();
> > >                } catch (InterruptedException e) {
> > >                }
> > >            }
> > >        }
> > >        socket.send(packet);
> > >        socket.receive(packet);
> > >        socket.close();
> > >        assertTrue("datagram received wrong port: " + packet.getPort(),
> packet
> > >                .getPort() == port);
> > >    }
> > >
> > > Notice that main threads socket is being used to send to itself - not
> to
> > > the other socket which is left waiting until timeout for a packet from
> > > itself that it hasn't sent yet!  I think this can be more simply
> written
> > > as:
> > >
> > >    public void test_getPort() throws IOException {
> > >        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
> > >                InetAddress.getLocalHost(), 1000);
> > >        assertEquals("Incorrect port returned", 1000, dp.getPort());
> > >
> > >        final InetAddress localhost = InetAddress.getLocalHost();
> > >        DatagramSocket socket = new DatagramSocket(0, localhost);
> > >        final int port = socket.getLocalPort();
> > >
> > >        socket.setSoTimeout(3000);
> > >        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3,
> 4,
> > > 5,
> > >                6 }, 6, localhost, port);
> > >        socket.send(packet);
> > >        socket.receive(packet);
> > >        socket.close();
> > >        assertTrue("datagram received wrong port: " + packet.getPort(),
> packet
> > >                .getPort() == port);
> > >    }
> > >
> > > Does that make sense or am I missing something?
> >
> > It seems the original one transfer packet in two directions, but the
> > new version only transfer the packet in one way.
>
> The original does a send then receive in the testing thread.  But the
> other thread does only a receive which times out with an exception but
> the exception is ignored so it is not testing anything.
>
> I assume the intention was for the two threads to send each other a
> packet but the original test does not do that.
>
> The new version only has the original testing thread.  I don't think
> anything is lost.  (Except perhaps that the test would hang if the
> receive in the second thread did not timeout but there are more
> obvious/sane ways to test this.)
>
> > > Sadly they both fail on FreeBSD.
>
> I know why the fail on FreeBSD now.
>
> Regards,
>  Mark.
>
>
>


-- 
Yours sincerely,
Charles Lee

Re: [classlib][luni] Confusing DatagramPacketTest

Posted by Mark Hindess <ma...@googlemail.com>.
In message <AA...@mail.gmail.com>,
Charles Lee writes:
> 
> On Tue, Jun 8, 2010 at 3:18 PM, Mark Hindess <ma...@googlemail.com>
> wrote:
> >
> > I was looking at failing tests on FreeBSD and I spotted the following
> > test in DatagramPacketTest.java:
> >
> >    public void test_getPort() throws IOException {
> >        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
> >                InetAddress.getLocalHost(), 1000);
> >        assertEquals("Incorrect port returned", 1000, dp.getPort());
> >
> >        final InetAddress localhost = InetAddress.getLocalHost();
> >        DatagramSocket socket = new DatagramSocket(0, localhost);
> >        final int port = socket.getLocalPort();
> >
> >        final Object lock = new Object();
> >
> >        Thread thread = new Thread(new Runnable() {
> >            public void run() {
> >                DatagramSocket socket = null;
> >                try {
> >                    socket = new DatagramSocket(0, localhost);
> >                    synchronized (lock) {
> >                        started = true;
> >                        lock.notifyAll();
> >                    }
> >                    socket.setSoTimeout(3000);
> >                    DatagramPacket packet = new DatagramPacket(new byte[256],
> >                            256);
> >                    socket.receive(packet);
> >                    socket.send(packet);
> >                    socket.close();
> >                } catch (IOException e) {
> >                    System.out.println("thread exception: " + e);
> >                    if (socket != null)
> >                        socket.close();
> >                }
> >            }
> >        });
> >        thread.start();
> >
> >        socket.setSoTimeout(3000);
> >        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4, 5,
> >                6 }, 6, localhost, port);
> >        synchronized (lock) {
> >            while (!started) {
> >                try {
> >                    lock.wait();
> >                } catch (InterruptedException e) {
> >                }
> >            }
> >        }
> >        socket.send(packet);
> >        socket.receive(packet);
> >        socket.close();
> >        assertTrue("datagram received wrong port: " + packet.getPort(), packet
> >                .getPort() == port);
> >    }
> >
> > Notice that main threads socket is being used to send to itself - not to
> > the other socket which is left waiting until timeout for a packet from
> > itself that it hasn't sent yet!  I think this can be more simply written
> > as:
> >
> >    public void test_getPort() throws IOException {
> >        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
> >                InetAddress.getLocalHost(), 1000);
> >        assertEquals("Incorrect port returned", 1000, dp.getPort());
> >
> >        final InetAddress localhost = InetAddress.getLocalHost();
> >        DatagramSocket socket = new DatagramSocket(0, localhost);
> >        final int port = socket.getLocalPort();
> >
> >        socket.setSoTimeout(3000);
> >        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4,
> > 5,
> >                6 }, 6, localhost, port);
> >        socket.send(packet);
> >        socket.receive(packet);
> >        socket.close();
> >        assertTrue("datagram received wrong port: " + packet.getPort(), packet
> >                .getPort() == port);
> >    }
> >
> > Does that make sense or am I missing something?
>
> It seems the original one transfer packet in two directions, but the
> new version only transfer the packet in one way.

The original does a send then receive in the testing thread.  But the
other thread does only a receive which times out with an exception but
the exception is ignored so it is not testing anything.

I assume the intention was for the two threads to send each other a
packet but the original test does not do that.

The new version only has the original testing thread.  I don't think
anything is lost.  (Except perhaps that the test would hang if the
receive in the second thread did not timeout but there are more
obvious/sane ways to test this.)

> > Sadly they both fail on FreeBSD.

I know why the fail on FreeBSD now.

Regards,
 Mark.



Re: [classlib][luni] Confusing DatagramPacketTest

Posted by Charles Lee <li...@gmail.com>.
It seems the original one transfer packet in two directions, but the new
version only transfer the packet in one way.

On Tue, Jun 8, 2010 at 3:18 PM, Mark Hindess <ma...@googlemail.com>wrote:

>
> I was looking at failing tests on FreeBSD and I spotted the following
> test in DatagramPacketTest.java:
>
>    public void test_getPort() throws IOException {
>        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
>                InetAddress.getLocalHost(), 1000);
>        assertEquals("Incorrect port returned", 1000, dp.getPort());
>
>        final InetAddress localhost = InetAddress.getLocalHost();
>        DatagramSocket socket = new DatagramSocket(0, localhost);
>        final int port = socket.getLocalPort();
>
>        final Object lock = new Object();
>
>        Thread thread = new Thread(new Runnable() {
>            public void run() {
>                DatagramSocket socket = null;
>                try {
>                    socket = new DatagramSocket(0, localhost);
>                    synchronized (lock) {
>                        started = true;
>                        lock.notifyAll();
>                    }
>                    socket.setSoTimeout(3000);
>                    DatagramPacket packet = new DatagramPacket(new
> byte[256],
>                            256);
>                    socket.receive(packet);
>                    socket.send(packet);
>                    socket.close();
>                } catch (IOException e) {
>                    System.out.println("thread exception: " + e);
>                    if (socket != null)
>                        socket.close();
>                }
>            }
>        });
>        thread.start();
>
>        socket.setSoTimeout(3000);
>        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4,
> 5,
>                6 }, 6, localhost, port);
>        synchronized (lock) {
>            while (!started) {
>                try {
>                    lock.wait();
>                } catch (InterruptedException e) {
>                }
>            }
>        }
>        socket.send(packet);
>        socket.receive(packet);
>        socket.close();
>        assertTrue("datagram received wrong port: " + packet.getPort(),
> packet
>                .getPort() == port);
>    }
>
> Notice that main threads socket is being used to send to itself - not to
> the other socket which is left waiting until timeout for a packet from
> itself that it hasn't sent yet!  I think this can be more simply written
> as:
>
>    public void test_getPort() throws IOException {
>        DatagramPacket dp = new DatagramPacket("Hello".getBytes(), 5,
>                InetAddress.getLocalHost(), 1000);
>        assertEquals("Incorrect port returned", 1000, dp.getPort());
>
>        final InetAddress localhost = InetAddress.getLocalHost();
>        DatagramSocket socket = new DatagramSocket(0, localhost);
>        final int port = socket.getLocalPort();
>
>        socket.setSoTimeout(3000);
>        DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4,
> 5,
>                6 }, 6, localhost, port);
>        socket.send(packet);
>        socket.receive(packet);
>        socket.close();
>        assertTrue("datagram received wrong port: " + packet.getPort(),
> packet
>                .getPort() == port);
>    }
>
> Does that make sense or am I missing something?
>
> Sadly they both fail on FreeBSD.
>
> Regards,
>  Mark.
>
>
>


-- 
Yours sincerely,
Charles Lee